Skip to content

Commit 5237667

Browse files
committed
use baggage header util
1 parent 8d066a4 commit 5237667

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

packages/opentelemetry/src/propagator.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
dynamicSamplingContextToSentryBaggageHeader,
1616
generateSentryTraceHeader,
1717
logger,
18+
parseBaggageHeader,
1819
propagationContextFromHeaders,
1920
stringMatchesSomePattern,
2021
} from '@sentry/utils';
@@ -101,9 +102,12 @@ export class SentryPropagator extends W3CBaggagePropagator {
101102
const { dynamicSamplingContext, traceId, spanId, sampled } = getInjectionData(context);
102103

103104
if (existingBaggageHeader) {
104-
const baggageEntries = parseBaggageHeaderString(existingBaggageHeader);
105-
for (const [key, value] of baggageEntries) {
106-
baggage = baggage.setEntry(key, { value });
105+
const baggageEntries = parseBaggageHeader(existingBaggageHeader);
106+
107+
if (baggageEntries) {
108+
Object.entries(baggageEntries).forEach(([key, value]) => {
109+
baggage = baggage.setEntry(key, { value });
110+
});
107111
}
108112
}
109113

@@ -329,14 +333,3 @@ function getExistingBaggage(carrier: unknown): string | undefined {
329333
return undefined;
330334
}
331335
}
332-
333-
/**
334-
* Will parse a baggage header into an array of tuples,
335-
* where the first item is the baggage name, the second the baggage value.
336-
*/
337-
function parseBaggageHeaderString(baggageHeader: string): [string, string][] {
338-
return baggageHeader.split(',').map(baggageEntry => {
339-
const [key, value] = baggageEntry.split('=');
340-
return [decodeURIComponent(key.trim()), decodeURIComponent(value.trim())];
341-
});
342-
}

packages/utils/src/baggage.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,10 @@ export function baggageHeaderToDynamicSamplingContext(
2828
// Very liberal definition of what any incoming header might look like
2929
baggageHeader: string | string[] | number | null | undefined | boolean,
3030
): Partial<DynamicSamplingContext> | undefined {
31-
if (!isString(baggageHeader) && !Array.isArray(baggageHeader)) {
32-
return undefined;
33-
}
34-
35-
// Intermediary object to store baggage key value pairs of incoming baggage headers on.
36-
// It is later used to read Sentry-DSC-values from.
37-
let baggageObject: Readonly<Record<string, string>> = {};
31+
const baggageObject = parseBaggageHeader(baggageHeader);
3832

39-
if (Array.isArray(baggageHeader)) {
40-
// Combine all baggage headers into one object containing the baggage values so we can later read the Sentry-DSC-values from it
41-
baggageObject = baggageHeader.reduce<Record<string, string>>((acc, curr) => {
42-
const currBaggageObject = baggageHeaderToObject(curr);
43-
for (const key of Object.keys(currBaggageObject)) {
44-
acc[key] = currBaggageObject[key];
45-
}
46-
return acc;
47-
}, {});
48-
} else {
49-
// Return undefined if baggage header is an empty string (technically an empty baggage header is not spec conform but
50-
// this is how we choose to handle it)
51-
if (!baggageHeader) {
52-
return undefined;
53-
}
54-
55-
baggageObject = baggageHeaderToObject(baggageHeader);
33+
if (!baggageObject) {
34+
return undefined;
5635
}
5736

5837
// Read all "sentry-" prefixed values out of the baggage object and put it onto a dynamic sampling context object.
@@ -104,6 +83,30 @@ export function dynamicSamplingContextToSentryBaggageHeader(
10483
return objectToBaggageHeader(sentryPrefixedDSC);
10584
}
10685

86+
/**
87+
* Take a baggage header and parse it into an object.
88+
*/
89+
export function parseBaggageHeader(
90+
baggageHeader: string | string[] | number | null | undefined | boolean,
91+
): Record<string, string> | undefined {
92+
if (!baggageHeader || (!isString(baggageHeader) && !Array.isArray(baggageHeader))) {
93+
return undefined;
94+
}
95+
96+
if (Array.isArray(baggageHeader)) {
97+
// Combine all baggage headers into one object containing the baggage values so we can later read the Sentry-DSC-values from it
98+
return baggageHeader.reduce<Record<string, string>>((acc, curr) => {
99+
const currBaggageObject = baggageHeaderToObject(curr);
100+
for (const key of Object.keys(currBaggageObject)) {
101+
acc[key] = currBaggageObject[key];
102+
}
103+
return acc;
104+
}, {});
105+
}
106+
107+
return baggageHeaderToObject(baggageHeader);
108+
}
109+
107110
/**
108111
* Will parse a baggage header, which is a simple key-value map, into a flat object.
109112
*

0 commit comments

Comments
 (0)