Skip to content

Commit e216a2d

Browse files
committed
feat(core): Deprecate span.toTraceparent() in favor of util
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?
1 parent 4c82160 commit e216a2d

File tree

20 files changed

+81
-29
lines changed

20 files changed

+81
-29
lines changed

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/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
/**
@@ -312,7 +313,7 @@ export class Span implements SpanInterface {
312313
* @inheritDoc
313314
*/
314315
public toTraceparent(): string {
315-
return generateSentryTraceHeader(this.traceId, this.spanId, this.sampled);
316+
return spanToTraceHeader(this);
316317
}
317318

318319
/**
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);

0 commit comments

Comments
 (0)