Skip to content

Commit c722213

Browse files
committed
fix: Ensure DSC is correct
1 parent da287e6 commit c722213

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

packages/core/src/baseclient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
414414
public on(hook: 'beforeAddBreadcrumb', callback: (breadcrumb: Breadcrumb, hint?: BreadcrumbHint) => void): void;
415415

416416
/** @inheritdoc */
417-
public on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext) => void): void;
417+
public on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext, rootSpan?: Span) => void): void;
418418

419419
/** @inheritdoc */
420420
public on(
@@ -499,7 +499,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
499499
public emit(hook: 'beforeAddBreadcrumb', breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void;
500500

501501
/** @inheritdoc */
502-
public emit(hook: 'createDsc', dsc: DynamicSamplingContext): void;
502+
public emit(hook: 'createDsc', dsc: DynamicSamplingContext, rootSpan?: Span): void;
503503

504504
/** @inheritdoc */
505505
public emit(hook: 'beforeSendFeedback', feedback: FeedbackEvent, options?: { includeReplay: boolean }): void;

packages/core/src/tracing/dynamicSamplingContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
105105

106106
dsc.sampled = String(spanIsSampled(rootSpan));
107107

108-
client.emit('createDsc', dsc);
108+
client.emit('createDsc', dsc, rootSpan);
109109

110110
return dsc;
111111
}

packages/node/src/sdk/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import {
1515
openTelemetrySetupCheck,
1616
setOpenTelemetryContextAsyncContextStrategy,
17+
setupDscHandler,
1718
setupEventContextTrace,
1819
} from '@sentry/opentelemetry';
1920
import type { Client, Integration, Options } from '@sentry/types';
@@ -175,6 +176,7 @@ function _init(
175176
validateOpenTelemetrySetup();
176177
}
177178

179+
setupDscHandler(client);
178180
setupEventContextTrace(client);
179181
}
180182

packages/opentelemetry/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export { getDynamicSamplingContextFromSpan } from '@sentry/core';
2121

2222
export { isSentryRequestSpan } from './utils/isSentryRequest';
2323

24+
export { setupDscHandler } from './utils/setupDscHandler';
25+
2426
export { getActiveSpan } from './utils/getActiveSpan';
2527
export { startSpan, startSpanManual, startInactiveSpan, withActiveSpan, continueTrace } from './trace';
2628

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, spanToJSON } from '@sentry/core';
2+
import type { Client } from '@sentry/types';
3+
import { parseSpanDescription } from './parseSpanDescription';
4+
import { spanHasName } from './spanTypes';
5+
6+
/**
7+
* Setup a DSC handler on the passed client, ensuring that the transaction name is inferred from the span correctly.
8+
*/
9+
export function setupDscHandler(client: Client): void {
10+
client.on('createDsc', (dsc, rootSpan) => {
11+
// We want to overwrite the transaction on the DSC that is created by default in core
12+
// The reason for this is that we want to infer the span name, not use the initial one
13+
// Otherwise, we'll get names like "GET" instead of e.g. "GET /foo"
14+
// `parseSpanDescription` takes the attributes of the span into account for the name
15+
// This mutates the passed-in DSC
16+
if (rootSpan) {
17+
const jsonSpan = spanToJSON(rootSpan);
18+
const attributes = jsonSpan.data || {};
19+
const source = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
20+
21+
const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: undefined };
22+
if (source !== 'url' && description) {
23+
dsc.transaction = description;
24+
}
25+
}
26+
});
27+
}

packages/types/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export interface Client<O extends ClientOptions = ClientOptions> {
244244
/**
245245
* Register a callback when a DSC (Dynamic Sampling Context) is created.
246246
*/
247-
on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext) => void): void;
247+
on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext, rootSpan?: Span) => void): void;
248248

249249
/**
250250
* Register a callback when a Feedback event has been prepared.
@@ -338,7 +338,7 @@ export interface Client<O extends ClientOptions = ClientOptions> {
338338
/**
339339
* Fire a hook for when a DSC (Dynamic Sampling Context) is created. Expects the DSC as second argument.
340340
*/
341-
emit(hook: 'createDsc', dsc: DynamicSamplingContext): void;
341+
emit(hook: 'createDsc', dsc: DynamicSamplingContext, rootSpan?: Span): void;
342342

343343
/**
344344
* Fire a hook event for after preparing a feedback event. Events to be given

0 commit comments

Comments
 (0)