Skip to content

Commit

Permalink
merge baggage together
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jan 31, 2025
1 parent f244bbc commit 2c8d0d6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/utils-hoist/baggage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function baggageHeaderToObject(baggageHeader: string): Record<string, string> {
* @returns a baggage header string, or `undefined` if the object didn't have any values, since an empty baggage header
* is not spec compliant.
*/
function objectToBaggageHeader(object: Record<string, string>): string | undefined {
export function objectToBaggageHeader(object: Record<string, string>): string | undefined {
if (Object.keys(object).length === 0) {
// An empty baggage header is not spec compliant: We return undefined.
return undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils-hoist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export {
baggageHeaderToDynamicSamplingContext,
dynamicSamplingContextToSentryBaggageHeader,
parseBaggageHeader,
objectToBaggageHeader,
} from './baggage';

export { getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment } from './url';
Expand Down
47 changes: 37 additions & 10 deletions packages/node/src/integrations/http/SentryHttpInstrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/* eslint-disable max-lines */
import type * as http from 'node:http';
import type { IncomingMessage, RequestOptions } from 'node:http';
import type * as https from 'node:https';
import type { EventEmitter } from 'node:stream';
/* eslint-disable max-lines */
import { VERSION } from '@opentelemetry/core';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
import type { AggregationCounts, Client, RequestEventData, SanitizedRequestData, Scope } from '@sentry/core';
import type {
AggregationCounts,
Client,
RequestEventData,
SanitizedRequestData,
Scope} from '@sentry/core';
import {
LRUMap,
addBreadcrumb,
Expand All @@ -18,6 +23,8 @@ import {
getTraceData,
httpRequestToRequestData,
logger,
objectToBaggageHeader,
parseBaggageHeader,
parseUrl,
stripUrlQueryAndFragment,
withIsolationScope,
Expand Down Expand Up @@ -493,14 +500,17 @@ function addSentryHeadersToRequestOptions(
}
const headers = options.headers;

Object.entries(addedHeaders).forEach(([k, v]) => {
// We do not want to overwrite existing headers here
// If the core HttpInstrumentation is registered, it will already have set the headers
// We do not want to add any then
if (!headers[k]) {
headers[k] = v;
}
});
const { 'sentry-trace': sentryTrace, baggage } = addedHeaders;

// We do not want to overwrite existing header here, if it was already set
if (sentryTrace && !headers['sentry-trace']) {
headers['sentry-trace'] = sentryTrace;
}

// For baggage, we make sure to merge this into a possibly existing header
if (baggage) {
headers['baggage'] = mergeBaggageHeaders(headers['baggage'], baggage);
}
}

/**
Expand Down Expand Up @@ -597,3 +607,20 @@ function getAbsoluteUrl(origin: string, path: string = '/'): string {
return `${url}${path}`;
}
}

function mergeBaggageHeaders(
existing: string | string[] | number | null | undefined | boolean,
baggage: string,
): string | undefined {
if (!existing) {
return baggage;
}

const existingBaggageEntries = parseBaggageHeader(existing);
const newBaggageEntries = parseBaggageHeader(baggage);

// Existing entries take precedence
const mergedBaggageEntries = { ...newBaggageEntries, ...existingBaggageEntries };

return objectToBaggageHeader(mergedBaggageEntries);
}

0 comments on commit 2c8d0d6

Please sign in to comment.