Skip to content

Commit 38eb964

Browse files
authored
feat(core): Deprecate span.toTraceparent() in favor of spanToTraceHeader() util (#10031)
Instead, there is a new util `spanToTraceHeader(span)` which can be used. This is done to align the Span schema with OpenTelemetry. Open question: Do we need to re-export this from all our packages, so users can also use this directly?
1 parent 23ef22b commit 38eb964

File tree

22 files changed

+89
-30
lines changed

22 files changed

+89
-30
lines changed

MIGRATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ In v8, the Span class is heavily reworked. The following properties & methods ar
1515
* `span.toContext()`: Access the fields directly instead.
1616
* `span.updateWithContext(newSpanContext)`: Update the fields directly instead.
1717
* `span.setName(newName)`: Use `span.updateName(newName)` instead.
18+
* `span.toTraceparent()`: use `spanToTraceHeader(span)` util instead.
1819

1920
## Deprecate `pushScope` & `popScope` in favor of `withScope`
2021

packages/astro/src/server/meta.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDynamicSamplingContextFromClient } from '@sentry/core';
1+
import { getDynamicSamplingContextFromClient, spanToTraceHeader } from '@sentry/core';
22
import type { Client, Scope, Span } from '@sentry/types';
33
import {
44
TRACEPARENT_REGEXP,
@@ -30,7 +30,7 @@ export function getTracingMetaTags(
3030
const { dsc, sampled, traceId } = scope.getPropagationContext();
3131
const transaction = span?.transaction;
3232

33-
const sentryTrace = span ? span.toTraceparent() : generateSentryTraceHeader(traceId, undefined, sampled);
33+
const sentryTrace = span ? spanToTraceHeader(span) : generateSentryTraceHeader(traceId, undefined, sampled);
3434

3535
const dynamicSamplingContext = transaction
3636
? transaction.getDynamicSamplingContext()

packages/astro/test/server/meta.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { vi } from 'vitest';
44
import { getTracingMetaTags, isValidBaggageString } from '../../src/server/meta';
55

66
const mockedSpan = {
7-
toTraceparent: () => '12345678901234567890123456789012-1234567890123456-1',
7+
sampled: true,
8+
traceId: '12345678901234567890123456789012',
9+
spanId: '1234567890123456',
810
transaction: {
911
getDynamicSamplingContext: () => ({
1012
environment: 'production',
@@ -68,7 +70,9 @@ describe('getTracingMetaTags', () => {
6870
const tags = getTracingMetaTags(
6971
// @ts-expect-error - only passing a partial span object
7072
{
71-
toTraceparent: () => '12345678901234567890123456789012-1234567890123456-1',
73+
sampled: true,
74+
traceId: '12345678901234567890123456789012',
75+
spanId: '1234567890123456',
7276
transaction: undefined,
7377
},
7478
mockedScope,
@@ -89,7 +93,9 @@ describe('getTracingMetaTags', () => {
8993
const tags = getTracingMetaTags(
9094
// @ts-expect-error - only passing a partial span object
9195
{
92-
toTraceparent: () => '12345678901234567890123456789012-1234567890123456-1',
96+
sampled: true,
97+
traceId: '12345678901234567890123456789012',
98+
spanId: '1234567890123456',
9399
transaction: undefined,
94100
},
95101
mockedScope,

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export { prepareEvent } from './utils/prepareEvent';
6969
export { createCheckInEnvelope } from './checkin';
7070
export { hasTracingEnabled } from './utils/hasTracingEnabled';
7171
export { isSentryRequestUrl } from './utils/isSentryRequestUrl';
72+
export { spanToTraceHeader } from './utils/spanUtils';
7273
export { DEFAULT_ENVIRONMENT } from './constants';
7374
export { ModuleMetadata } from './integrations/metadata';
7475
export { RequestData } from './integrations/requestdata';

packages/core/src/tracing/hubextensions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { logger } from '@sentry/utils';
44
import { DEBUG_BUILD } from '../debug-build';
55
import type { Hub } from '../hub';
66
import { getMainCarrier } from '../hub';
7+
import { spanToTraceHeader } from '../utils/spanUtils';
78
import { registerErrorInstrumentation } from './errors';
89
import { IdleTransaction } from './idletransaction';
910
import { sampleTransaction } from './sampling';
@@ -16,7 +17,7 @@ function traceHeaders(this: Hub): { [key: string]: string } {
1617

1718
return span
1819
? {
19-
'sentry-trace': span.toTraceparent(),
20+
'sentry-trace': spanToTraceHeader(span),
2021
}
2122
: {};
2223
}

packages/core/src/tracing/span.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import type {
1010
TraceContext,
1111
Transaction,
1212
} from '@sentry/types';
13-
import { dropUndefinedKeys, generateSentryTraceHeader, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
13+
import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1414

1515
import { DEBUG_BUILD } from '../debug-build';
16+
import { spanToTraceHeader } from '../utils/spanUtils';
1617
import { ensureTimestampInSeconds } from './utils';
1718

1819
/**
@@ -320,7 +321,7 @@ export class Span implements SpanInterface {
320321
* @inheritDoc
321322
*/
322323
public toTraceparent(): string {
323-
return generateSentryTraceHeader(this.traceId, this.spanId, this.sampled);
324+
return spanToTraceHeader(this);
324325
}
325326

326327
/**

packages/core/src/utils/spanUtils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Span } from '@sentry/types';
2+
import { generateSentryTraceHeader } from '@sentry/utils';
3+
4+
/**
5+
* Convert a Span to a Sentry trace header.
6+
*/
7+
export function spanToTraceHeader(span: Span): string {
8+
return generateSentryTraceHeader(span.traceId, span.spanId, span.sampled);
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TRACEPARENT_REGEXP } from '@sentry/utils';
2+
import { Span, spanToTraceHeader } from '../../../src';
3+
4+
describe('spanToTraceHeader', () => {
5+
test('simple', () => {
6+
const span = new Span();
7+
expect(spanToTraceHeader(span)).toMatch(TRACEPARENT_REGEXP);
8+
});
9+
test('with sample', () => {
10+
const span = new Span({ sampled: true });
11+
expect(spanToTraceHeader(span)).toMatch(TRACEPARENT_REGEXP);
12+
});
13+
});

packages/nextjs/src/common/wrapAppGetInitialPropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addTracingExtensions, getClient, getCurrentScope } from '@sentry/core';
1+
import { addTracingExtensions, getClient, getCurrentScope, spanToTraceHeader } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type App from 'next/app';
44

@@ -63,7 +63,7 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI
6363
}
6464

6565
if (requestTransaction) {
66-
appGetInitialProps.pageProps._sentryTraceData = requestTransaction.toTraceparent();
66+
appGetInitialProps.pageProps._sentryTraceData = spanToTraceHeader(requestTransaction);
6767

6868
const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext();
6969
appGetInitialProps.pageProps._sentryBaggage =

packages/nextjs/src/common/wrapErrorGetInitialPropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addTracingExtensions, getClient, getCurrentScope } from '@sentry/core';
1+
import { addTracingExtensions, getClient, getCurrentScope, spanToTraceHeader } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type { NextPageContext } from 'next';
44
import type { ErrorProps } from 'next/error';
@@ -55,7 +55,7 @@ export function wrapErrorGetInitialPropsWithSentry(
5555

5656
const requestTransaction = getTransactionFromRequest(req) ?? getCurrentScope().getTransaction();
5757
if (requestTransaction) {
58-
errorGetInitialProps._sentryTraceData = requestTransaction.toTraceparent();
58+
errorGetInitialProps._sentryTraceData = spanToTraceHeader(requestTransaction);
5959

6060
const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext();
6161
errorGetInitialProps._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);

0 commit comments

Comments
 (0)