Skip to content

Commit 8d066a4

Browse files
committed
fix circular dependency & tests
1 parent a520f44 commit 8d066a4

File tree

8 files changed

+54
-45
lines changed

8 files changed

+54
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ describe('getRootSpan', () => {
234234
expect(getRootSpan(root)).toBe(root);
235235
});
236236

237-
it('returns the root span of a child span xxx', () => {
237+
it('returns the root span of a child span', () => {
238238
startSpan({ name: 'outer' }, root => {
239239
startSpan({ name: 'inner' }, inner => {
240240
expect(getRootSpan(inner)).toBe(root);

packages/opentelemetry/src/propagator.ts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import { DEBUG_BUILD } from './debug-build';
3030
import { getScopesFromContext, setScopesOnContext } from './utils/contextData';
3131
import { getDynamicSamplingContextFromSpan } from './utils/dynamicSamplingContext';
32+
import { getSamplingDecision } from './utils/getSamplingDecision';
3233
import { setIsSetup } from './utils/setupCheck';
3334

3435
/** Get the Sentry propagation context from a span context. */
@@ -319,44 +320,6 @@ export function continueTraceAsRemoteSpan<T>(
319320
return context.with(ctxWithSpanContext, callback);
320321
}
321322

322-
/**
323-
* OpenTelemetry only knows about SAMPLED or NONE decision,
324-
* but for us it is important to differentiate between unset and unsampled.
325-
*
326-
* Both of these are identified as `traceFlags === TracegFlags.NONE`,
327-
* but we additionally look at a special trace state to differentiate between them.
328-
*/
329-
export function getSamplingDecision(spanContext: SpanContext): boolean | undefined {
330-
const { traceFlags, traceState } = spanContext;
331-
332-
const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
333-
334-
// If trace flag is `SAMPLED`, we interpret this as sampled
335-
// If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
336-
// For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
337-
// to identify which it is
338-
if (traceFlags === TraceFlags.SAMPLED) {
339-
return true;
340-
}
341-
342-
if (sampledNotRecording) {
343-
return false;
344-
}
345-
346-
// Fall back to DSC as a last resort, that may also contain `sampled`...
347-
const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
348-
const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
349-
350-
if (dsc?.sampled === 'true') {
351-
return true;
352-
}
353-
if (dsc?.sampled === 'false') {
354-
return false;
355-
}
356-
357-
return undefined;
358-
}
359-
360323
/** Try to get the existing baggage header so we can merge this in. */
361324
function getExistingBaggage(carrier: unknown): string | undefined {
362325
try {

packages/opentelemetry/src/sampler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { logger } from '@sentry/utils';
99
import { SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING } from './constants';
1010

1111
import { DEBUG_BUILD } from './debug-build';
12-
import { getPropagationContextFromSpan, getSamplingDecision } from './propagator';
12+
import { getPropagationContextFromSpan } from './propagator';
13+
import { getSamplingDecision } from './utils/getSamplingDecision';
1314
import { setIsSetup } from './utils/setupCheck';
1415

1516
/**

packages/opentelemetry/src/trace.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import {
1414
spanToJSON,
1515
} from '@sentry/core';
1616
import type { Client, Scope } from '@sentry/types';
17-
import { continueTraceAsRemoteSpan, getSamplingDecision, makeTraceState } from './propagator';
17+
import { continueTraceAsRemoteSpan, makeTraceState } from './propagator';
1818

1919
import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types';
2020
import { getContextFromScope, getScopesFromContext } from './utils/contextData';
2121
import { getDynamicSamplingContextFromSpan } from './utils/dynamicSamplingContext';
22+
import { getSamplingDecision } from './utils/getSamplingDecision';
2223

2324
/**
2425
* Wraps a function with a transaction/span and finishes the span after the function is done.

packages/opentelemetry/src/utils/dynamicSamplingContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
import type { DynamicSamplingContext } from '@sentry/types';
88
import { baggageHeaderToDynamicSamplingContext } from '@sentry/utils';
99
import { SENTRY_TRACE_STATE_DSC } from '../constants';
10-
import { getSamplingDecision } from '../propagator';
1110
import type { AbstractSpan } from '../types';
11+
import { getSamplingDecision } from './getSamplingDecision';
1212
import { parseSpanDescription } from './parseSpanDescription';
1313
import { spanHasAttributes, spanHasName } from './spanTypes';
1414

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { SpanContext } from '@opentelemetry/api';
2+
import { TraceFlags } from '@opentelemetry/api';
3+
import { baggageHeaderToDynamicSamplingContext } from '@sentry/utils';
4+
import { SENTRY_TRACE_STATE_DSC, SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING } from '../constants';
5+
6+
/**
7+
* OpenTelemetry only knows about SAMPLED or NONE decision,
8+
* but for us it is important to differentiate between unset and unsampled.
9+
*
10+
* Both of these are identified as `traceFlags === TracegFlags.NONE`,
11+
* but we additionally look at a special trace state to differentiate between them.
12+
*/
13+
export function getSamplingDecision(spanContext: SpanContext): boolean | undefined {
14+
const { traceFlags, traceState } = spanContext;
15+
16+
const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
17+
18+
// If trace flag is `SAMPLED`, we interpret this as sampled
19+
// If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
20+
// For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
21+
// to identify which it is
22+
if (traceFlags === TraceFlags.SAMPLED) {
23+
return true;
24+
}
25+
26+
if (sampledNotRecording) {
27+
return false;
28+
}
29+
30+
// Fall back to DSC as a last resort, that may also contain `sampled`...
31+
const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
32+
const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
33+
34+
if (dsc?.sampled === 'true') {
35+
return true;
36+
}
37+
if (dsc?.sampled === 'false') {
38+
return false;
39+
}
40+
41+
return undefined;
42+
}

packages/opentelemetry/test/propagator.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { suppressTracing } from '@opentelemetry/core';
1111
import { addTracingExtensions, withScope } from '@sentry/core';
1212

1313
import { SENTRY_BAGGAGE_HEADER, SENTRY_SCOPES_CONTEXT_KEY, SENTRY_TRACE_HEADER } from '../src/constants';
14-
import { SentryPropagator, getSamplingDecision, makeTraceState } from '../src/propagator';
14+
import { SentryPropagator, makeTraceState } from '../src/propagator';
1515
import { getScopesFromContext } from '../src/utils/contextData';
16+
import { getSamplingDecision } from '../src/utils/getSamplingDecision';
1617
import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit';
1718

1819
beforeAll(() => {

packages/opentelemetry/test/trace.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import {
1818
withScope,
1919
} from '@sentry/core';
2020
import type { Event, Scope } from '@sentry/types';
21-
import { getSamplingDecision, makeTraceState } from '../src/propagator';
21+
import { makeTraceState } from '../src/propagator';
2222

2323
import { continueTrace, startInactiveSpan, startSpan, startSpanManual } from '../src/trace';
2424
import type { AbstractSpan } from '../src/types';
2525
import { getDynamicSamplingContextFromSpan } from '../src/utils/dynamicSamplingContext';
2626
import { getActiveSpan } from '../src/utils/getActiveSpan';
27+
import { getSamplingDecision } from '../src/utils/getSamplingDecision';
2728
import { getSpanKind } from '../src/utils/getSpanKind';
2829
import { spanHasAttributes, spanHasName } from '../src/utils/spanTypes';
2930
import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit';
@@ -307,7 +308,7 @@ describe('trace', () => {
307308
expect(getActiveSpan()).toBe(undefined);
308309
});
309310

310-
it('allows to force a transaction with forceTransaction=true xxx', async () => {
311+
it('allows to force a transaction with forceTransaction=true', async () => {
311312
const client = getClient()!;
312313
const transactionEvents: Event[] = [];
313314

0 commit comments

Comments
 (0)