Skip to content

Commit cff75ed

Browse files
committed
better trace flags & comments
1 parent dce6d66 commit cff75ed

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

packages/astro/test/server/meta.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { vi } from 'vitest';
33

44
import { getTracingMetaTags, isValidBaggageString } from '../../src/server/meta';
55

6+
const TRACE_FLAG_SAMPLED = 0x1;
7+
68
const mockedSpan = {
79
isRecording: () => true,
810
spanContext: () => {
911
return {
1012
traceId: '12345678901234567890123456789012',
1113
spanId: '1234567890123456',
12-
traceFlags: TraceFlagSampled,
14+
traceFlags: TRACE_FLAG_SAMPLED,
1315
};
1416
},
1517
transaction: {
@@ -19,9 +21,6 @@ const mockedSpan = {
1921
},
2022
} as any;
2123

22-
// eslint-disable-next-line no-bitwise
23-
const TraceFlagSampled = 0x1 << 0;
24-
2524
const mockedClient = {} as any;
2625

2726
const mockedScope = {
@@ -83,7 +82,7 @@ describe('getTracingMetaTags', () => {
8382
return {
8483
traceId: '12345678901234567890123456789012',
8584
spanId: '1234567890123456',
86-
traceFlags: TraceFlagSampled,
85+
traceFlags: TRACE_FLAG_SAMPLED,
8786
};
8887
},
8988
transaction: undefined,
@@ -111,7 +110,7 @@ describe('getTracingMetaTags', () => {
111110
return {
112111
traceId: '12345678901234567890123456789012',
113112
spanId: '1234567890123456',
114-
traceFlags: TraceFlagSampled,
113+
traceFlags: TRACE_FLAG_SAMPLED,
115114
};
116115
},
117116
transaction: undefined,

packages/core/src/tracing/span.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/ut
1717

1818
import { DEBUG_BUILD } from '../debug-build';
1919
import {
20-
TraceFlagNone,
21-
TraceFlagSampled,
20+
TRACE_FLAG_NONE,
21+
TRACE_FLAG_SAMPLED,
2222
spanTimeInputToSeconds,
2323
spanToTraceContext,
2424
spanToTraceHeader,
@@ -240,7 +240,7 @@ export class Span implements SpanInterface {
240240
return {
241241
spanId,
242242
traceId,
243-
traceFlags: sampled ? TraceFlagSampled : TraceFlagNone,
243+
traceFlags: sampled ? TRACE_FLAG_SAMPLED : TRACE_FLAG_NONE,
244244
};
245245
}
246246

packages/core/src/utils/spanUtils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import type { Span, SpanJSON, SpanTimeInput, TraceContext } from '@sentry/types'
22
import { dropUndefinedKeys, generateSentryTraceHeader, timestampInSeconds } from '@sentry/utils';
33
import type { Span as SpanClass } from '../tracing/span';
44

5-
export const TraceFlagNone = 0x0;
6-
// eslint-disable-next-line no-bitwise
7-
export const TraceFlagSampled = 0x1 << 0;
5+
// These are aligned with OpenTelemetry trace flags
6+
export const TRACE_FLAG_NONE = 0x0;
7+
export const TRACE_FLAG_SAMPLED = 0x1;
88

99
/**
1010
* Convert a span to a trace context, which can be sent as the `trace` context in an event.
1111
*/
1212
export function spanToTraceContext(span: Span): TraceContext {
13-
const { spanId: span_id, traceId: trace_id } = span;
13+
const { spanId: span_id, traceId: trace_id } = span.spanContext();
1414
const { data, description, op, parent_span_id, status, tags, origin } = spanToJSON(span);
1515

1616
return dropUndefinedKeys({
@@ -102,7 +102,9 @@ function spanIsSpanClass(span: Span): span is SpanClass {
102102
* So in the case where this distinction is important, use this method.
103103
*/
104104
export function spanIsSampled(span: Span): boolean {
105+
// We align our trace flags with the ones OpenTelemetry use
106+
// So we also check for sampled the same way they do.
105107
const { traceFlags } = span.spanContext();
106108
// eslint-disable-next-line no-bitwise
107-
return Boolean(traceFlags & TraceFlagSampled);
109+
return Boolean(traceFlags & TRACE_FLAG_SAMPLED);
108110
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { timestampInSeconds } from '@sentry/utils';
22
import { Span } from '../../../src';
3-
import { TraceFlagNone, TraceFlagSampled } from '../../../src/utils/spanUtils';
3+
import { TRACE_FLAG_NONE, TRACE_FLAG_SAMPLED } from '../../../src/utils/spanUtils';
44

55
describe('span', () => {
66
it('works with name', () => {
@@ -233,7 +233,7 @@ describe('span', () => {
233233
expect(span.spanContext()).toEqual({
234234
spanId: span['_spanId'],
235235
traceId: span['_traceId'],
236-
traceFlags: TraceFlagNone,
236+
traceFlags: TRACE_FLAG_NONE,
237237
});
238238
});
239239

@@ -242,7 +242,7 @@ describe('span', () => {
242242
expect(span.spanContext()).toEqual({
243243
spanId: span['_spanId'],
244244
traceId: span['_traceId'],
245-
traceFlags: TraceFlagSampled,
245+
traceFlags: TRACE_FLAG_SAMPLED,
246246
});
247247
});
248248

@@ -251,7 +251,7 @@ describe('span', () => {
251251
expect(span.spanContext()).toEqual({
252252
spanId: span['_spanId'],
253253
traceId: span['_traceId'],
254-
traceFlags: TraceFlagNone,
254+
traceFlags: TRACE_FLAG_NONE,
255255
});
256256
});
257257
});

packages/core/test/lib/utils/spanUtils.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TRACEPARENT_REGEXP, timestampInSeconds } from '@sentry/utils';
22
import { Span, spanToTraceHeader } from '../../../src';
3-
import { spanTimeInputToSeconds, spanToJSON, spanIsSampled } from '../../../src/utils/spanUtils';
3+
import { spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../../../src/utils/spanUtils';
44

55
describe('spanToTraceHeader', () => {
66
test('simple', () => {
@@ -51,8 +51,8 @@ describe('spanToJSON', () => {
5151
it('works with a simple span', () => {
5252
const span = new Span();
5353
expect(spanToJSON(span)).toEqual({
54-
span_id: span.spanId,
55-
trace_id: span.traceId,
54+
span_id: span.spanContext().spanId,
55+
trace_id: span.spanContext().traceId,
5656
origin: 'manual',
5757
start_timestamp: span.startTimestamp,
5858
});

packages/types/src/span.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export interface SpanJSON {
4343
origin?: SpanOrigin;
4444
}
4545

46-
const TraceFlagNone = 0x0;
47-
// eslint-disable-next-line no-bitwise
48-
const TraceFlagSampled = 0x1 << 0;
49-
export type TraceFlag = typeof TraceFlagNone | typeof TraceFlagSampled;
46+
// These are aligned with OpenTelemetry trace flags
47+
type TraceFlagNone = 0x0;
48+
type TraceFlagSampled = 0x1;
49+
export type TraceFlag = TraceFlagNone | TraceFlagSampled;
5050

5151
export interface SpanContextData {
5252
/**

0 commit comments

Comments
 (0)