-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(v3): handle dot keys in flattenAttributes (#1510) #3185
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 |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| import { Attributes } from "@opentelemetry/api"; | ||
|
|
||
| function escapeKey(key: string): string { | ||
| return key.replace(/\./g, "\\."); | ||
| } | ||
|
Comment on lines
+3
to
+5
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. 🟡 Incomplete escape scheme: keys containing backslashes cause incorrect unflattening (regression) The new Reproduction scenarioConsider
The fix requires also escaping backslashes in Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| function unescapeKey(key: string): string { | ||
| return key.replace(/\\\./g, "."); | ||
| } | ||
|
Comment on lines
+3
to
+9
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. 🚩 Version mismatch risk: new flatten with old unflatten If a newer SDK client uses the new Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
|
|
||
| export const NULL_SENTINEL = "$@null(("; | ||
| export const CIRCULAR_REFERENCE_SENTINEL = "$@circular(("; | ||
|
|
||
|
|
@@ -24,7 +33,7 @@ class AttributeFlattener { | |
| constructor( | ||
| private maxAttributeCount?: number, | ||
| private maxDepth: number = DEFAULT_MAX_DEPTH | ||
| ) {} | ||
| ) { } | ||
|
|
||
| get attributes(): Attributes { | ||
| return this.result; | ||
|
|
@@ -117,7 +126,8 @@ class AttributeFlattener { | |
| if (!this.canAddMoreAttributes()) break; | ||
| // Use the key directly if it's a string, otherwise convert it | ||
| const keyStr = typeof key === "string" ? key : String(key); | ||
| this.#processValue(value, `${prefix || "map"}.${keyStr}`, depth); | ||
| this.#processValue(value, `${prefix || "map"}.${escapeKey(keyStr)}`, depth); | ||
|
|
||
| } | ||
| return; | ||
| } | ||
|
|
@@ -200,7 +210,9 @@ class AttributeFlattener { | |
| break; | ||
| } | ||
|
|
||
| const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`; | ||
| const escapedKey = Array.isArray(obj) ? `[${key}]` : escapeKey(key); | ||
| const newPrefix = `${prefix ? `${prefix}.` : ""}${escapedKey}`; | ||
|
|
||
|
|
||
| if (Array.isArray(value)) { | ||
| for (let i = 0; i < value.length; i++) { | ||
|
|
@@ -278,25 +290,27 @@ export function unflattenAttributes( | |
| continue; | ||
| } | ||
|
|
||
| const parts = key.split(".").reduce( | ||
| const parts = key.split(/(?<!\\)\./).reduce( | ||
| (acc, part) => { | ||
| if (part.startsWith("[") && part.endsWith("]")) { | ||
| const unescapedPart = unescapeKey(part); | ||
| if (unescapedPart.startsWith("[") && unescapedPart.endsWith("]")) { | ||
| // Handle array indices more precisely | ||
| const match = part.match(/^\[(\d+)\]$/); | ||
| const match = unescapedPart.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(unescapedPart.slice(1, -1)); | ||
| } | ||
| } else { | ||
| acc.push(part); | ||
| acc.push(unescapedPart); | ||
| } | ||
| return acc; | ||
| }, | ||
| [] as (string | number)[] | ||
| ); | ||
|
|
||
|
|
||
| // Skip keys that exceed max depth to prevent memory exhaustion | ||
| if (parts.length > maxDepth) { | ||
| continue; | ||
|
|
||
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.
🚩 Missing changeset for public package changes
Per
CLAUDE.mdandCONTRIBUTING.md, changes to packages underpackages/require a changeset (pnpm run changeset:add). This PR modifiespackages/coreandpackages/cli-v3(both public packages) but no.changeset/file appears in the diff. This may be intentional if changesets are added separately, but it's worth confirming before merge.Was this helpful? React with 👍 or 👎 to provide feedback.