Skip to content

Commit

Permalink
add baggage
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Aug 2, 2023
1 parent 6a99eb1 commit d36e0fd
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/main/integrations/net-breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable deprecation/deprecation */
import { getDynamicSamplingContextFromClient } from '@sentry/core';
import { getCurrentHub } from '@sentry/node';
import { EventProcessor, Hub, Integration, Span, TracePropagationTargets } from '@sentry/types';
import { DynamicSamplingContext, EventProcessor, Hub, Integration, Span, TracePropagationTargets } from '@sentry/types';
import {
dynamicSamplingContextToSentryBaggageHeader,
fill,
Expand Down Expand Up @@ -109,6 +109,21 @@ function parseOptions(optionsIn: ClientRequestConstructorOptions | string): { me
};
}

function addHeadersToRequest(
request: Electron.ClientRequest,
url: string,
sentryTraceHeader: string,
dynamicSamplingContext?: Partial<DynamicSamplingContext>,
): void {
logger.log(`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${url}": `);
request.setHeader('sentry-trace', sentryTraceHeader);

const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
if (sentryBaggageHeader) {
request.setHeader('baggage', sentryBaggageHeader);
}
}

type RequestOptions = string | ClientRequestConstructorOptions;
type RequestMethod = (opt: RequestOptions) => ClientRequest;
type WrappedRequestMethodFactory = (original: RequestMethod) => RequestMethod;
Expand Down Expand Up @@ -207,25 +222,21 @@ function createWrappedRequestFactory(
});

if (shouldAttachTraceData(method, url)) {
request.setHeader('sentry-trace', span.toTraceparent());
const sentryTraceHeader = span.toTraceparent();
const dynamicSamplingContext = span?.transaction?.getDynamicSamplingContext();

addHeadersToRequest(request, url, sentryTraceHeader, dynamicSamplingContext);
}
} else {
if (shouldAttachTraceData(method, url)) {
const { traceId, sampled, dsc } = scope.getPropagationContext();
const sentryTraceHeader = generateSentryTraceHeader(traceId, undefined, sampled);

logger.log(`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${url}": `);

const client = hub.getClient();
const dynamicSamplingContext =
dsc || (client ? getDynamicSamplingContextFromClient(traceId, client, scope) : undefined);

request.setHeader('sentry-trace', sentryTraceHeader);

const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
if (sentryBaggageHeader) {
request.setHeader('baggage', sentryBaggageHeader);
}
addHeadersToRequest(request, url, sentryTraceHeader, dynamicSamplingContext);
}
}
}
Expand Down

0 comments on commit d36e0fd

Please sign in to comment.