Skip to content

Commit

Permalink
simplify baggage code and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ida613 committed Nov 4, 2024
1 parent f58e746 commit eb92123
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
25 changes: 9 additions & 16 deletions packages/dd-trace/src/opentracing/propagation/text_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,19 @@ class TextMapPropagator {
})
}
if (this._hasPropagationStyle('inject', 'baggage')) {
if (this._config.baggageMaxItems < 1) return
let baggage = ''
let counter = 1
let itemCounter = 0
let byteCounter = 0

for (const [key, value] of Object.entries(spanContext._baggageItems)) {
baggage += `${this._encodeOtelBaggageKey(String(key).trim())}=${encodeURIComponent(String(value).trim())},`
if (counter === this._config.baggageMaxItems || counter > this._config.baggageMaxItems) break
counter += 1
let item = `${this._encodeOtelBaggageKey(String(key).trim())}=${encodeURIComponent(String(value).trim())},`

Check failure on line 131 in packages/dd-trace/src/opentracing/propagation/text_map.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10

Check failure on line 131 in packages/dd-trace/src/opentracing/propagation/text_map.js

View workflow job for this annotation

GitHub Actions / lint

'item' is never reassigned. Use 'const' instead
itemCounter += 1

Check failure on line 132 in packages/dd-trace/src/opentracing/propagation/text_map.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10
byteCounter += item.length

Check failure on line 133 in packages/dd-trace/src/opentracing/propagation/text_map.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10
if (itemCounter > this._config.baggageMaxItems || byteCounter > this._config.baggageMaxBytes) break

Check failure on line 134 in packages/dd-trace/src/opentracing/propagation/text_map.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10
baggage += item

Check failure on line 135 in packages/dd-trace/src/opentracing/propagation/text_map.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10
}

baggage = baggage.slice(0, baggage.length - 1)
let buf = Buffer.from(baggage)
if (buf.length > this._config.baggageMaxBytes) {
const originalBaggages = baggage.split(',')
buf = buf.subarray(0, this._config.baggageMaxBytes)
const truncatedBaggages = buf.toString('utf8').split(',')
const lastPairIndex = truncatedBaggages.length - 1
if (truncatedBaggages[lastPairIndex] !== originalBaggages[lastPairIndex]) {
truncatedBaggages.splice(lastPairIndex, 1)
}
baggage = truncatedBaggages.slice(0, this._config.baggageMaxItems).join(',')
}
if (baggage) carrier.baggage = baggage
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ describe('TextMapPropagator', () => {
it('should handle special characters in baggage', () => {
const carrier = {}
const baggageItems = {
'",;\\()/:<=>?@[]{}': '",;\\'
'",;\\()/:<=>?@[]{}🐶é我': '",;\\🐶é我'
}
const spanContext = createContext({ baggageItems })

propagator.inject(spanContext, carrier)
expect(carrier.baggage).to.be.equal('%22%2C%3B%5C%28%29%2F%3A%3C%3D%3E%3F%40%5B%5D%7B%7D=%22%2C%3B%5C')
expect(carrier.baggage).to.be.equal('%22%2C%3B%5C%28%29%2F%3A%3C%3D%3E%3F%40%5B%5D%7B%7D%F0%9F%90%B6%C3%A9%E6%88%91=%22%2C%3B%5C%F0%9F%90%B6%C3%A9%E6%88%91')

Check failure on line 111 in packages/dd-trace/test/opentracing/propagation/text_map.spec.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 163. Maximum allowed is 120
})

it('should drop excess baggage items when there are too many pairs', () => {
Expand Down

0 comments on commit eb92123

Please sign in to comment.