Skip to content

fix(node): Ensure adding sentry-trace and baggage headers via SentryHttpInstrumentation doesn't crash #16473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/node/src/integrations/http/SentryHttpInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getSanitizedUrlString,
getTraceData,
httpRequestToRequestData,
isError,
logger,
LRUMap,
parseUrl,
Expand Down Expand Up @@ -262,15 +263,34 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns

// We do not want to overwrite existing header here, if it was already set
if (sentryTrace && !request.getHeader('sentry-trace')) {
request.setHeader('sentry-trace', sentryTrace);
logger.log(INSTRUMENTATION_NAME, 'Added sentry-trace header to outgoing request');
try {
request.setHeader('sentry-trace', sentryTrace);
DEBUG_BUILD && logger.log(INSTRUMENTATION_NAME, 'Added sentry-trace header to outgoing request');
} catch (error) {
DEBUG_BUILD &&
logger.error(
INSTRUMENTATION_NAME,
'Failed to add sentry-trace header to outgoing request:',
isError(error) ? error.message : 'Unknown error',
);
}
}

if (baggage) {
// For baggage, we make sure to merge this into a possibly existing header
const newBaggage = mergeBaggageHeaders(request.getHeader('baggage'), baggage);
if (newBaggage) {
request.setHeader('baggage', newBaggage);
try {
request.setHeader('baggage', newBaggage);
DEBUG_BUILD && logger.log(INSTRUMENTATION_NAME, 'Added baggage header to outgoing request');
} catch (error) {
DEBUG_BUILD &&
logger.error(
INSTRUMENTATION_NAME,
'Failed to add baggage header to outgoing request:',
isError(error) ? error.message : 'Unknown error',
);
}
}
}
}
Expand Down
Loading