From d0529a3e2077f3643b930a1b6bf65e548d9aac5f Mon Sep 17 00:00:00 2001 From: shubham yadav <126192924+yadavshubham01@users.noreply.github.com> Date: Sat, 30 Nov 2024 00:28:02 +0530 Subject: [PATCH 1/3] Update flattenAttributes.ts --- packages/core/src/v3/utils/flattenAttributes.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/core/src/v3/utils/flattenAttributes.ts b/packages/core/src/v3/utils/flattenAttributes.ts index 545b0184e6..8efb70c2ea 100644 --- a/packages/core/src/v3/utils/flattenAttributes.ts +++ b/packages/core/src/v3/utils/flattenAttributes.ts @@ -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 | Array | string | boolean | number | null | undefined, prefix?: string , @@ -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) { @@ -86,6 +91,10 @@ function isRecord(value: unknown): value is Record { 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 | number | boolean | null | undefined { @@ -109,8 +118,10 @@ export function unflattenAttributes( const result: Record = {}; for (const [key, value] of Object.entries(obj)) { - const parts = key.split(".").reduce( - (acc, part) => { + const parts = key + .split(/(? { if (part.startsWith("[") && part.endsWith("]")) { // Handle array indices more precisely const match = part.match(/^\[(\d+)\]$/); From e5be66f551d9edef14a94be379a42f632c2e354b Mon Sep 17 00:00:00 2001 From: shubham yadav <126192924+yadavshubham01@users.noreply.github.com> Date: Sat, 30 Nov 2024 00:48:35 +0530 Subject: [PATCH 2/3] Update flattenAttributes.ts --- .../core/src/v3/utils/flattenAttributes.ts | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/packages/core/src/v3/utils/flattenAttributes.ts b/packages/core/src/v3/utils/flattenAttributes.ts index 8efb70c2ea..b41f28a175 100644 --- a/packages/core/src/v3/utils/flattenAttributes.ts +++ b/packages/core/src/v3/utils/flattenAttributes.ts @@ -118,26 +118,21 @@ export function unflattenAttributes( const result: Record = {}; for (const [key, value] of Object.entries(obj)) { - const parts = key - .split(/(? { - 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++) { From e3b982ee02b1cb4c3f763a5073d3e711148f5775 Mon Sep 17 00:00:00 2001 From: shubham yadav <126192924+yadavshubham01@users.noreply.github.com> Date: Sat, 30 Nov 2024 01:00:13 +0530 Subject: [PATCH 3/3] Update flattenAttributes.ts --- packages/core/src/v3/utils/flattenAttributes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/v3/utils/flattenAttributes.ts b/packages/core/src/v3/utils/flattenAttributes.ts index b41f28a175..57e291a693 100644 --- a/packages/core/src/v3/utils/flattenAttributes.ts +++ b/packages/core/src/v3/utils/flattenAttributes.ts @@ -132,7 +132,7 @@ export function unflattenAttributes( }, [] as (string | number)[]) .map(unescapeKey); // Unescape keys } - ); + let current: any = result; for (let i = 0; i < parts.length - 1; i++) {