Skip to content

Commit 672b24c

Browse files
committed
feat(core): Add span.isRecording() instead of span.sampled
To align this with OTEL API.
1 parent 5eeb8a9 commit 672b24c

File tree

9 files changed

+47
-4
lines changed

9 files changed

+47
-4
lines changed

MIGRATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ In v8, the Span class is heavily reworked. The following properties & methods ar
4646
* `span.setName(newName)`: Use `span.updateName(newName)` instead.
4747
* `span.toTraceparent()`: use `spanToTraceHeader(span)` util instead.
4848
* `span.getTraceContext()`: Use `spanToTraceContext(span)` utility function instead.
49+
* `span.sampled`: Use `span.isRecording()` instead.
4950

5051
## Deprecate `pushScope` & `popScope` in favor of `withScope`
5152

packages/browser/src/profiling/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ export function shouldProfileTransaction(transaction: Transaction): boolean {
515515
return false;
516516
}
517517

518-
if (!transaction.sampled) {
518+
if (!transaction.isRecording()) {
519519
if (DEBUG_BUILD) {
520520
logger.log('[Profiling] Discarding profile because transaction was not sampled.');
521521
}

packages/bun/test/integrations/bunserver.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('Bun Serve Integration', () => {
8080
client.on('finishTransaction', transaction => {
8181
expect(transaction.traceId).toBe(TRACE_ID);
8282
expect(transaction.parentSpanId).toBe(PARENT_SPAN_ID);
83-
expect(transaction.sampled).toBe(true);
83+
expect(transaction.isRecording()).toBe(true);
8484

8585
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
8686
});

packages/core/src/tracing/hubextensions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function _startTransaction(
5555
The transaction will not be sampled. Please use the ${configInstrumenter} instrumentation to start transactions.`,
5656
);
5757

58+
// eslint-disable-next-line deprecation/deprecation
5859
transactionContext.sampled = false;
5960
}
6061

@@ -64,7 +65,7 @@ The transaction will not be sampled. Please use the ${configInstrumenter} instru
6465
transactionContext,
6566
...customSamplingContext,
6667
});
67-
if (transaction.sampled) {
68+
if (transaction.isRecording()) {
6869
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
6970
}
7071
if (client && client.emit) {
@@ -94,7 +95,7 @@ export function startIdleTransaction(
9495
transactionContext,
9596
...customSamplingContext,
9697
});
97-
if (transaction.sampled) {
98+
if (transaction.isRecording()) {
9899
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
99100
}
100101
if (client && client.emit) {

packages/core/src/tracing/sampling.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ export function sampleTransaction<T extends Transaction>(
2121
): T {
2222
// nothing to do if tracing is not enabled
2323
if (!hasTracingEnabled(options)) {
24+
// eslint-disable-next-line deprecation/deprecation
2425
transaction.sampled = false;
2526
return transaction;
2627
}
2728

2829
// if the user has forced a sampling decision by passing a `sampled` value in their transaction context, go with that
30+
// eslint-disable-next-line deprecation/deprecation
2931
if (transaction.sampled !== undefined) {
3032
transaction.setMetadata({
33+
// eslint-disable-next-line deprecation/deprecation
3134
sampleRate: Number(transaction.sampled),
3235
});
3336
return transaction;
@@ -60,6 +63,7 @@ export function sampleTransaction<T extends Transaction>(
6063
// only valid values are booleans or numbers between 0 and 1.)
6164
if (!isValidSampleRate(sampleRate)) {
6265
DEBUG_BUILD && logger.warn('[Tracing] Discarding transaction because of invalid sample rate.');
66+
// eslint-disable-next-line deprecation/deprecation
6367
transaction.sampled = false;
6468
return transaction;
6569
}
@@ -74,15 +78,18 @@ export function sampleTransaction<T extends Transaction>(
7478
: 'a negative sampling decision was inherited or tracesSampleRate is set to 0'
7579
}`,
7680
);
81+
// eslint-disable-next-line deprecation/deprecation
7782
transaction.sampled = false;
7883
return transaction;
7984
}
8085

8186
// Now we roll the dice. Math.random is inclusive of 0, but not of 1, so strict < is safe here. In case sampleRate is
8287
// a boolean, the < comparison will cause it to be automatically cast to 1 if it's true and 0 if it's false.
88+
// eslint-disable-next-line deprecation/deprecation
8389
transaction.sampled = Math.random() < (sampleRate as number | boolean);
8490

8591
// if we're not going to keep it, we're done
92+
// eslint-disable-next-line deprecation/deprecation
8693
if (!transaction.sampled) {
8794
DEBUG_BUILD &&
8895
logger.log(

packages/core/src/tracing/span.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export class Span implements SpanInterface {
154154
}
155155
// We want to include booleans as well here
156156
if ('sampled' in spanContext) {
157+
// eslint-disable-next-line deprecation/deprecation
157158
this.sampled = spanContext.sampled;
158159
}
159160
if (spanContext.op) {
@@ -193,6 +194,7 @@ export class Span implements SpanInterface {
193194
const childSpan = new Span({
194195
...spanContext,
195196
parentSpanId: this.spanId,
197+
// eslint-disable-next-line deprecation/deprecation
196198
sampled: this.sampled,
197199
traceId: this.traceId,
198200
});
@@ -352,6 +354,7 @@ export class Span implements SpanInterface {
352354
this.endTimestamp = spanContext.endTimestamp;
353355
this.op = spanContext.op;
354356
this.parentSpanId = spanContext.parentSpanId;
357+
// eslint-disable-next-line deprecation/deprecation
355358
this.sampled = spanContext.sampled;
356359
this.spanId = spanContext.spanId || this.spanId;
357360
this.startTimestamp = spanContext.startTimestamp || this.startTimestamp;
@@ -401,6 +404,11 @@ export class Span implements SpanInterface {
401404
});
402405
}
403406

407+
/** @inheritdoc */
408+
public isRecording(): boolean {
409+
return !this.endTimestamp && !!this.sampled;
410+
}
411+
404412
/**
405413
* Get the merged data for this span.
406414
* For now, this combines `data` and `attributes` together,

packages/core/test/lib/tracing/span.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ describe('span', () => {
174174
});
175175
});
176176

177+
describe('isRecording', () => {
178+
it('returns true for sampled span', () => {
179+
const span = new Span({ sampled: true });
180+
expect(span.isRecording()).toEqual(true);
181+
});
182+
183+
it('returns false for sampled, finished span', () => {
184+
const span = new Span({ sampled: true, endTimestamp: Date.now() });
185+
expect(span.isRecording()).toEqual(false);
186+
});
187+
188+
it('returns false for unsampled span', () => {
189+
const span = new Span({ sampled: false });
190+
expect(span.isRecording()).toEqual(false);
191+
});
192+
});
193+
177194
// Ensure that attributes & data are merged together
178195
describe('_getData', () => {
179196
it('works without data & attributes', () => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ export class BrowserTracing implements Integration {
333333
this._latestRouteName = finalContext.name;
334334
this._latestRouteSource = finalContext.metadata && finalContext.metadata.source;
335335

336+
// eslint-disable-next-line deprecation/deprecation
336337
if (finalContext.sampled === false) {
337338
DEBUG_BUILD && logger.log(`[Tracing] Will not send ${finalContext.op} transaction because of beforeNavigate.`);
338339
}

packages/types/src/span.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export interface SpanContext {
5454

5555
/**
5656
* Was this span chosen to be sent as part of the sample?
57+
*
58+
* @deprecated Use `isRecording()` instead.
5759
*/
5860
sampled?: boolean;
5961

@@ -264,4 +266,10 @@ export interface Span extends SpanContext {
264266
trace_id: string;
265267
origin?: SpanOrigin;
266268
};
269+
270+
/**
271+
* If this is span is actually recording data.
272+
* This will return false if tracing is disabled, this span was not sampled or if the span is already finished.
273+
*/
274+
isRecording(): boolean;
267275
}

0 commit comments

Comments
 (0)