Skip to content

Commit c97a177

Browse files
authored
fix(encode): write truncated meta keys back to span.meta (#8236)
`tags-processors.js#truncateSpan` deleted an over-long meta key from `span.meta` and re-inserted the truncated key into `span.metrics`, which is the numeric-only map. Anything reading `span.metrics` then saw a string value where it expected a number; anything reading `span.meta` lost the tag entirely. The parallel metrics loop right below already mirrors back into `span.metrics`; the meta loop must mirror to `span.meta` to match. Drive-by fix: * Adjusted for-in to prevent iterating over inherited keys.
1 parent e5e3e4d commit c97a177

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

packages/dd-trace/src/encode/tags-processors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ function truncateSpan (span, shouldTruncateResourceName = true) {
3434
if (shouldTruncateResourceName && span.resource && span.resource.length > MAX_RESOURCE_NAME_LENGTH) {
3535
span.resource = `${span.resource.slice(0, MAX_RESOURCE_NAME_LENGTH)}...`
3636
}
37-
for (let metaKey in span.meta) {
37+
for (let metaKey of Object.keys(span.meta)) {
3838
const val = span.meta[metaKey]
3939
if (metaKey.length > MAX_META_KEY_LENGTH) {
4040
delete span.meta[metaKey]
4141
metaKey = `${metaKey.slice(0, MAX_META_KEY_LENGTH)}...`
42-
span.metrics[metaKey] = val
42+
span.meta[metaKey] = val
4343
}
4444
if (val && val.length > MAX_META_VALUE_LENGTH) {
4545
span.meta[metaKey] = `${val.slice(0, MAX_META_VALUE_LENGTH)}...`
4646
}
4747
}
48-
for (let metricsKey in span.metrics) {
48+
for (let metricsKey of Object.keys(span.metrics)) {
4949
const val = span.metrics[metricsKey]
5050
if (metricsKey.length > MAX_METRIC_KEY_LENGTH) {
5151
delete span.metrics[metricsKey]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const assert = require('node:assert/strict')
4+
5+
const { describe, it } = require('mocha')
6+
7+
require('../setup/core')
8+
9+
const {
10+
truncateSpan,
11+
MAX_META_KEY_LENGTH,
12+
MAX_METRIC_KEY_LENGTH,
13+
} = require('../../src/encode/tags-processors')
14+
15+
describe('tags-processors', () => {
16+
describe('truncateSpan', () => {
17+
it('writes a truncated meta key back to span.meta and not span.metrics', () => {
18+
const longKey = 'a'.repeat(MAX_META_KEY_LENGTH + 50)
19+
const expectedKey = `${'a'.repeat(MAX_META_KEY_LENGTH)}...`
20+
const span = {
21+
meta: { [longKey]: 'value' },
22+
metrics: {},
23+
}
24+
25+
truncateSpan(span)
26+
27+
assert.deepStrictEqual(span.meta, { [expectedKey]: 'value' })
28+
assert.deepStrictEqual(span.metrics, {})
29+
})
30+
31+
it('writes a truncated metric key back to span.metrics and not span.meta', () => {
32+
const longKey = 'b'.repeat(MAX_METRIC_KEY_LENGTH + 50)
33+
const expectedKey = `${'b'.repeat(MAX_METRIC_KEY_LENGTH)}...`
34+
const span = {
35+
meta: {},
36+
metrics: { [longKey]: 42 },
37+
}
38+
39+
truncateSpan(span)
40+
41+
assert.deepStrictEqual(span.meta, {})
42+
assert.deepStrictEqual(span.metrics, { [expectedKey]: 42 })
43+
})
44+
})
45+
})

0 commit comments

Comments
 (0)