Skip to content

Commit 7271640

Browse files
committed
add outgoing request baggage propagation to node Http integration
1 parent d7f1b94 commit 7271640

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

packages/node/src/integrations/http.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getCurrentHub } from '@sentry/core';
22
import { Integration, Span } from '@sentry/types';
3-
import { fill, logger, parseSemver } from '@sentry/utils';
3+
import { fill, logger, mergeAndSerializeBaggage, parseSemver } from '@sentry/utils';
44
import * as http from 'http';
55
import * as https from 'https';
66

@@ -123,7 +123,14 @@ function _createWrappedRequestMethodFactory(
123123
logger.log(
124124
`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to ${requestUrl}: `,
125125
);
126-
requestOptions.headers = { ...requestOptions.headers, 'sentry-trace': sentryTraceHeader };
126+
127+
const headerBaggageString = requestOptions.headers && (requestOptions.headers.baggage as string);
128+
129+
requestOptions.headers = {
130+
...requestOptions.headers,
131+
'sentry-trace': sentryTraceHeader,
132+
baggage: mergeAndSerializeBaggage(span.getBaggage(), headerBaggageString),
133+
};
127134
}
128135
}
129136

packages/tracing/src/browser/request.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
/* eslint-disable max-lines */
2-
import { Baggage } from '@sentry/types';
32
import {
43
addInstrumentationHandler,
54
BAGGAGE_HEADER_NAME,
6-
createBaggage,
7-
getThirdPartyBaggage,
85
isInstanceOf,
96
isMatchingPattern,
10-
parseBaggageString,
11-
serializeBaggage,
7+
mergeAndSerializeBaggage,
128
} from '@sentry/utils';
139

1410
import { Span } from '../span';
@@ -314,30 +310,3 @@ export function xhrCallback(
314310
}
315311
}
316312
}
317-
318-
/**
319-
* Merges the baggage header we saved from the incoming request (or meta tag) with
320-
* a possibly created or modified baggage header by a third party that's been added
321-
* to the outgoing request header.
322-
*
323-
* In case @param headerBaggage exists, we can safely add the the 3rd party part of @param headerBaggage
324-
* with our @param incomingBaggage. This is possible because if we modified anything beforehand,
325-
* it would only affect parts of the sentry baggage (@see Baggage interface).
326-
*
327-
* @param incomingBaggage the baggage header of the incoming request that might contain sentry entries
328-
* @param headerBaggageString possibly existing baggage header string added from a third party to request headers
329-
*
330-
* @return a merged and serialized baggage string to be propagated with the outgoing request
331-
*/
332-
function mergeAndSerializeBaggage(incomingBaggage?: Baggage, headerBaggageString?: string): string {
333-
if (!incomingBaggage && !headerBaggageString) {
334-
return '';
335-
}
336-
337-
const headerBaggage = (headerBaggageString && parseBaggageString(headerBaggageString)) || undefined;
338-
const thirdPartyHeaderBaggage = headerBaggage && getThirdPartyBaggage(headerBaggage);
339-
340-
const finalBaggage = createBaggage((incomingBaggage && incomingBaggage[0]) || {}, thirdPartyHeaderBaggage || '');
341-
342-
return serializeBaggage(finalBaggage);
343-
}

packages/utils/src/baggage.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,30 @@ export function parseBaggageString(inputBaggageString: string): Baggage {
8181
[{}, ''],
8282
);
8383
}
84+
85+
/**
86+
* Merges the baggage header we saved from the incoming request (or meta tag) with
87+
* a possibly created or modified baggage header by a third party that's been added
88+
* to the outgoing request header.
89+
*
90+
* In case @param headerBaggage exists, we can safely add the the 3rd party part of @param headerBaggage
91+
* with our @param incomingBaggage. This is possible because if we modified anything beforehand,
92+
* it would only affect parts of the sentry baggage (@see Baggage interface).
93+
*
94+
* @param incomingBaggage the baggage header of the incoming request that might contain sentry entries
95+
* @param headerBaggageString possibly existing baggage header string added from a third party to request headers
96+
*
97+
* @return a merged and serialized baggage string to be propagated with the outgoing request
98+
*/
99+
export function mergeAndSerializeBaggage(incomingBaggage?: Baggage, headerBaggageString?: string): string {
100+
if (!incomingBaggage && !headerBaggageString) {
101+
return '';
102+
}
103+
104+
const headerBaggage = (headerBaggageString && parseBaggageString(headerBaggageString)) || undefined;
105+
const thirdPartyHeaderBaggage = headerBaggage && getThirdPartyBaggage(headerBaggage);
106+
107+
const finalBaggage = createBaggage((incomingBaggage && incomingBaggage[0]) || {}, thirdPartyHeaderBaggage || '');
108+
109+
return serializeBaggage(finalBaggage);
110+
}

0 commit comments

Comments
 (0)