Skip to content

Commit ff6b436

Browse files
committed
last cleanup, one more test
1 parent 4caafd8 commit ff6b436

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

packages/node-core/src/utils/outgoingFetchRequest.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,12 @@ function _deduplicateArrayHeader(headers: string[], headerName: string): void {
147147
continue;
148148
}
149149

150-
if (headerName === SENTRY_BAGGAGE_HEADER) {
151-
// merge the initial entry into the later occurrence so that we keep the initial sentry- values around.
152-
// all other non-sentry values are merged
153-
const merged = mergeBaggageHeaders(headers[i + 1] as string, headers[firstIndex + 1] as string);
150+
const firstHeaderValue = headers[firstIndex + 1];
151+
if (headerName === SENTRY_BAGGAGE_HEADER && firstHeaderValue) {
152+
// mergeBaggageHeaders always takes sentry- values from the new baggage (2nd param) and merges
153+
// it with the existing one (1st param). Here, we want to keep the first header's existing
154+
// sentry- values in favor of the new ones. Hence we swap the parameters.
155+
const merged = mergeBaggageHeaders(headers[i + 1], firstHeaderValue);
154156
if (merged) {
155157
headers[firstIndex + 1] = merged;
156158
}

packages/node-core/src/utils/outgoingHttpRequest.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ export function addTracePropagationHeadersToOutgoingRequest(
6868

6969
const hasExistingSentryTraceHeader = !!request.getHeader('sentry-trace');
7070

71-
if (sentryTrace && !hasExistingSentryTraceHeader) {
71+
if (hasExistingSentryTraceHeader) {
72+
return;
73+
}
74+
75+
if (sentryTrace) {
7276
try {
7377
request.setHeader('sentry-trace', sentryTrace);
7478
DEBUG_BUILD && debug.log(LOG_PREFIX, 'Added sentry-trace header to outgoing request');
@@ -82,7 +86,7 @@ export function addTracePropagationHeadersToOutgoingRequest(
8286
}
8387
}
8488

85-
if (traceparent && !hasExistingSentryTraceHeader && !request.getHeader('traceparent')) {
89+
if (traceparent && !request.getHeader('traceparent')) {
8690
try {
8791
request.setHeader('traceparent', traceparent);
8892
DEBUG_BUILD && debug.log(LOG_PREFIX, 'Added traceparent header to outgoing request');
@@ -96,23 +100,9 @@ export function addTracePropagationHeadersToOutgoingRequest(
96100
}
97101
}
98102

99-
if (baggage && !hasExistingSentryTraceHeader) {
103+
if (baggage) {
100104
const existingBaggage = request.getHeader('baggage');
101-
102-
let cleanedExistingBaggage = existingBaggage;
103-
104-
// In the edge case that a baggage header with sentry- keys was added
105-
// BUT NO sentry-trace header, we overwrite the sentry- keys in the header we attach.
106-
// Therefore, we clean the existing baggage header of all sentry- keys.
107-
if (existingBaggage) {
108-
const tmpBaggage = parseBaggageHeader(existingBaggage);
109-
const baggageWithoutSentry = tmpBaggage
110-
? Object.fromEntries(Object.entries(tmpBaggage).filter(([key]) => !key.startsWith(SENTRY_BAGGAGE_KEY_PREFIX)))
111-
: {};
112-
cleanedExistingBaggage = objectToBaggageHeader(baggageWithoutSentry);
113-
}
114-
115-
const newBaggage = mergeBaggageHeaders(cleanedExistingBaggage, baggage);
105+
const newBaggage = mergeBaggageHeaders(existingBaggage, baggage);
116106
if (newBaggage) {
117107
try {
118108
request.setHeader('baggage', newBaggage);

packages/node-core/test/utils/baggage.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,10 @@ describe('mergeBaggageHeaders', () => {
158158
const result = mergeBaggageHeaders(existing, newBaggage);
159159
expect(result).toBe('non-sentry=keep,sentry-trace_id=new,sentry-environment=new');
160160
});
161+
162+
it('preserves existing sentry entries when new baggage has no sentry entries', () => {
163+
const result = mergeBaggageHeaders('sentry-release=1.0.0,foo=bar', 'baz=qux');
164+
165+
expect(result).toBe('sentry-release=1.0.0,foo=bar,baz=qux');
166+
});
161167
});

0 commit comments

Comments
 (0)