Skip to content

Commit 49ddce3

Browse files
committed
add deprecation warning to toTraceparent and alias to private method
1 parent 6e2e0ae commit 49ddce3

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

packages/tracing/src/span.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
22
import { getCurrentHub } from '@sentry/hub';
33
import { Hub, Primitive, Span as SpanInterface, SpanContext, TraceHeaders } from '@sentry/types';
4-
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
4+
import { dropUndefinedKeys, logger, timestampWithMs, uuid4 } from '@sentry/utils';
55

66
import { SpanStatus } from './spanstatus';
77
import { Transaction } from './transaction';
@@ -242,11 +242,9 @@ export class Span implements SpanInterface {
242242
* @inheritDoc
243243
*/
244244
public toTraceparent(): string {
245-
let sampledString = '';
246-
if (this.sampled !== undefined) {
247-
sampledString = this.sampled ? '-1' : '-0';
248-
}
249-
return `${this.traceId}-${this.spanId}${sampledString}`;
245+
logger.warn('Direct use of `span.toTraceparent` is deprecated. Use `span.getTraceHeaders` instead.');
246+
247+
return this._toSentrytrace();
250248
}
251249

252250
/**
@@ -294,7 +292,7 @@ export class Span implements SpanInterface {
294292
const tracestate = this._toTracestate();
295293

296294
return {
297-
'sentry-trace': this.toTraceparent(),
295+
'sentry-trace': this._toSentrytrace(),
298296
...(tracestate && { tracestate }),
299297
};
300298
}
@@ -383,6 +381,17 @@ export class Span implements SpanInterface {
383381
})}`;
384382
}
385383

384+
/**
385+
* Return a tracestate-compatible header string.
386+
*/
387+
private _toSentrytrace(): string {
388+
let sampledString = '';
389+
if (this.sampled !== undefined) {
390+
sampledString = this.sampled ? '-1' : '-0';
391+
}
392+
return `${this.traceId}-${this.spanId}${sampledString}`;
393+
}
394+
386395
/**
387396
* Return a tracestate-compatible header string. Returns undefined if there is no client or no DSN.
388397
*/

packages/tracing/test/span.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ describe('Span', () => {
9595

9696
describe('toTraceparent', () => {
9797
test('simple', () => {
98-
expect(new Span().toTraceparent()).toMatch(SENTRY_TRACE_REGEX);
98+
expect(new Span().getTraceHeaders()['sentry-trace']).toMatch(SENTRY_TRACE_REGEX);
9999
});
100100
test('with sample', () => {
101-
expect(new Span({ sampled: true }).toTraceparent()).toMatch(SENTRY_TRACE_REGEX);
101+
expect(new Span({ sampled: true }).getTraceHeaders()['sentry-trace']).toMatch(SENTRY_TRACE_REGEX);
102102
});
103103
});
104104

@@ -159,7 +159,7 @@ describe('Span', () => {
159159

160160
const headers = span.getTraceHeaders();
161161

162-
expect(headers['sentry-trace']).toEqual(span.toTraceparent());
162+
expect(headers['sentry-trace']).toEqual(`${span.traceId}-${span.spanId}-1`);
163163
expect(headers.tracestate).toEqual(transaction.metadata?.tracestate?.sentry);
164164
});
165165
});

packages/types/src/span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export interface Span extends SpanContext {
152152
/**
153153
* Return a traceparent-compatible header string
154154
*
155-
* NOTE: Do not use `span.toTraceparnt` directly. Use `span.getTraceHeaders` instead.
155+
* @deprecated Do not use `span.toTraceparnt` directly. Use `span.getTraceHeaders` instead.
156156
*/
157157
toTraceparent(): string; // TODO (kmclb) make this private
158158

0 commit comments

Comments
 (0)