From 2136eb4f057a11b40899d03c6f20b7b586ef22f1 Mon Sep 17 00:00:00 2001 From: Ida Liu <119438987+ida613@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:36:06 -0500 Subject: [PATCH] simplify baggage code and add test case (#4858) --- .../src/opentracing/propagation/text_map.js | 25 +++++++------------ .../opentracing/propagation/text_map.spec.js | 5 ++-- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/packages/dd-trace/src/opentracing/propagation/text_map.js b/packages/dd-trace/src/opentracing/propagation/text_map.js index 57a16325690..4c67cfa5957 100644 --- a/packages/dd-trace/src/opentracing/propagation/text_map.js +++ b/packages/dd-trace/src/opentracing/propagation/text_map.js @@ -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 + const item = `${this._encodeOtelBaggageKey(String(key).trim())}=${encodeURIComponent(String(value).trim())},` + itemCounter += 1 + byteCounter += item.length + if (itemCounter > this._config.baggageMaxItems || byteCounter > this._config.baggageMaxBytes) break + baggage += item } + 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 } } diff --git a/packages/dd-trace/test/opentracing/propagation/text_map.spec.js b/packages/dd-trace/test/opentracing/propagation/text_map.spec.js index 4b2c85e55e5..45ddc905ee4 100644 --- a/packages/dd-trace/test/opentracing/propagation/text_map.spec.js +++ b/packages/dd-trace/test/opentracing/propagation/text_map.spec.js @@ -103,12 +103,13 @@ 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') + // eslint-disable-next-line max-len + 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') }) it('should drop excess baggage items when there are too many pairs', () => {