Skip to content

Commit f6fadf8

Browse files
committed
add deprecation warning to toTraceparent and alias to private method
1 parent 269a678 commit f6fadf8

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, Transaction } 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 { computeTracestateValue } from './utils';
@@ -241,11 +241,9 @@ export class Span implements SpanInterface {
241241
* @inheritDoc
242242
*/
243243
public toTraceparent(): string {
244-
let sampledString = '';
245-
if (this.sampled !== undefined) {
246-
sampledString = this.sampled ? '-1' : '-0';
247-
}
248-
return `${this.traceId}-${this.spanId}${sampledString}`;
244+
logger.warn('Direct use of `span.toTraceparent` is deprecated. Use `span.getTraceHeaders` instead.');
245+
246+
return this._toSentrytrace();
249247
}
250248

251249
/**
@@ -293,7 +291,7 @@ export class Span implements SpanInterface {
293291
const tracestate = this._toTracestate();
294292

295293
return {
296-
'sentry-trace': this.toTraceparent(),
294+
'sentry-trace': this._toSentrytrace(),
297295
...(tracestate && { tracestate }),
298296
};
299297
}
@@ -382,6 +380,17 @@ export class Span implements SpanInterface {
382380
})}`;
383381
}
384382

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

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)