Skip to content
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

simplify baggage code and add test case #4858

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
})
}
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 @@
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
Loading