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);

packages/nextjs/src/common/wrapGetInitialPropsWithSentry.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 { NextPage } from 'next';
44

@@ -51,7 +51,7 @@ export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialPro
5151

5252
const requestTransaction = getTransactionFromRequest(req) ?? getCurrentScope().getTransaction();
5353
if (requestTransaction) {
54-
initialProps._sentryTraceData = requestTransaction.toTraceparent();
54+
initialProps._sentryTraceData = spanToTraceHeader(requestTransaction);
5555

5656
const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext();
5757
initialProps._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);

packages/nextjs/src/common/wrapGetServerSidePropsWithSentry.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 { GetServerSideProps } from 'next';
44

@@ -48,7 +48,7 @@ export function wrapGetServerSidePropsWithSentry(
4848
if (serverSideProps && 'props' in serverSideProps) {
4949
const requestTransaction = getTransactionFromRequest(req) ?? getCurrentScope().getTransaction();
5050
if (requestTransaction) {
51-
serverSideProps.props._sentryTraceData = requestTransaction.toTraceparent();
51+
serverSideProps.props._sentryTraceData = spanToTraceHeader(requestTransaction);
5252

5353
const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext();
5454
serverSideProps.props._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);

packages/node/src/integrations/hapi/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
convertIntegrationFnToClass,
66
getActiveTransaction,
77
getCurrentScope,
8+
spanToTraceHeader,
89
startTransaction,
910
} from '@sentry/core';
1011
import type { IntegrationFn } from '@sentry/types';
@@ -93,7 +94,7 @@ export const hapiTracingPlugin = {
9394

9495
if (request.response && isResponseObject(request.response) && transaction) {
9596
const response = request.response as ResponseObject;
96-
response.header('sentry-trace', transaction.toTraceparent());
97+
response.header('sentry-trace', spanToTraceHeader(transaction));
9798

9899
const dynamicSamplingContext = dynamicSamplingContextToSentryBaggageHeader(
99100
transaction.getDynamicSamplingContext(),

packages/node/src/integrations/http.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type * as http from 'http';
22
import type * as https from 'https';
33
import type { Hub } from '@sentry/core';
4+
import { spanToTraceHeader } from '@sentry/core';
45
import { addBreadcrumb, getClient, getCurrentScope } from '@sentry/core';
56
import { getCurrentHub, getDynamicSamplingContextFromClient, isSentryRequestUrl } from '@sentry/core';
67
import type {
@@ -260,7 +261,7 @@ function _createWrappedRequestMethodFactory(
260261

261262
if (shouldAttachTraceData(rawRequestUrl)) {
262263
if (requestSpan) {
263-
const sentryTraceHeader = requestSpan.toTraceparent();
264+
const sentryTraceHeader = spanToTraceHeader(requestSpan);
264265
const dynamicSamplingContext = requestSpan?.transaction?.getDynamicSamplingContext();
265266
addHeadersToRequestOptions(requestOptions, requestUrl, sentryTraceHeader, dynamicSamplingContext);
266267
} else {

packages/node/src/integrations/undici/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getCurrentScope,
66
getDynamicSamplingContextFromClient,
77
isSentryRequestUrl,
8+
spanToTraceHeader,
89
} from '@sentry/core';
910
import type { EventProcessor, Integration, Span } from '@sentry/types';
1011
import {
@@ -183,7 +184,7 @@ export class Undici implements Integration {
183184
const dynamicSamplingContext = span?.transaction?.getDynamicSamplingContext();
184185
const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
185186

186-
setHeadersOnRequest(request, span.toTraceparent(), sentryBaggageHeader);
187+
setHeadersOnRequest(request, spanToTraceHeader(span), sentryBaggageHeader);
187188
} else {
188189
const { traceId, sampled, dsc } = scope.getPropagationContext();
189190
const sentryTrace = generateSentryTraceHeader(traceId, undefined, sampled);

packages/node/test/integrations/undici.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as http from 'http';
22
import type { Transaction } from '@sentry/core';
3+
import { spanToTraceHeader } from '@sentry/core';
34
import { Hub, makeMain, runWithAsyncContext } from '@sentry/core';
45
import type { fetch as FetchType } from 'undici';
56

@@ -207,7 +208,7 @@ conditionalTest({ min: 16 })('Undici integration', () => {
207208
expect(transaction.spanRecorder?.spans.length).toBe(2);
208209
const span = transaction.spanRecorder?.spans[1];
209210

210-
expect(requestHeaders['sentry-trace']).toEqual(span?.toTraceparent());
211+
expect(requestHeaders['sentry-trace']).toEqual(spanToTraceHeader(span!));
211212
expect(requestHeaders['baggage']).toEqual(
212213
`sentry-environment=production,sentry-public_key=0,sentry-trace_id=${transaction.traceId},sentry-sample_rate=1,sentry-transaction=test-transaction`,
213214
);

packages/opentelemetry-node/src/propagator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Baggage, Context, TextMapGetter, TextMapSetter } from '@opentelemetry/api';
22
import { TraceFlags, isSpanContextValid, propagation, trace } from '@opentelemetry/api';
33
import { W3CBaggagePropagator, isTracingSuppressed } from '@opentelemetry/core';
4+
import { spanToTraceHeader } from '@sentry/core';
45
import {
56
SENTRY_BAGGAGE_KEY_PREFIX,
67
baggageHeaderToDynamicSamplingContext,
@@ -32,7 +33,7 @@ export class SentryPropagator extends W3CBaggagePropagator {
3233

3334
const span = getSentrySpan(spanContext.spanId);
3435
if (span) {
35-
setter.set(carrier, SENTRY_TRACE_HEADER, span.toTraceparent());
36+
setter.set(carrier, SENTRY_TRACE_HEADER, spanToTraceHeader(span));
3637

3738
if (span.transaction) {
3839
const dynamicSamplingContext = span.transaction.getDynamicSamplingContext();

packages/remix/src/utils/instrumentServer.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/* eslint-disable max-lines */
2-
import { getActiveTransaction, getClient, getCurrentScope, hasTracingEnabled, runWithAsyncContext } from '@sentry/core';
2+
import {
3+
getActiveTransaction,
4+
getClient,
5+
getCurrentScope,
6+
hasTracingEnabled,
7+
runWithAsyncContext,
8+
spanToTraceHeader,
9+
} from '@sentry/core';
310
import type { Hub } from '@sentry/node';
411
import { captureException, getCurrentHub } from '@sentry/node';
512
import type { Transaction, TransactionSource, WrappedFunction } from '@sentry/types';
@@ -293,7 +300,7 @@ function getTraceAndBaggage(): {
293300
const dynamicSamplingContext = transaction.getDynamicSamplingContext();
294301

295302
return {
296-
sentryTrace: span.toTraceparent(),
303+
sentryTrace: spanToTraceHeader(span),
297304
sentryBaggage: dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext),
298305
};
299306
}

packages/sveltekit/src/server/handle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentScope } from '@sentry/core';
1+
import { getCurrentScope, spanToTraceHeader } from '@sentry/core';
22
import { getActiveTransaction, runWithAsyncContext, startSpan } from '@sentry/core';
33
import { captureException } from '@sentry/node';
44
/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
@@ -97,7 +97,7 @@ export function addSentryCodeToPage(options: SentryHandleOptions): NonNullable<R
9797
return ({ html }) => {
9898
const transaction = getActiveTransaction();
9999
if (transaction) {
100-
const traceparentData = transaction.toTraceparent();
100+
const traceparentData = spanToTraceHeader(transaction);
101101
const dynamicSamplingContext = dynamicSamplingContextToSentryBaggageHeader(
102102
transaction.getDynamicSamplingContext(),
103103
);

packages/tracing-internal/src/browser/request.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/* eslint-disable max-lines */
2-
import { getClient, getCurrentScope, getDynamicSamplingContextFromClient, hasTracingEnabled } from '@sentry/core';
2+
import {
3+
getClient,
4+
getCurrentScope,
5+
getDynamicSamplingContextFromClient,
6+
hasTracingEnabled,
7+
spanToTraceHeader,
8+
} from '@sentry/core';
39
import type { HandlerDataXhr, SentryWrappedXMLHttpRequest, Span } from '@sentry/types';
410
import {
511
BAGGAGE_HEADER_NAME,
@@ -291,7 +297,7 @@ export function xhrCallback(
291297
const transaction = span && span.transaction;
292298
const dynamicSamplingContext = transaction && transaction.getDynamicSamplingContext();
293299
const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
294-
setHeaderOnXhr(xhr, span.toTraceparent(), sentryBaggageHeader);
300+
setHeaderOnXhr(xhr, spanToTraceHeader(span), sentryBaggageHeader);
295301
} else {
296302
const client = getClient();
297303
const { traceId, sampled, dsc } = scope.getPropagationContext();

packages/tracing-internal/src/common/fetch.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { getClient, getCurrentScope, getDynamicSamplingContextFromClient, hasTracingEnabled } from '@sentry/core';
1+
import {
2+
getClient,
3+
getCurrentScope,
4+
getDynamicSamplingContextFromClient,
5+
hasTracingEnabled,
6+
spanToTraceHeader,
7+
} from '@sentry/core';
28
import type { Client, HandlerDataFetch, Scope, Span, SpanOrigin } from '@sentry/types';
39
import {
410
BAGGAGE_HEADER_NAME,
@@ -128,7 +134,7 @@ export function addTracingHeadersToFetchRequest(
128134

129135
const { traceId, sampled, dsc } = scope.getPropagationContext();
130136

131-
const sentryTraceHeader = span ? span.toTraceparent() : generateSentryTraceHeader(traceId, undefined, sampled);
137+
const sentryTraceHeader = span ? spanToTraceHeader(span) : generateSentryTraceHeader(traceId, undefined, sampled);
132138
const dynamicSamplingContext = transaction
133139
? transaction.getDynamicSamplingContext()
134140
: dsc

packages/types/src/span.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ export interface Span extends SpanContext {
225225
*/
226226
isSuccess(): boolean;
227227

228-
/** Return a traceparent compatible header string */
228+
/**
229+
* Return a traceparent compatible header string.
230+
* @deprecated Use `spanToTraceHeader()` instead.
231+
*/
229232
toTraceparent(): string;
230233

231234
/**

0 commit comments

Comments
 (0)