-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: escape periods in attribute keys during flatten/unflatten #3975
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { Attributes } from "@opentelemetry/api"; | |
|
|
||
| export const NULL_SENTINEL = "$@null(("; | ||
| export const CIRCULAR_REFERENCE_SENTINEL = "$@circular(("; | ||
| const DOT_ESCAPE = "$@dot"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚩 Backward compatibility: old unflatten code won't understand $@dot in keys This change alters the wire format of flattened attributes. Keys that previously contained literal dots (e.g., Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| const DEFAULT_MAX_DEPTH = 128; | ||
|
|
||
|
|
@@ -200,7 +201,8 @@ class AttributeFlattener { | |
| break; | ||
| } | ||
|
|
||
| const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`; | ||
| const escapedKey = Array.isArray(obj) ? `[${key}]` : key.replace(/\./g, DOT_ESCAPE); | ||
| const newPrefix = `${prefix ? `${prefix}.` : ""}${escapedKey}`; | ||
|
|
||
| if (Array.isArray(value)) { | ||
| for (let i = 0; i < value.length; i++) { | ||
|
|
@@ -280,17 +282,18 @@ export function unflattenAttributes( | |
|
|
||
| const parts = key.split(".").reduce( | ||
| (acc, part) => { | ||
| if (part.startsWith("[") && part.endsWith("]")) { | ||
| const unescaped = part.split(DOT_ESCAPE).join("."); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Escape sentinel The escaping mechanism replaces For example, Minimal reproduction and suggested fix approachA proper escape scheme needs to escape the escape character first, e.g.:
Alternatively, choose a longer/more distinctive sentinel like Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| if (unescaped.startsWith("[") && unescaped.endsWith("]")) { | ||
| // Handle array indices more precisely | ||
| const match = part.match(/^\[(\d+)\]$/); | ||
| const match = unescaped.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)); | ||
| acc.push(unescaped.slice(1, -1)); | ||
| } | ||
| } else { | ||
| acc.push(part); | ||
| acc.push(unescaped); | ||
| } | ||
| return acc; | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚩 Map keys with dots are not escaped, inconsistent with object key handling
The Map handling code at
packages/core/src/v3/utils/flattenAttributes.ts:120-121constructs the flattened key without escaping dots:Meanwhile, object keys ARE escaped at line 204:
key.replace(/\./g, DOT_ESCAPE). This means a Map with a string key like"a.b"will still have the dot treated as a hierarchy separator, producing an incorrect unflattened result. This is not a regression (Map keys were never escaped before this PR), but it is an inconsistency — the PR fixes the problem for plain objects but leaves Map objects with the same original issue. If the intent is to fully support dots in keys, Map keys should receive the same treatment.(Refers to lines 120-121)
Was this helpful? React with 👍 or 👎 to provide feedback.