Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Handling of Keys with Periods in flattenAttributes and unflattenAttributes #1514

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions packages/core/src/v3/utils/flattenAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { Attributes } from "@opentelemetry/api";
export const NULL_SENTINEL = "$@null((";
export const CIRCULAR_REFERENCE_SENTINEL = "$@circular((";

function escapeKey(key: string): string {
return key.replace(/\./g, '\\.');
}

export function flattenAttributes(
obj: Record<string, unknown> | Array<unknown> | string | boolean | number | null | undefined,
prefix?: string ,
Expand Down Expand Up @@ -53,7 +57,8 @@ export function flattenAttributes(


for (const [key, value] of Object.entries(obj)) {
const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`;
const escapedKey = escapeKey(key);
const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${escapedKey}]` : escapedKey}`;
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (typeof value[i] === "object" && value[i] !== null) {
Expand Down Expand Up @@ -86,6 +91,10 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}

function unescapeKey(key: string): string {
return key.replace(/\\\./g, '.');
}

export function unflattenAttributes(
obj: Attributes
): Record<string, unknown> | string | number | boolean | null | undefined {
Expand All @@ -109,24 +118,21 @@ export function unflattenAttributes(
const result: Record<string, unknown> = {};

for (const [key, value] of Object.entries(obj)) {
const parts = key.split(".").reduce(
(acc, part) => {
if (part.startsWith("[") && part.endsWith("]")) {
// Handle array indices more precisely
const match = part.match(/^\[(\d+)\]$/);
if (match && match[1]) {
acc.push(parseInt(match[1]));
} else {
// Remove brackets for non-numeric array keys
acc.push(part.slice(1, -1));
}
} else {
acc.push(part);
}
return acc;
},
[] as (string | number)[]
);
const parts = key
.split('.') // Split by all dots
.reduce((acc, part, i, arr) => {
// If the previous part ends with an odd number of backslashes,
// the dot is escaped and should be part of the last segment.
if (i > 0 && arr[i - 1].match(/\\+$/)?.[0]?.length % 2 === 1) {
acc[acc.length - 1] += '.' + part;
} else {
acc.push(part);
}
return acc;
}, [] as (string | number)[])
.map(unescapeKey); // Unescape keys
}


let current: any = result;
for (let i = 0; i < parts.length - 1; i++) {
Expand Down
Loading