Skip to content

Commit

Permalink
feat(baggage): include baggage metadata when propagating baggage entr…
Browse files Browse the repository at this point in the history
…ies (#2766)

Co-authored-by: legendecas <legendecas@gmail.com>
  • Loading branch information
chrskrchr and legendecas authored Feb 9, 2022
1 parent 4f8849f commit 1dd8a89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 11 additions & 6 deletions packages/opentelemetry-core/src/baggage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ export function serializeKeyPairs(keyPairs: string[]): string {
}

export function getKeyPairs(baggage: Baggage): string[] {
return baggage
.getAllEntries()
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(value.value)}`
);
return baggage.getAllEntries().map(([key, value]) => {
let entry = `${encodeURIComponent(key)}=${encodeURIComponent(value.value)}`;

// include opaque metadata if provided
// NOTE: we intentionally don't URI-encode the metadata - that responsibility falls on the metadata implementation
if (value.metadata !== undefined) {
entry += BAGGAGE_PROPERTIES_SEPARATOR + value.metadata.toString();
}

return entry;
});
}

export function parsePairKeyValue(entry: string): ParsedBaggageKeyValue | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
defaultTextMapGetter,
defaultTextMapSetter,
propagation,
baggageEntryMetadataFromString
} from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/api';
import * as assert from 'assert';
Expand All @@ -39,8 +40,9 @@ describe('W3CBaggagePropagator', () => {
it('should set baggage header', () => {
const baggage = propagation.createBaggage({
key1: { value: 'd4cda95b652f4a1592b449d5929fda1b' },
key3: { value: 'c88815a7-0fa9-4d95-a1f1-cdccce3c5c2a' },
'with/slash': { value: 'with spaces' },
key3: { value: 'c88815a7-0fa9-4d95-a1f1-cdccce3c5c2a' },
key4: { value: 'foo', metadata: baggageEntryMetadataFromString('key4prop1=value1;key4prop2=value2;key4prop3WithNoValue') }
});

httpBaggagePropagator.inject(
Expand All @@ -50,7 +52,7 @@ describe('W3CBaggagePropagator', () => {
);
assert.deepStrictEqual(
carrier[BAGGAGE_HEADER],
'key1=d4cda95b652f4a1592b449d5929fda1b,key3=c88815a7-0fa9-4d95-a1f1-cdccce3c5c2a,with%2Fslash=with%20spaces'
'key1=d4cda95b652f4a1592b449d5929fda1b,with%2Fslash=with%20spaces,key3=c88815a7-0fa9-4d95-a1f1-cdccce3c5c2a,key4=foo;key4prop1=value1;key4prop2=value2;key4prop3WithNoValue'
);
});

Expand Down

0 comments on commit 1dd8a89

Please sign in to comment.