import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { getCapturedScopesOnSpan, getCurrentScope, getIsolationScope, getMainCarrier, getTraceData, Scope, setAsyncContextStrategy, setCurrentClient, spanToJSON, withScope, } from '../../../src'; import { getAsyncContextStrategy } from '../../../src/asyncContext'; import { continueTrace, getDynamicSamplingContextFromSpan, isTracingSuppressed, registerSpanErrorInstrumentation, SentrySpan, suppressTracing, withActiveSpan, } from '../../../src/tracing'; import { startInactiveSpan, startSpan, startSpanManual } from '../../../src/tracing/trace'; import { SentryNonRecordingSpan } from '../../../src/tracing/sentryNonRecordingSpan'; import { startNewTrace } from '../../../src/tracing/trace'; import type { Event } from '../../../src/types/event'; import type { Span } from '../../../src/types/span'; import { _setSpanForScope } from '../../../src/utils/spanOnScope'; import { getActiveSpan, getRootSpan, getSpanDescendants, spanIsSampled } from '../../../src/utils/spanUtils'; import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; import { SUPPRESS_TRACING_KEY } from '../../../src/tracing/constants'; import { resetGlobals } from '../../testutils'; import { SENTRY_SEGMENT_NAME_SOURCE, SENTRY_OP, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; const enum Type { Sync = 'sync', Async = 'async', } let client: TestClient; describe('startSpan', () => { beforeEach(() => { registerSpanErrorInstrumentation(); resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); afterEach(() => { vi.clearAllMocks(); }); describe.each([ // isSync, isError, callback, expectedReturnValue [Type.Async, false, () => Promise.resolve('async good'), 'async good'], [Type.Sync, false, () => 'sync good', 'sync good'], [Type.Async, true, () => Promise.reject('async bad'), 'async bad'], [ Type.Sync, true, () => { throw 'sync bad'; }, 'sync bad', ], ])('with %s callback and error %s', (_type, isError, callback, expected) => { it('should return the same value as the callback', async () => { try { const result = await startSpan({ name: 'GET users/[id]' }, () => { return callback(); }); expect(result).toEqual(expected); } catch (e) { expect(e).toEqual(expected); } }); it('creates a transaction', async () => { let _span: Span | undefined = undefined; client.on('spanEnd', span => { _span = span; }); try { await startSpan({ name: 'GET users/[id]' }, () => { return callback(); }); } catch { // } expect(_span).toBeDefined(); expect(spanToJSON(_span!).name).toEqual('GET users/[id]'); expect(spanToJSON(_span!).status).toEqual(isError ? 'error' : 'ok'); }); it('allows for transaction to be mutated', async () => { let _span: Span | undefined = undefined; client.on('spanEnd', span => { _span = span; }); try { await startSpan({ name: 'GET users/[id]' }, span => { span.setAttribute(SENTRY_OP, 'http.server'); return callback(); }); } catch { // } expect(spanToJSON(_span!).attributes[SENTRY_OP]).toEqual('http.server'); }); it('creates a span with correct description', async () => { let _span: Span | undefined = undefined; client.on('spanEnd', span => { if (span === getRootSpan(span)) { _span = span; } }); try { await startSpan({ name: 'GET users/[id]' }, () => { return startSpan({ name: 'SELECT * from users' }, () => { return callback(); }); }); } catch { // } expect(_span).toBeDefined(); const spans = getSpanDescendants(_span!); expect(spans).toHaveLength(2); expect(spanToJSON(spans[1]!).name).toEqual('SELECT * from users'); expect(spanToJSON(spans[1]!).parent_span_id).toEqual(_span!.spanContext().spanId); expect(spanToJSON(spans[1]!).status).toEqual(isError ? 'error' : 'ok'); }); it('allows for span to be mutated', async () => { let _span: Span | undefined = undefined; client.on('spanEnd', span => { if (span === getRootSpan(span)) { _span = span; } }); try { await startSpan({ name: 'GET users/[id]' }, () => { return startSpan({ name: 'SELECT * from users' }, childSpan => { childSpan.setAttribute(SENTRY_OP, 'db.query'); return callback(); }); }); } catch { // } expect(_span).toBeDefined(); const spans = getSpanDescendants(_span!); expect(spans).toHaveLength(2); expect(spanToJSON(spans[1]!).attributes[SENTRY_OP]).toEqual('db.query'); }); it('correctly sets the span origin', async () => { let _span: Span | undefined = undefined; client.on('spanEnd', span => { _span = span; }); try { await startSpan( { name: 'GET users/[id]', attributes: { [SENTRY_ORIGIN]: 'auto.http.browser' }, }, () => { return callback(); }, ); } catch { // } expect(_span).toBeDefined(); const jsonSpan = spanToJSON(_span!); expect(jsonSpan).toEqual({ attributes: { 'sentry.origin': 'auto.http.browser', 'sentry.sample_rate': 1, 'sentry.segment.name.source': 'custom', ...(isError && { 'sentry.status.message': 'internal_error' }), }, name: 'GET users/[id]', span_id: expect.stringMatching(/[a-f0-9]{16}/), parent_span_id: undefined, start_timestamp: expect.any(Number), end_timestamp: expect.any(Number), is_segment: true, status: isError ? 'error' : 'ok', links: undefined, trace_id: expect.stringMatching(/[a-f0-9]{32}/), }); }); }); describe('AsyncContext withScope promise integrity behavior', () => { it('preserves custom thenable methods', async () => { const jqXHR = { then: Promise.resolve(1).then.bind(Promise.resolve(1)), abort: vi.fn(), }; expect(jqXHR instanceof Promise).toBe(false); const result = startSpan({ name: 'test' }, () => jqXHR); expect(typeof result.abort).toBe('function'); result.abort(); expect(jqXHR.abort).toHaveBeenCalled(); }); }); it('returns a non recording span if tracing is disabled', () => { const options = getDefaultTestClientOptions({}); client = new TestClient(options); setCurrentClient(client); client.init(); getCurrentScope().setPropagationContext({ traceId: '12345678901234567890123456789012', sampleRand: 0.42, }); let scopeInCallback: Scope | undefined; const span = startSpan({ name: 'GET users/[id]' }, span => { scopeInCallback = getCurrentScope(); return span; }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(span.spanContext().traceId).toBe('12345678901234567890123456789012'); expect(getDynamicSamplingContextFromSpan(span)).toEqual({ environment: 'production', trace_id: expect.stringMatching(/[a-f0-9]{32}/), }); // Scopes are captured on the placeholder so consumers (e.g. SentryTraceProvider) can read them. // `startSpan` forks the scope, so the captured scope is the one active inside the callback. expect(getCapturedScopesOnSpan(span).scope).toBe(scopeInCallback); expect(getCapturedScopesOnSpan(span).isolationScope).toBe(getIsolationScope()); }); it('creates the DSC from the client for a trace continued without baggage when tracing is disabled', () => { const options = getDefaultTestClientOptions({}); client = new TestClient(options); setCurrentClient(client); client.init(); const span = continueTrace( { sentryTrace: '12345678901234567890123456789012-1121201211212012-1', baggage: undefined }, () => startSpan({ name: 'GET users/[id]' }, span => span), ); expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(span.spanContext().traceId).toBe('12345678901234567890123456789012'); expect(getDynamicSamplingContextFromSpan(span)).toEqual({ environment: 'production', trace_id: '12345678901234567890123456789012', }); }); it('does not add a url-source span name to the DSC when tracing is disabled', () => { const options = getDefaultTestClientOptions({}); client = new TestClient(options); setCurrentClient(client); client.init(); const span = startSpan( { name: '/users/123e4567-e89b-12d3-a456-426614174000', attributes: { [SENTRY_SEGMENT_NAME_SOURCE]: 'url' }, }, span => span, ); expect(span).toBeInstanceOf(SentryNonRecordingSpan); // URLs might contain PII, so the span name must not end up in the DSC. expect(getDynamicSamplingContextFromSpan(span)).toEqual({ environment: 'production', trace_id: expect.stringMatching(/[a-f0-9]{32}/), }); }); it('creates & finishes span', async () => { const span = startSpan({ name: 'GET users/[id]' }, span => { expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentrySpan); expect(spanToJSON(span).end_timestamp).toBeUndefined(); return span; }); expect(span).toBeDefined(); expect(spanToJSON(span).end_timestamp).toBeDefined(); }); it('allows to pass a `startTime`', () => { const start = startSpan({ name: 'outer', startTime: [1234, 0] }, span => { return spanToJSON(span).start_timestamp; }); expect(start).toEqual(1234); }); it('forks the scope', () => { const initialScope = getCurrentScope(); startSpan({ name: 'GET users/[id]' }, span => { expect(getCurrentScope()).not.toBe(initialScope); expect(getActiveSpan()).toBe(span); }); expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); it('starts the span on the fork of a passed custom scope', () => { const initialScope = getCurrentScope(); const customScope = initialScope.clone(); customScope.setTag('dogs', 'great'); const parentSpan = new SentrySpan({ spanId: 'parent-span-id', sampled: true }); _setSpanForScope(customScope, parentSpan); startSpan({ name: 'GET users/[id]', scope: customScope }, span => { // current scope is forked from the customScope expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(customScope); expect(getCurrentScope().getScopeData().tags).toEqual({ dogs: 'great' }); // active span is set correctly expect(getActiveSpan()).toBe(span); // span has the correct parent span expect(spanToJSON(span).parent_span_id).toBe('parent-span-id'); // scope data modifications getCurrentScope().setTag('cats', 'great'); customScope.setTag('bears', 'great'); expect(getCurrentScope().getScopeData().tags).toEqual({ dogs: 'great', cats: 'great' }); expect(customScope.getScopeData().tags).toEqual({ dogs: 'great', bears: 'great' }); }); // customScope modifications are persisted expect(customScope.getScopeData().tags).toEqual({ dogs: 'great', bears: 'great' }); // span is parent span again on customScope withScope(customScope, () => { expect(getActiveSpan()).toBe(parentSpan); }); // but activeSpan and currentScope are reset, since customScope was never active expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); describe('handles multiple spans in sequence with a custom scope', () => { it('with parent span', () => { const initialScope = getCurrentScope(); const customScope = initialScope.clone(); const parentSpan = new SentrySpan({ spanId: 'parent-span-id', sampled: true }); _setSpanForScope(customScope, parentSpan); startSpan({ name: 'span 1', scope: customScope }, span1 => { // current scope is forked from the customScope expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(customScope); expect(getActiveSpan()).toBe(span1); expect(spanToJSON(span1).parent_span_id).toBe('parent-span-id'); }); // active span on customScope is reset withScope(customScope, () => { expect(getActiveSpan()).toBe(parentSpan); }); startSpan({ name: 'span 2', scope: customScope }, span2 => { // current scope is forked from the customScope expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(customScope); expect(getActiveSpan()).toBe(span2); // both, span1 and span2 are children of the parent span expect(spanToJSON(span2).parent_span_id).toBe('parent-span-id'); }); withScope(customScope, () => { expect(getActiveSpan()).toBe(parentSpan); }); expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); it('without parent span', () => { const initialScope = getCurrentScope(); const customScope = initialScope.clone(); const traceId = customScope.getPropagationContext()?.traceId; startSpan({ name: 'span 1', scope: customScope }, span1 => { expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(customScope); expect(getActiveSpan()).toBe(span1); expect(getRootSpan(getActiveSpan()!)).toBe(span1); expect(span1.spanContext().traceId).toBe(traceId); }); withScope(customScope, () => { expect(getActiveSpan()).toBe(undefined); }); startSpan({ name: 'span 2', scope: customScope }, span2 => { expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(customScope); expect(getActiveSpan()).toBe(span2); expect(getRootSpan(getActiveSpan()!)).toBe(span2); expect(span2.spanContext().traceId).toBe(traceId); }); withScope(customScope, () => { expect(getActiveSpan()).toBe(undefined); }); expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); }); it('allows to pass a parentSpan', () => { const parentSpan = new SentrySpan({ spanId: 'parent-span-id', sampled: true, name: 'parent-span' }); startSpan({ name: 'GET users/[id]', parentSpan }, span => { expect(getActiveSpan()).toBe(span); expect(spanToJSON(span).parent_span_id).toBe('parent-span-id'); }); expect(getActiveSpan()).toBe(undefined); }); it('allows to pass parentSpan=null', () => { startSpan({ name: 'GET users/[id]' }, () => { startSpan({ name: 'GET users/[id]', parentSpan: null }, span => { expect(spanToJSON(span).parent_span_id).toBe(undefined); }); }); }); it('allows to add span links', () => { const rawSpan1 = startInactiveSpan({ name: 'pageload_span' }); // @ts-expect-error _links exists on span expect(rawSpan1?._links).toEqual(undefined); const span1JSON = spanToJSON(rawSpan1); startSpan({ name: '/users/:id' }, rawSpan2 => { rawSpan2.addLink({ context: rawSpan1.spanContext(), attributes: { 'sentry.link.type': 'previous_trace', }, }); const span2LinkJSON = spanToJSON(rawSpan2).links?.[0]; expect(span2LinkJSON?.attributes?.['sentry.link.type']).toBe('previous_trace'); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2._links?.[0].context.traceId).toEqual(rawSpan1._traceId); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.traceId).toEqual(span1JSON.trace_id); expect(span2LinkJSON?.trace_id).toBe(span1JSON.trace_id); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.spanId).toEqual(rawSpan1?._spanId); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.spanId).toEqual(span1JSON.span_id); expect(span2LinkJSON?.span_id).toBe(span1JSON.span_id); }); }); it('allows to force a transaction with forceTransaction=true', async () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); client = new TestClient(options); setCurrentClient(client); client.init(); const transactionEvents: Event[] = []; client.addEventProcessor(event => { if (event.type === 'transaction') { transactionEvents.push(event); } return event; }); startSpan({ name: 'outer transaction' }, () => { startSpan({ name: 'inner span' }, () => { startSpan({ name: 'inner transaction', forceTransaction: true }, () => { startSpan({ name: 'inner span 2' }, () => { // all good }); }); }); }); await client.flush(); const normalizedTransactionEvents = transactionEvents.map(event => { return { ...event, spans: event.spans?.map(span => ({ name: span.description, id: span.span_id })), sdkProcessingMetadata: { dynamicSamplingContext: event.sdkProcessingMetadata?.dynamicSamplingContext, }, }; }); expect(normalizedTransactionEvents).toHaveLength(2); const outerTransaction = normalizedTransactionEvents.find(event => event.transaction === 'outer transaction'); const innerTransaction = normalizedTransactionEvents.find(event => event.transaction === 'inner transaction'); const outerTraceId = outerTransaction?.contexts?.trace?.trace_id; // The inner transaction should be a child of the last span of the outer transaction const innerParentSpanId = outerTransaction?.spans?.[0]?.id; const innerSpanId = innerTransaction?.contexts?.trace?.span_id; expect(outerTraceId).toBeDefined(); expect(innerParentSpanId).toBeDefined(); expect(innerSpanId).toBeDefined(); // inner span ID should _not_ be the parent span ID, but the id of the new span expect(innerSpanId).not.toEqual(innerParentSpanId); expect(outerTransaction?.contexts).toEqual({ trace: { data: { 'sentry.segment.name.source': 'custom', 'sentry.sample_rate': 1, 'sentry.origin': 'manual', }, span_id: expect.stringMatching(/[a-f0-9]{16}/), trace_id: expect.stringMatching(/[a-f0-9]{32}/), origin: 'manual', status: 'ok', }, }); expect(outerTransaction?.spans).toEqual([{ name: 'inner span', id: expect.any(String) }]); expect(outerTransaction?.transaction).toEqual('outer transaction'); expect(outerTransaction?.sdkProcessingMetadata).toEqual({ dynamicSamplingContext: { environment: 'production', trace_id: outerTraceId, sample_rate: '1', transaction: 'outer transaction', sampled: 'true', sample_rand: expect.any(String), }, }); expect(innerTransaction?.contexts).toEqual({ trace: { data: { 'sentry.segment.name.source': 'custom', 'sentry.origin': 'manual', }, parent_span_id: innerParentSpanId, span_id: expect.stringMatching(/[a-f0-9]{16}/), trace_id: outerTraceId, origin: 'manual', status: 'ok', }, }); expect(innerTransaction?.spans).toEqual([{ name: 'inner span 2', id: expect.any(String) }]); expect(innerTransaction?.transaction).toEqual('inner transaction'); expect(innerTransaction?.sdkProcessingMetadata).toEqual({ dynamicSamplingContext: { environment: 'production', trace_id: outerTraceId, sample_rate: '1', transaction: 'outer transaction', sampled: 'true', sample_rand: expect.any(String), }, }); }); it("picks up the trace id off the parent scope's propagation context", () => { expect.assertions(1); withScope(scope => { scope.setPropagationContext({ traceId: '99999999999999999999999999999999', sampleRand: Math.random(), dsc: {}, parentSpanId: '4242424242424242', }); startSpan({ name: 'span' }, span => { expect(span.spanContext().traceId).toBe('99999999999999999999999999999999'); }); }); }); describe('onlyIfParent', () => { it('starts a non recording span and records no_parent_span client report if there is no parent', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span = startSpan({ name: 'test span', onlyIfParent: true }, span => { return span; }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(spyOnDroppedEvent).toHaveBeenCalledWith('no_parent_span', 'span'); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(1); }); it('records no_parent_span client reports for spans nested in a span without parent', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'test span', onlyIfParent: true }, () => { startSpan({ name: 'child span' }, () => { startInactiveSpan({ name: 'grandchild span' }).end(); }); }); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(3); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(1, 'no_parent_span', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(2, 'no_parent_span', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(3, 'no_parent_span', 'span'); }); it('creates a span if there is a parent', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span = startSpan({ name: 'parent span' }, () => { const span = startSpan({ name: 'test span', onlyIfParent: true }, span => { return span; }); return span; }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentrySpan); expect(spyOnDroppedEvent).not.toHaveBeenCalledWith('no_parent_span', 'span'); }); it('does not record no_parent_span client report when onlyIfParent is not set', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'root span without onlyIfParent' }, span => { return span; }); expect(spyOnDroppedEvent).not.toHaveBeenCalledWith('no_parent_span', 'span'); }); }); describe('parentSpanIsAlwaysRootSpan', () => { it('creates a span as child of root span if parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'parent span' }, span => { expect(spanToJSON(span).parent_span_id).toBe(undefined); startSpan({ name: 'child span' }, childSpan => { expect(spanToJSON(childSpan).parent_span_id).toBe(span.spanContext().spanId); startSpan({ name: 'grand child span' }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(span.spanContext().spanId); }); }); }); }); it('does not creates a span as child of root span if parentSpanIsAlwaysRootSpan=false', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: false, }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'parent span' }, span => { expect(spanToJSON(span).parent_span_id).toBe(undefined); startSpan({ name: 'child span' }, childSpan => { expect(spanToJSON(childSpan).parent_span_id).toBe(span.spanContext().spanId); startSpan({ name: 'grand child span' }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(childSpan.spanContext().spanId); }); }); }); }); it('explicit parentSpan takes precedence over parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); const parentSpan = startInactiveSpan({ name: 'parent span' }); startSpan({ name: 'parent span' }, () => { startSpan({ name: 'child span' }, () => { startSpan({ name: 'grand child span', parentSpan }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(parentSpan.spanContext().spanId); }); }); }); }); it('explicit parentSpan=null takes precedence over parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'parent span' }, () => { startSpan({ name: 'child span' }, () => { startSpan({ name: 'grand child span', parentSpan: null }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(undefined); }); }); }); }); }); describe('uses tracesSampler if defined', () => { const tracesSampler = vi.fn<() => boolean | number>(() => { return true; }); it.each([true, 1])('returns a positive sampling decision if tracesSampler returns %s', tracesSamplerResult => { tracesSampler.mockReturnValueOnce(tracesSamplerResult); const options = getDefaultTestClientOptions({ tracesSampler }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, outerSpan => { expect(outerSpan).toBeDefined(); expect(spanIsSampled(outerSpan)).toBe(true); }); expect(tracesSampler).toBeCalledTimes(1); expect(tracesSampler).toHaveBeenLastCalledWith({ parentSampled: undefined, name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb', }, inheritOrSampleWith: expect.any(Function), }); }); it.each([false, 0])('returns a negative sampling decision if tracesSampler returns %s', tracesSamplerResult => { tracesSampler.mockReturnValueOnce(tracesSamplerResult); const options = getDefaultTestClientOptions({ tracesSampler }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'outer' }, outerSpan => { expect(outerSpan).toBeDefined(); expect(spanIsSampled(outerSpan)).toBe(false); }); expect(tracesSampler).toBeCalledTimes(1); expect(tracesSampler).toHaveBeenLastCalledWith({ parentSampled: undefined, attributes: {}, name: 'outer', inheritOrSampleWith: expect.any(Function), }); }); it('passes normalizedRequest from the isolation scope to the sampling context', () => { const options = getDefaultTestClientOptions({ tracesSampler }); client = new TestClient(options); setCurrentClient(client); client.init(); const normalizedRequest = { url: '/test?query=123', method: 'GET', query_string: 'query=123' }; getIsolationScope().setSDKProcessingMetadata({ normalizedRequest }); startSpan({ name: 'outer' }, () => {}); expect(tracesSampler).toHaveBeenLastCalledWith(expect.objectContaining({ normalizedRequest })); }); }); it('includes the scope at the time the span was started when finished', async () => { const beforeSendTransaction = vi.fn(event => event); const client = new TestClient( getDefaultTestClientOptions({ dsn: 'https://username@domain/123', tracesSampleRate: 1, beforeSendTransaction, }), ); setCurrentClient(client); client.init(); withScope(scope1 => { scope1.setTag('scope', 1); startSpanManual({ name: 'my-span' }, span => { withScope(scope2 => { scope2.setTag('scope', 2); span.end(); }); }); }); await client.flush(); expect(beforeSendTransaction).toHaveBeenCalledTimes(1); expect(beforeSendTransaction).toHaveBeenCalledWith( expect.objectContaining({ tags: expect.objectContaining({ scope: 1, }), }), expect.anything(), ); }); it('sets a child span reference on the parent span', () => { expect.assertions(1); startSpan({ name: 'outer' }, (outerSpan: any) => { startSpan({ name: 'inner' }, innerSpan => { const childSpans = Array.from(outerSpan._sentryChildSpans); expect(childSpans).toContain(innerSpan); }); }); }); }); describe('startSpanManual', () => { beforeEach(() => { registerSpanErrorInstrumentation(); resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); it('returns a non recording span if tracing is disabled', () => { const options = getDefaultTestClientOptions({}); client = new TestClient(options); setCurrentClient(client); client.init(); let scopeInCallback: Scope | undefined; const span = startSpanManual({ name: 'GET users/[id]' }, span => { scopeInCallback = getCurrentScope(); return span; }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(getDynamicSamplingContextFromSpan(span)).toEqual({ environment: 'production', trace_id: expect.stringMatching(/[a-f0-9]{32}/), }); // Scopes are captured on the placeholder so consumers (e.g. SentryTraceProvider) can read them. // `startSpanManual` forks the scope, so the captured scope is the one active inside the callback. expect(getCapturedScopesOnSpan(span).scope).toBe(scopeInCallback); expect(getCapturedScopesOnSpan(span).isolationScope).toBe(getIsolationScope()); }); it('creates & finishes span', async () => { startSpanManual({ name: 'GET users/[id]' }, (span, finish) => { expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentrySpan); expect(spanToJSON(span).end_timestamp).toBeUndefined(); finish(); expect(spanToJSON(span).end_timestamp).toBeDefined(); }); }); it('forks the scope automatically', () => { const initialScope = getCurrentScope(); startSpanManual({ name: 'GET users/[id]' }, (span, finish) => { expect(getCurrentScope()).not.toBe(initialScope); expect(getActiveSpan()).toBe(span); finish(); // Is still the active span expect(getActiveSpan()).toBe(span); }); expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); describe('starts a span on the fork of a custom scope if passed', () => { it('with parent span', () => { const initialScope = getCurrentScope(); const customScope = initialScope.clone(); customScope.setTag('dogs', 'great'); const parentSpan = new SentrySpan({ spanId: 'parent-span-id', sampled: true }); _setSpanForScope(customScope, parentSpan); startSpanManual({ name: 'GET users/[id]', scope: customScope }, span => { // current scope is forked from the customScope expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(customScope); expect(spanToJSON(span).parent_span_id).toBe('parent-span-id'); // span is active span expect(getActiveSpan()).toBe(span); span.end(); // span is still the active span (weird but it is what it is) expect(getActiveSpan()).toBe(span); getCurrentScope().setTag('cats', 'great'); customScope.setTag('bears', 'great'); expect(getCurrentScope().getScopeData().tags).toEqual({ dogs: 'great', cats: 'great' }); expect(customScope.getScopeData().tags).toEqual({ dogs: 'great', bears: 'great' }); }); expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); startSpanManual({ name: 'POST users/[id]', scope: customScope }, (span, finish) => { // current scope is forked from the customScope expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(customScope); expect(spanToJSON(span).parent_span_id).toBe('parent-span-id'); // scope data modification from customScope in previous callback is persisted expect(getCurrentScope().getScopeData().tags).toEqual({ dogs: 'great', bears: 'great' }); // span is active span expect(getActiveSpan()).toBe(span); // calling finish() or span.end() has the same effect finish(); // using finish() resets the scope correctly expect(getActiveSpan()).toBe(span); }); expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); it('without parent span', () => { const initialScope = getCurrentScope(); const manualScope = initialScope.clone(); startSpanManual({ name: 'GET users/[id]', scope: manualScope }, span => { // current scope is forked from the customScope expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(manualScope); expect(getCurrentScope()).toEqual(manualScope); // span is active span and a root span expect(getActiveSpan()).toBe(span); expect(getRootSpan(span)).toBe(span); span.end(); expect(getActiveSpan()).toBe(span); }); startSpanManual({ name: 'POST users/[id]', scope: manualScope }, (span, finish) => { expect(getCurrentScope()).not.toBe(initialScope); expect(getCurrentScope()).not.toBe(manualScope); expect(getCurrentScope()).toEqual(manualScope); // second span is active span and its own root span expect(getActiveSpan()).toBe(span); expect(getRootSpan(span)).toBe(span); finish(); // calling finish() or span.end() has the same effect expect(getActiveSpan()).toBe(span); }); expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); }); it('allows to pass a parentSpan', () => { const parentSpan = new SentrySpan({ spanId: 'parent-span-id', sampled: true, name: 'parent-span' }); startSpanManual({ name: 'GET users/[id]', parentSpan }, span => { expect(getActiveSpan()).toBe(span); expect(spanToJSON(span).parent_span_id).toBe('parent-span-id'); span.end(); }); expect(getActiveSpan()).toBe(undefined); }); it('allows to pass parentSpan=null', () => { startSpan({ name: 'GET users/[id]' }, () => { startSpanManual({ name: 'child', parentSpan: null }, span => { expect(spanToJSON(span).parent_span_id).toBe(undefined); span.end(); }); }); }); it('allows to add span links', () => { const rawSpan1 = startInactiveSpan({ name: 'pageload_span' }); // @ts-expect-error _links exists on span expect(rawSpan1?._links).toEqual(undefined); const span1JSON = spanToJSON(rawSpan1); startSpanManual({ name: '/users/:id' }, rawSpan2 => { rawSpan2.addLink({ context: rawSpan1.spanContext(), attributes: { 'sentry.link.type': 'previous_trace', }, }); const span2LinkJSON = spanToJSON(rawSpan2).links?.[0]; expect(span2LinkJSON?.attributes?.['sentry.link.type']).toBe('previous_trace'); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.traceId).toEqual(rawSpan1._traceId); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.traceId).toEqual(span1JSON.trace_id); expect(span2LinkJSON?.trace_id).toBe(span1JSON.trace_id); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.spanId).toEqual(rawSpan1?._spanId); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.spanId).toEqual(span1JSON.span_id); expect(span2LinkJSON?.span_id).toBe(span1JSON.span_id); }); }); it('allows to force a transaction with forceTransaction=true', async () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); client = new TestClient(options); setCurrentClient(client); client.init(); const transactionEvents: Event[] = []; client.addEventProcessor(event => { if (event.type === 'transaction') { transactionEvents.push(event); } return event; }); startSpanManual({ name: 'outer transaction' }, span => { startSpanManual({ name: 'inner span' }, span => { startSpanManual({ name: 'inner transaction', forceTransaction: true }, span => { startSpanManual({ name: 'inner span 2' }, span => { // all good span.end(); }); span.end(); }); span.end(); }); span.end(); }); await client.flush(); const normalizedTransactionEvents = transactionEvents.map(event => { return { ...event, spans: event.spans?.map(span => ({ name: span.description, id: span.span_id })), sdkProcessingMetadata: { dynamicSamplingContext: event.sdkProcessingMetadata?.dynamicSamplingContext, }, }; }); expect(normalizedTransactionEvents).toHaveLength(2); const outerTransaction = normalizedTransactionEvents.find(event => event.transaction === 'outer transaction'); const innerTransaction = normalizedTransactionEvents.find(event => event.transaction === 'inner transaction'); const outerTraceId = outerTransaction?.contexts?.trace?.trace_id; // The inner transaction should be a child of the last span of the outer transaction const innerParentSpanId = outerTransaction?.spans?.[0]?.id; const innerSpanId = innerTransaction?.contexts?.trace?.span_id; expect(outerTraceId).toBeDefined(); expect(innerParentSpanId).toBeDefined(); expect(innerSpanId).toBeDefined(); // inner span ID should _not_ be the parent span ID, but the id of the new span expect(innerSpanId).not.toEqual(innerParentSpanId); expect(outerTransaction?.contexts).toEqual({ trace: { data: { 'sentry.segment.name.source': 'custom', 'sentry.sample_rate': 1, 'sentry.origin': 'manual', }, span_id: expect.stringMatching(/[a-f0-9]{16}/), trace_id: expect.stringMatching(/[a-f0-9]{32}/), origin: 'manual', status: 'ok', }, }); expect(outerTransaction?.spans).toEqual([{ name: 'inner span', id: expect.any(String) }]); expect(outerTransaction?.transaction).toEqual('outer transaction'); expect(outerTransaction?.sdkProcessingMetadata).toEqual({ dynamicSamplingContext: { environment: 'production', trace_id: outerTraceId, sample_rate: '1', transaction: 'outer transaction', sampled: 'true', sample_rand: expect.any(String), }, }); expect(innerTransaction?.contexts).toEqual({ trace: { data: { 'sentry.segment.name.source': 'custom', 'sentry.origin': 'manual', }, parent_span_id: innerParentSpanId, span_id: expect.stringMatching(/[a-f0-9]{16}/), trace_id: outerTraceId, origin: 'manual', status: 'ok', }, }); expect(innerTransaction?.spans).toEqual([{ name: 'inner span 2', id: expect.any(String) }]); expect(innerTransaction?.transaction).toEqual('inner transaction'); expect(innerTransaction?.sdkProcessingMetadata).toEqual({ dynamicSamplingContext: { environment: 'production', trace_id: outerTraceId, sample_rate: '1', transaction: 'outer transaction', sampled: 'true', sample_rand: expect.any(String), }, }); }); it('allows to pass a `startTime`', () => { const start = startSpanManual({ name: 'outer', startTime: [1234, 0] }, span => { span.end(); return spanToJSON(span).start_timestamp; }); expect(start).toEqual(1234); }); it("picks up the trace id off the parent scope's propagation context", () => { expect.assertions(1); withScope(scope => { scope.setPropagationContext({ traceId: '99999999999999999999999999999991', sampleRand: Math.random(), dsc: {}, parentSpanId: '4242424242424242', }); startSpanManual({ name: 'span' }, span => { expect(span.spanContext().traceId).toBe('99999999999999999999999999999991'); span.end(); }); }); }); describe('onlyIfParent', () => { it('does not create a span and records no_parent_span client report if there is no parent', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span = startSpanManual({ name: 'test span', onlyIfParent: true }, span => { return span; }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(spyOnDroppedEvent).toHaveBeenCalledWith('no_parent_span', 'span'); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(1); }); it('creates a span if there is a parent', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span = startSpan({ name: 'parent span' }, () => { const span = startSpanManual({ name: 'test span', onlyIfParent: true }, span => { return span; }); return span; }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentrySpan); expect(spyOnDroppedEvent).not.toHaveBeenCalledWith('no_parent_span', 'span'); }); it('does not record no_parent_span client report when onlyIfParent is not set', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpanManual({ name: 'root span without onlyIfParent' }, span => { span.end(); return span; }); expect(spyOnDroppedEvent).not.toHaveBeenCalledWith('no_parent_span', 'span'); }); }); describe('parentSpanIsAlwaysRootSpan', () => { it('creates a span as child of root span if parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpanManual({ name: 'parent span' }, span => { expect(spanToJSON(span).parent_span_id).toBe(undefined); startSpanManual({ name: 'child span' }, childSpan => { expect(spanToJSON(childSpan).parent_span_id).toBe(span.spanContext().spanId); startSpanManual({ name: 'grand child span' }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(span.spanContext().spanId); grandChildSpan.end(); }); childSpan.end(); }); span.end(); }); }); it('does not creates a span as child of root span if parentSpanIsAlwaysRootSpan=false', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: false, }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpanManual({ name: 'parent span' }, span => { expect(spanToJSON(span).parent_span_id).toBe(undefined); startSpanManual({ name: 'child span' }, childSpan => { expect(spanToJSON(childSpan).parent_span_id).toBe(span.spanContext().spanId); startSpanManual({ name: 'grand child span' }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(childSpan.spanContext().spanId); grandChildSpan.end(); }); childSpan.end(); }); span.end(); }); }); it('explicit parentSpan takes precedence over parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); const parentSpan = startInactiveSpan({ name: 'parent span' }); startSpan({ name: 'parent span' }, () => { startSpan({ name: 'child span' }, () => { startSpanManual({ name: 'grand child span', parentSpan }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(parentSpan.spanContext().spanId); grandChildSpan.end(); }); }); }); }); it('explicit parentSpan=null takes precedence over parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'parent span' }, () => { startSpan({ name: 'child span' }, () => { startSpanManual({ name: 'grand child span', parentSpan: null }, grandChildSpan => { expect(spanToJSON(grandChildSpan).parent_span_id).toBe(undefined); grandChildSpan.end(); }); }); }); }); }); it('sets a child span reference on the parent span', () => { expect.assertions(1); startSpan({ name: 'outer' }, (outerSpan: any) => { startSpanManual({ name: 'inner' }, innerSpan => { const childSpans = Array.from(outerSpan._sentryChildSpans); expect(childSpans).toContain(innerSpan); }); }); }); }); describe('startInactiveSpan', () => { beforeEach(() => { registerSpanErrorInstrumentation(); resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); it('includes the scope the span was started on when finished', async () => { const beforeSendTransaction = vi.fn(event => event); const options = getDefaultTestClientOptions({ tracesSampleRate: 1, beforeSendTransaction }); client = new TestClient(options); setCurrentClient(client); client.init(); let span: Span | undefined; withScope(scope => { scope.setTag('scope', 1); span = startInactiveSpan({ name: 'my-span' }); // The span captures the scope it was started on, so later mutations of that scope // are reflected on the transaction. scope.setTag('scope_after_span', 2); }); withScope(scope => { scope.setTag('scope', 2); span?.end(); }); await client.flush(); expect(beforeSendTransaction).toHaveBeenCalledTimes(1); expect(beforeSendTransaction).toHaveBeenCalledWith( expect.objectContaining({ // The span-start scope is captured, including `scope_after_span` (set on the same scope // after span start), but not `scope: 2` (a different scope active at `end()`). tags: { scope: 1, scope_after_span: 2 }, }), expect.anything(), ); }); it('returns a non recording span if tracing is disabled', () => { const options = getDefaultTestClientOptions({}); client = new TestClient(options); setCurrentClient(client); client.init(); const span = startInactiveSpan({ name: 'GET users/[id]' }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(getDynamicSamplingContextFromSpan(span)).toEqual({ environment: 'production', trace_id: expect.stringMatching(/[a-f0-9]{32}/), }); // Scopes are captured on the placeholder so consumers (e.g. SentryTraceProvider) can read them. expect(getCapturedScopesOnSpan(span).scope).toBe(getCurrentScope()); expect(getCapturedScopesOnSpan(span).isolationScope).toBe(getIsolationScope()); }); it('creates & finishes span', async () => { const span = startInactiveSpan({ name: 'GET users/[id]' }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentrySpan); expect(spanToJSON(span).end_timestamp).toBeUndefined(); span.end(); expect(spanToJSON(span).end_timestamp).toBeDefined(); }); it('does not set span on scope', () => { const span = startInactiveSpan({ name: 'GET users/[id]' }); expect(span).toBeDefined(); expect(getActiveSpan()).toBeUndefined(); span.end(); expect(getActiveSpan()).toBeUndefined(); }); // A root span reads its trace from the current scope only. The isolation // scope's propagation context must not contribute: mixing the two yields // a span whose `parent_span_id` and frozen DSC describe a different trace // than its own `trace_id`. it('ignores the propagation context on the isolation scope', () => { const isolationTraceId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; getIsolationScope().setPropagationContext({ traceId: isolationTraceId, sampleRand: 0.1, parentSpanId: 'bbbbbbbbbbbbbbbb', sampled: true, dsc: { trace_id: isolationTraceId, transaction: 'from-isolation-scope' }, }); const currentTraceId = getCurrentScope().getPropagationContext().traceId; const span = startInactiveSpan({ name: 'GET users/[id]' }); expect(spanToJSON(span).trace_id).toBe(currentTraceId); expect(spanToJSON(span).parent_span_id).toBeUndefined(); expect(getDynamicSamplingContextFromSpan(span)).toEqual( expect.objectContaining({ trace_id: currentTraceId, transaction: 'GET users/[id]' }), ); }); it('allows to pass a scope', () => { const initialScope = getCurrentScope(); const manualScope = initialScope.clone(); const parentSpan = new SentrySpan({ spanId: 'parent-span-id', sampled: true }); _setSpanForScope(manualScope, parentSpan); const span = startInactiveSpan({ name: 'GET users/[id]', scope: manualScope }); expect(span).toBeDefined(); expect(spanToJSON(span).parent_span_id).toBe('parent-span-id'); expect(getActiveSpan()).toBeUndefined(); span.end(); expect(getActiveSpan()).toBeUndefined(); }); it('allows to pass a parentSpan', () => { const parentSpan = new SentrySpan({ spanId: 'parent-span-id', sampled: true, name: 'parent-span' }); const span = startInactiveSpan({ name: 'GET users/[id]', parentSpan }); expect(spanToJSON(span).parent_span_id).toBe('parent-span-id'); expect(getActiveSpan()).toBe(undefined); span.end(); expect(getActiveSpan()).toBeUndefined(); }); it('allows to pass parentSpan=null', () => { startSpan({ name: 'outer' }, () => { const span = startInactiveSpan({ name: 'GET users/[id]', parentSpan: null }); expect(spanToJSON(span).parent_span_id).toBe(undefined); span.end(); }); }); it('allows to pass span links in span options', () => { const rawSpan1 = startInactiveSpan({ name: 'pageload_span' }); // @ts-expect-error _links exists on span expect(rawSpan1?._links).toEqual(undefined); const rawSpan2 = startInactiveSpan({ name: 'GET users/[id]', links: [ { context: rawSpan1.spanContext(), attributes: { 'sentry.link.type': 'previous_trace' }, }, ], }); const span1JSON = spanToJSON(rawSpan1); const span2JSON = spanToJSON(rawSpan2); const span2LinkJSON = span2JSON.links?.[0]; expect(span2LinkJSON?.attributes?.['sentry.link.type']).toBe('previous_trace'); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.traceId).toEqual(rawSpan1._traceId); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.traceId).toEqual(span1JSON.trace_id); expect(span2LinkJSON?.trace_id).toBe(span1JSON.trace_id); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.spanId).toEqual(rawSpan1?._spanId); // @ts-expect-error _links and _traceId exist on SentrySpan expect(rawSpan2?._links?.[0].context.spanId).toEqual(span1JSON.span_id); expect(span2LinkJSON?.span_id).toBe(span1JSON.span_id); // sampling decision is inherited expect(span2LinkJSON?.sampled).toBe(Boolean(spanToJSON(rawSpan1).attributes['sentry.sample_rate'])); }); it('allows to force a transaction with forceTransaction=true', async () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); client = new TestClient(options); setCurrentClient(client); client.init(); const transactionEvents: Event[] = []; client.addEventProcessor(event => { if (event.type === 'transaction') { transactionEvents.push(event); } return event; }); startSpan({ name: 'outer transaction' }, () => { startSpan({ name: 'inner span' }, () => { const innerTransaction = startInactiveSpan({ name: 'inner transaction', forceTransaction: true }); innerTransaction.end(); }); }); await client.flush(); const normalizedTransactionEvents = transactionEvents.map(event => { return { ...event, spans: event.spans?.map(span => ({ name: span.description, id: span.span_id })), sdkProcessingMetadata: { dynamicSamplingContext: event.sdkProcessingMetadata?.dynamicSamplingContext, }, }; }); expect(normalizedTransactionEvents).toHaveLength(2); const outerTransaction = normalizedTransactionEvents.find(event => event.transaction === 'outer transaction'); const innerTransaction = normalizedTransactionEvents.find(event => event.transaction === 'inner transaction'); const outerTraceId = outerTransaction?.contexts?.trace?.trace_id; // The inner transaction should be a child of the last span of the outer transaction const innerParentSpanId = outerTransaction?.spans?.[0]?.id; const innerSpanId = innerTransaction?.contexts?.trace?.span_id; expect(outerTraceId).toBeDefined(); expect(innerParentSpanId).toBeDefined(); expect(innerSpanId).toBeDefined(); // inner span ID should _not_ be the parent span ID, but the id of the new span expect(innerSpanId).not.toEqual(innerParentSpanId); expect(outerTransaction?.contexts).toEqual({ trace: { data: { 'sentry.segment.name.source': 'custom', 'sentry.sample_rate': 1, 'sentry.origin': 'manual', }, span_id: expect.stringMatching(/[a-f0-9]{16}/), trace_id: expect.stringMatching(/[a-f0-9]{32}/), origin: 'manual', status: 'ok', }, }); expect(outerTransaction?.spans).toEqual([{ name: 'inner span', id: expect.any(String) }]); expect(outerTransaction?.transaction).toEqual('outer transaction'); expect(outerTransaction?.sdkProcessingMetadata).toEqual({ dynamicSamplingContext: { environment: 'production', trace_id: outerTraceId, sample_rate: '1', transaction: 'outer transaction', sampled: 'true', sample_rand: expect.any(String), }, }); expect(innerTransaction?.contexts).toEqual({ trace: { data: { 'sentry.segment.name.source': 'custom', 'sentry.origin': 'manual', }, parent_span_id: innerParentSpanId, span_id: expect.stringMatching(/[a-f0-9]{16}/), trace_id: outerTraceId, origin: 'manual', status: 'ok', }, }); expect(innerTransaction?.spans).toEqual([]); expect(innerTransaction?.transaction).toEqual('inner transaction'); expect(innerTransaction?.sdkProcessingMetadata).toEqual({ dynamicSamplingContext: { environment: 'production', trace_id: outerTraceId, sample_rate: '1', transaction: 'outer transaction', sampled: 'true', sample_rand: expect.any(String), }, }); }); it('allows to pass a `startTime`', () => { const span = startInactiveSpan({ name: 'outer', startTime: [1234, 0] }); expect(spanToJSON(span).start_timestamp).toEqual(1234); }); it("picks up the trace id off the parent scope's propagation context", () => { expect.assertions(1); withScope(scope => { scope.setPropagationContext({ traceId: '99999999999999999999999999999991', sampleRand: Math.random(), dsc: {}, parentSpanId: '4242424242424242', }); const span = startInactiveSpan({ name: 'span' }); expect(span.spanContext().traceId).toBe('99999999999999999999999999999991'); span.end(); }); }); describe('onlyIfParent', () => { it('does not create a span and records no_parent_span client report if there is no parent', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span = startInactiveSpan({ name: 'test span', onlyIfParent: true }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(spyOnDroppedEvent).toHaveBeenCalledWith('no_parent_span', 'span'); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(1); }); it('creates a span if there is a parent', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span = startSpan({ name: 'parent span' }, () => { const span = startInactiveSpan({ name: 'test span', onlyIfParent: true }); return span; }); expect(span).toBeDefined(); expect(span).toBeInstanceOf(SentrySpan); expect(spyOnDroppedEvent).not.toHaveBeenCalledWith('no_parent_span', 'span'); }); it('does not record no_parent_span client report when onlyIfParent is not set', () => { const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span = startInactiveSpan({ name: 'root span without onlyIfParent' }); span.end(); expect(spyOnDroppedEvent).not.toHaveBeenCalledWith('no_parent_span', 'span'); }); }); describe('parentSpanIsAlwaysRootSpan', () => { it('creates a span as child of root span if parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(undefined); startSpan({ name: 'parent span' }, span => { const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(span.spanContext().spanId); startSpan({ name: 'child span' }, () => { const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(span.spanContext().spanId); startSpan({ name: 'grand child span' }, () => { const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(span.spanContext().spanId); }); }); }); }); it('does not creates a span as child of root span if parentSpanIsAlwaysRootSpan=false', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: false, }); client = new TestClient(options); setCurrentClient(client); client.init(); const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(undefined); startSpan({ name: 'parent span' }, span => { const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(span.spanContext().spanId); startSpan({ name: 'child span' }, childSpan => { const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(childSpan.spanContext().spanId); startSpan({ name: 'grand child span' }, grandChildSpan => { const inactiveSpan = startInactiveSpan({ name: 'inactive span' }); expect(spanToJSON(inactiveSpan).parent_span_id).toBe(grandChildSpan.spanContext().spanId); }); }); }); }); it('explicit parentSpan takes precedence over parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); const parentSpan = startInactiveSpan({ name: 'parent span' }); startSpan({ name: 'parent span' }, () => { startSpan({ name: 'child span' }, () => { const grandChildSpan = startInactiveSpan({ name: 'grand child span', parentSpan }); expect(spanToJSON(grandChildSpan).parent_span_id).toBe(parentSpan.spanContext().spanId); grandChildSpan.end(); }); }); }); it('explicit parentSpan=null takes precedence over parentSpanIsAlwaysRootSpan=true', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, parentSpanIsAlwaysRootSpan: true, }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'parent span' }, () => { startSpan({ name: 'child span' }, () => { const grandChildSpan = startInactiveSpan({ name: 'grand child span', parentSpan: null }); expect(spanToJSON(grandChildSpan).parent_span_id).toBe(undefined); grandChildSpan.end(); }); }); }); }); it('includes the scope at the time the span was started when finished', async () => { const beforeSendTransaction = vi.fn(event => event); const client = new TestClient( getDefaultTestClientOptions({ dsn: 'https://username@domain/123', tracesSampleRate: 1, beforeSendTransaction, }), ); setCurrentClient(client); client.init(); let span: Span; const scope = getCurrentScope(); scope.setTag('outer', 'foo'); withScope(scope => { scope.setTag('scope', 1); span = startInactiveSpan({ name: 'my-span' }); scope.setTag('scope_after_span', 2); }); withScope(scope => { scope.setTag('scope', 2); span.end(); }); await client.flush(); expect(beforeSendTransaction).toHaveBeenCalledTimes(1); expect(beforeSendTransaction).toHaveBeenCalledWith( expect.objectContaining({ tags: expect.objectContaining({ outer: 'foo', scope: 1, scope_after_span: 2, }), }), expect.anything(), ); }); it('sets a child span reference on the parent span', () => { expect.assertions(1); startSpan({ name: 'outer' }, (outerSpan: any) => { const innerSpan = startInactiveSpan({ name: 'inner' }); const childSpans = Array.from(outerSpan._sentryChildSpans); expect(childSpans).toContain(innerSpan); }); }); }); describe('continueTrace', () => { beforeEach(() => { resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); it('works without trace & baggage data', () => { const scope = continueTrace({ sentryTrace: undefined, baggage: undefined }, () => { return getCurrentScope(); }); expect(scope.getPropagationContext()).toEqual({ sampled: undefined, traceId: expect.any(String), sampleRand: expect.any(Number), }); expect(scope.getScopeData().sdkProcessingMetadata).toEqual({}); }); it('works with trace data', () => { const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-0', baggage: undefined, }, () => { return getCurrentScope(); }, ); expect(scope.getPropagationContext()).toEqual({ dsc: undefined, // DSC is not frozen, because there was no incoming DSC sampled: false, parentSpanId: '1121201211212012', traceId: '12312012123120121231201212312012', sampleRand: expect.any(Number), }); expect(scope.getScopeData().sdkProcessingMetadata).toEqual({}); }); it('works with trace & baggage data', () => { const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-version=1.0,sentry-environment=production', }, () => { return getCurrentScope(); }, ); expect(scope.getPropagationContext()).toEqual({ dsc: { environment: 'production', version: '1.0', sample_rand: expect.any(String), }, sampled: true, parentSpanId: '1121201211212012', traceId: '12312012123120121231201212312012', sampleRand: expect.any(Number), }); expect(scope.getScopeData().sdkProcessingMetadata).toEqual({}); }); it('works with trace & 3rd party baggage data', () => { const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-version=1.0,sentry-environment=production,dogs=great,cats=boring', }, () => { return getCurrentScope(); }, ); expect(scope.getPropagationContext()).toEqual({ dsc: { environment: 'production', version: '1.0', sample_rand: expect.any(String), }, sampled: true, parentSpanId: '1121201211212012', traceId: '12312012123120121231201212312012', sampleRand: expect.any(Number), }); expect(scope.getScopeData().sdkProcessingMetadata).toEqual({}); }); it('returns response of callback', () => { const result = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-0', baggage: undefined, }, () => { return 'aha'; }, ); expect(result).toEqual('aha'); }); describe('strictTraceContinuation', () => { const creatOrgIdInDsn = (orgId: number) => { vi.spyOn(client, 'getDsn').mockReturnValue({ host: `o${orgId}.ingest.sentry.io`, protocol: 'https', projectId: 'projId', }); }; afterEach(() => { vi.clearAllMocks(); }); it('continues trace when org IDs match', () => { creatOrgIdInDsn(123); const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-org_id=123', }, () => { return getCurrentScope(); }, ); expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); }); it('starts new trace when both SDK and baggage org IDs are set and do not match', () => { creatOrgIdInDsn(123); const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-org_id=456', }, () => { return getCurrentScope(); }, ); // Should start a new trace with a different trace ID expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); }); describe('when strictTraceContinuation is true', () => { it('starts new trace when baggage org ID is missing', () => { client.getOptions().strictTraceContinuation = true; creatOrgIdInDsn(123); const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-environment=production', }, () => { return getCurrentScope(); }, ); // Should start a new trace with a different trace ID expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); }); it('starts new trace when SDK org ID is missing', () => { client.getOptions().strictTraceContinuation = true; const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-org_id=123', }, () => { return getCurrentScope(); }, ); // Should start a new trace with a different trace ID expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); }); it('continues trace when both org IDs are missing', () => { client.getOptions().strictTraceContinuation = true; const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-environment=production', }, () => { return getCurrentScope(); }, ); // Should continue the trace expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); }); }); describe('when strictTraceContinuation is false', () => { it('continues trace when baggage org ID is missing', () => { client.getOptions().strictTraceContinuation = false; creatOrgIdInDsn(123); const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-environment=production', }, () => { return getCurrentScope(); }, ); expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); }); it('SDK org ID is missing', () => { client.getOptions().strictTraceContinuation = false; const scope = continueTrace( { sentryTrace: '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-org_id=123', }, () => { return getCurrentScope(); }, ); expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); }); }); }); it('updates the propagation context when called inside an active span', () => { const client = new TestClient( getDefaultTestClientOptions({ dsn: 'https://username@domain/123', tracesSampleRate: 1, }), ); setCurrentClient(client); client.init(); const sentryTrace = '12312012123120121231201212312012-1121201211212012-1'; const sentryTraceId = '12312012123120121231201212312012'; const sentryBaggage = 'sentry-org_id=123'; startSpan({ name: 'outer' }, () => { continueTrace( { sentryTrace: sentryTrace, baggage: sentryBaggage, }, () => { const traceDataInContinuedTrace = getTraceData(); const traceIdInContinuedTrace = traceDataInContinuedTrace['sentry-trace']?.split('-')[0]; const traceIdInCurrentScope = getCurrentScope().getPropagationContext().traceId; expect(getActiveSpan()).toBeUndefined(); expect(traceIdInContinuedTrace).toBe(sentryTraceId); expect(traceIdInCurrentScope).toBe(sentryTraceId); }, ); }); }); it('sets the correct trace and parent span ids when called inside an active span and a new span is started from within the callback', () => { const client = new TestClient( getDefaultTestClientOptions({ dsn: 'https://username@domain/123', tracesSampleRate: 1, }), ); setCurrentClient(client); client.init(); const sentryTrace = '12312012123120121231201212312012-1121201211212012-1'; const sentryTraceId = '12312012123120121231201212312012'; const sentrySpanId = '1121201211212012'; const sentryBaggage = 'sentry-org_id=123'; startSpan({ name: 'outer' }, () => { continueTrace( { sentryTrace: sentryTrace, baggage: sentryBaggage, }, () => { startSpan({ name: 'inner' }, span => { const innerSpanJson = spanToJSON(span); const innerTraceId = innerSpanJson.trace_id; const innerParentSpanId = innerSpanJson.parent_span_id; expect(innerTraceId).toBe(sentryTraceId); expect(innerParentSpanId).toBe(sentrySpanId); }); }, ); }); }); }); describe('getActiveSpan', () => { beforeEach(() => { resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); it('works without an active span on the scope', () => { const span = getActiveSpan(); expect(span).toBeUndefined(); }); it('works with an active span on the scope', () => { const activeSpan = new SentrySpan({ spanId: 'aha', sampled: true }); withActiveSpan(activeSpan, () => { const span = getActiveSpan(); expect(span).toBe(activeSpan); }); }); it('uses implementation from ACS, if it exists', () => { const staticSpan = new SentrySpan({ spanId: 'aha', sampled: true }); const carrier = getMainCarrier(); const customFn = vi.fn(() => { return staticSpan; }) as typeof getActiveSpan; const acs = { ...getAsyncContextStrategy(carrier), getActiveSpan: customFn, }; setAsyncContextStrategy(acs); const result = getActiveSpan(); expect(result).toBe(staticSpan); }); it('handles active span when passing scopes to withScope', () => { const [scope, span] = startSpan({ name: 'outer' }, span => { return [getCurrentScope(), span]; }); const spanOnScope = withScope(scope, () => { return getActiveSpan(); }); expect(spanOnScope).toBeDefined(); expect(spanOnScope).toBe(span); }); }); describe('withActiveSpan()', () => { beforeEach(() => { resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1 }); const client = new TestClient(options); setCurrentClient(client); client.init(); }); it('should set the active span within the callback', () => { expect.assertions(2); const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); expect(getActiveSpan()).not.toBe(inactiveSpan); withActiveSpan(inactiveSpan, () => { expect(getActiveSpan()).toBe(inactiveSpan); }); }); it('should create child spans when calling startSpan within the callback', () => { const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); const parentSpanId = withActiveSpan(inactiveSpan, () => { return startSpan({ name: 'child-span' }, childSpan => { return spanToJSON(childSpan).parent_span_id; }); }); expect(parentSpanId).toBe(inactiveSpan.spanContext().spanId); }); it('when `null` is passed, no span should be active within the callback', () => { expect.assertions(1); startSpan({ name: 'parent-span' }, () => { withActiveSpan(null, () => { expect(getActiveSpan()).toBeUndefined(); }); }); }); it('uses implementation from ACS, if it exists', () => { const staticSpan = new SentrySpan({ spanId: 'aha', sampled: true }); const staticScope = new Scope(); const carrier = getMainCarrier(); const customFn = vi.fn((_span: Span | null, callback: (scope: Scope) => string) => { callback(staticScope); return 'aha'; }) as typeof withActiveSpan; const acs = { ...getAsyncContextStrategy(carrier), withActiveSpan: customFn, }; setAsyncContextStrategy(acs); const result = withActiveSpan(staticSpan, scope => { expect(scope).toBe(staticScope); return 'oho'; }); expect(result).toBe('aha'); }); }); describe('span hooks', () => { beforeEach(() => { resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); afterEach(() => { vi.clearAllMocks(); }); it('correctly emits span hooks', () => { const startedSpans: string[] = []; const endedSpans: string[] = []; client.on('spanStart', span => { startedSpans.push(spanToJSON(span).name); }); client.on('spanEnd', span => { endedSpans.push(spanToJSON(span).name); }); startSpan({ name: 'span1' }, () => { startSpan({ name: 'span2' }, () => { const span = startInactiveSpan({ name: 'span3' }); startSpanManual({ name: 'span5' }, span => { startInactiveSpan({ name: 'span4' }); span.end(); }); span.end(); }); }); expect(startedSpans).toHaveLength(5); expect(endedSpans).toHaveLength(4); expect(startedSpans).toEqual(['span1', 'span2', 'span3', 'span5', 'span4']); expect(endedSpans).toEqual(['span5', 'span3', 'span2', 'span1']); }); it('captures scopes on a root span before the spanStart event fires', () => { let scopeAtSpanStart: Scope | undefined; let isolationScopeAtSpanStart: Scope | undefined; client.on('spanStart', span => { scopeAtSpanStart = getCapturedScopesOnSpan(span).scope; isolationScopeAtSpanStart = getCapturedScopesOnSpan(span).isolationScope; }); startInactiveSpan({ name: 'root span' }); expect(scopeAtSpanStart).toBe(getCurrentScope()); expect(isolationScopeAtSpanStart).toBe(getIsolationScope()); }); it('captures scopes on a child span before the spanStart event fires', () => { let scopeAtSpanStart: Scope | undefined; let isolationScopeAtSpanStart: Scope | undefined; client.on('spanStart', span => { scopeAtSpanStart = getCapturedScopesOnSpan(span).scope; isolationScopeAtSpanStart = getCapturedScopesOnSpan(span).isolationScope; }); startSpan({ name: 'parent span' }, () => { startInactiveSpan({ name: 'child span' }); expect(scopeAtSpanStart).toBe(getCurrentScope()); expect(isolationScopeAtSpanStart).toBe(getIsolationScope()); }); }); }); describe('suppressTracing', () => { beforeEach(() => { resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); afterEach(() => { vi.clearAllMocks(); }); it('suppresses a root span', () => { const span = suppressTracing(() => { return startInactiveSpan({ name: 'span' }); }); expect(span.isRecording()).toBe(false); expect(spanIsSampled(span)).toBe(false); }); it('suppresses a child span', () => { startSpan({ name: 'outer' }, span => { expect(span.isRecording()).toBe(true); expect(spanIsSampled(span)).toBe(true); const child1 = startInactiveSpan({ name: 'inner1' }); expect(child1.isRecording()).toBe(true); expect(spanIsSampled(child1)).toBe(true); const child2 = suppressTracing(() => { return startInactiveSpan({ name: 'span' }); }); expect(child2.isRecording()).toBe(false); expect(spanIsSampled(child2)).toBe(false); }); }); it('suppresses a child span with forceTransaction=true', () => { startSpan({ name: 'outer' }, span => { expect(span.isRecording()).toBe(true); expect(spanIsSampled(span)).toBe(true); const child = suppressTracing(() => { return startInactiveSpan({ name: 'span', forceTransaction: true }); }); expect(child.isRecording()).toBe(false); expect(spanIsSampled(child)).toBe(false); }); }); it("doesn't record a client outcome for suppressed spans", () => { resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream' }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); const span1 = suppressTracing(() => { return startInactiveSpan({ name: 'span' }); }); expect(span1.isRecording()).toBe(false); expect(spanIsSampled(span1)).toBe(false); startSpan({ name: 'outer' }, span => { expect(span.isRecording()).toBe(true); expect(spanIsSampled(span)).toBe(true); const child = suppressTracing(() => { return startInactiveSpan({ name: 'span' }); }); expect(child.isRecording()).toBe(false); expect(spanIsSampled(child)).toBe(false); }); expect(spyOnDroppedEvent).not.toHaveBeenCalled(); }); it('works with parallel processes', async () => { const span = suppressTracing(() => { return startInactiveSpan({ name: 'span' }); }); // Note: This is unintuitive, but it is the expected behavior // because we only suppress tracing synchronously in the browser const span2Promise = suppressTracing(async () => { await new Promise(resolve => setTimeout(resolve, 100)); return startInactiveSpan({ name: 'span2' }); }); const span3Promise = suppressTracing(async () => { const span = startInactiveSpan({ name: 'span3' }); await new Promise(resolve => setTimeout(resolve, 100)); return span; }); const span4 = suppressTracing(() => { return startInactiveSpan({ name: 'span' }); }); const span5 = startInactiveSpan({ name: 'span5' }); const span2 = await span2Promise; const span3 = await span3Promise; expect(spanIsSampled(span)).toBe(false); expect(spanIsSampled(span2)).toBe(true); expect(spanIsSampled(span3)).toBe(false); expect(spanIsSampled(span4)).toBe(false); expect(spanIsSampled(span5)).toBe(true); }); }); describe('isTracingSuppressed', () => { beforeEach(() => { resetGlobals(); setAsyncContextStrategy(undefined); const options = getDefaultTestClientOptions({ tracesSampleRate: 1 }); client = new TestClient(options); setCurrentClient(client); client.init(); }); afterEach(() => { vi.clearAllMocks(); }); it('returns false when tracing is not suppressed', () => { expect(isTracingSuppressed()).toBe(false); }); it('returns true while inside suppressTracing', () => { const suppressed = suppressTracing(() => isTracingSuppressed()); expect(suppressed).toBe(true); }); it('returns false again after suppressTracing has finished', () => { suppressTracing(() => { expect(isTracingSuppressed()).toBe(true); }); expect(isTracingSuppressed()).toBe(false); }); it('only suppresses tracing within the active scope', () => { withScope(() => { const suppressed = suppressTracing(() => isTracingSuppressed()); expect(suppressed).toBe(true); }); // Outside of the suppressed scope, tracing is no longer suppressed expect(isTracingSuppressed()).toBe(false); }); it('respects a scope passed in explicitly', () => { const scope = getCurrentScope().clone(); scope.setSDKProcessingMetadata({ [SUPPRESS_TRACING_KEY]: true }); expect(isTracingSuppressed(scope)).toBe(true); expect(isTracingSuppressed()).toBe(false); }); }); describe('startNewTrace', () => { beforeEach(() => { resetGlobals(); }); it('creates a new propagation context on the current scope', () => { const oldCurrentScopeItraceId = getCurrentScope().getPropagationContext().traceId; startNewTrace(() => { const newCurrentScopeItraceId = getCurrentScope().getPropagationContext().traceId; expect(newCurrentScopeItraceId).toMatch(/^[a-f0-9]{32}$/); expect(newCurrentScopeItraceId).not.toEqual(oldCurrentScopeItraceId); }); }); it('keeps the propagation context on the isolation scope as-is', () => { const oldIsolationScopeTraceId = getIsolationScope().getPropagationContext().traceId; startNewTrace(() => { const newIsolationScopeTraceId = getIsolationScope().getPropagationContext().traceId; expect(newIsolationScopeTraceId).toMatch(/^[a-f0-9]{32}$/); expect(newIsolationScopeTraceId).toEqual(oldIsolationScopeTraceId); }); }); }); describe('ignoreSpans (core path, streaming)', () => { beforeEach(() => { registerSpanErrorInstrumentation(); resetGlobals(); setAsyncContextStrategy(undefined); }); afterEach(() => { vi.clearAllMocks(); }); it('returns SentryNonRecordingSpan for root span matching ignoreSpans', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['GET /health'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'GET /health' }, span => { expect(span).toBeInstanceOf(SentryNonRecordingSpan); expect(spanIsSampled(span)).toBe(false); startSpan({ name: 'child' }, childSpan => { expect(childSpan).toBeInstanceOf(SentryNonRecordingSpan); expect(spanIsSampled(childSpan)).toBe(false); }); }); expect(spyOnDroppedEvent).toHaveBeenCalledWith('ignored', 'span'); }); it('returns SentryNonRecordingSpan for child span matching ignoreSpans', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored-child'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'root' }, rootSpan => { startSpan({ name: 'ignored-child' }, ignoredSpan1 => { expect(ignoredSpan1).toBeInstanceOf(SentryNonRecordingSpan); expect(getRootSpan(ignoredSpan1)).toBe(rootSpan); startSpan({ name: 'ignored-child' }, ignoredSpan2 => { expect(ignoredSpan2).toBeInstanceOf(SentryNonRecordingSpan); // since ignoredSpan1 is not active, ignoredSpan2 also has root as its parent expect(getRootSpan(ignoredSpan2)).toBe(rootSpan); }); }); }); expect(spyOnDroppedEvent).toHaveBeenCalledWith('ignored', 'span'); }); it('children of ignored child spans parent to grandparent', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored-span'], }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'root' }, rootSpan => { startSpan({ name: 'ignored-span' }, () => { expect(getActiveSpan()).toBe(rootSpan); startSpan({ name: 'grandchild' }, grandchildSpan => { const json = spanToJSON(grandchildSpan); expect(json.parent_span_id).toBe(rootSpan.spanContext().spanId); }); }); }); }); it('does not ignore non-matching spans', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['GET /health'], }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'GET /users' }, span => { expect(span).toBeInstanceOf(SentrySpan); expect(spanIsSampled(span)).toBe(true); }); }); it('returns SentryNonRecordingSpan for startInactiveSpan matching ignoreSpans', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored-span'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const span = startInactiveSpan({ name: 'ignored-span' }); expect(span).toBeInstanceOf(SentryNonRecordingSpan); }); it('does not apply ignoreSpans on the static (non-streaming) path', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, ignoreSpans: ['GET /health'], }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'GET /health' }, span => { expect(span).toBeInstanceOf(SentrySpan); expect(spanIsSampled(span)).toBe(true); }); }); it('records ignored outcome for root span and all child spans', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['GET /health'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'GET /health' }, () => { startSpan({ name: 'db.query' }, () => { startSpan({ name: 'cache.lookup' }, () => {}); }); }); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(3); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(1, 'ignored', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(2, 'ignored', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(3, 'ignored', 'span'); }); it('records sample_rate outcome for unsampled root span and all child spans when streaming', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 0, traceLifecycle: 'stream', }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'GET /foo' }, () => { startSpan({ name: 'db.query' }, () => {}); }); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(2); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(1, 'sample_rate', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(2, 'sample_rate', 'span'); }); it('records sample_rate outcomes for the transaction and all of its spans on static path', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 0, }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'GET /foo' }, () => { startSpan({ name: 'db.query' }, () => { startSpan({ name: 'cache.lookup' }, () => {}); }); startInactiveSpan({ name: 'http.client' }).end(); }); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(5); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(1, 'sample_rate', 'transaction'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(2, 'sample_rate', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(3, 'sample_rate', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(4, 'sample_rate', 'span'); expect(spyOnDroppedEvent).toHaveBeenNthCalledWith(5, 'sample_rate', 'span'); }); it('records a single sample_rate/span outcome for an unsampled standalone root span on static path', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 0, }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); // oxlint-disable-next-line typescript/no-deprecated startInactiveSpan({ name: 'inp', experimental: { standalone: true } }).end(); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(1); expect(spyOnDroppedEvent).toHaveBeenCalledWith('sample_rate', 'span'); }); // Unsampled spans are counted when they start, so this also counts spans a sampled // transaction would never contain (here: a child that never ends). it('records sample_rate outcomes for unsampled spans that a sampled transaction would not contain', () => { const runScenario = (tracesSampleRate: number): ReturnType => { client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate })); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'GET /foo' }, () => { startInactiveSpan({ name: 'never ended' }); }); return spyOnDroppedEvent; }; const sampledSpy = vi.spyOn(TestClient.prototype, 'sendEvent'); runScenario(1); expect(sampledSpy).toHaveBeenCalledWith(expect.objectContaining({ spans: [] }), expect.anything()); sampledSpy.mockRestore(); const unsampledDroppedEventSpy = runScenario(0); expect(unsampledDroppedEventSpy).toHaveBeenCalledTimes(3); expect(unsampledDroppedEventSpy).toHaveBeenNthCalledWith(1, 'sample_rate', 'transaction'); expect(unsampledDroppedEventSpy).toHaveBeenNthCalledWith(2, 'sample_rate', 'span'); expect(unsampledDroppedEventSpy).toHaveBeenNthCalledWith(3, 'sample_rate', 'span'); }); it('records only one ignored outcome for directly ignored child span', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored-child'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); startSpan({ name: 'root' }, () => { startSpan({ name: 'ignored-child' }, () => {}); startSpan({ name: 'normal-child' }, normalChild => { expect(normalChild).toBeInstanceOf(SentrySpan); }); }); expect(spyOnDroppedEvent).toHaveBeenCalledTimes(1); expect(spyOnDroppedEvent).toHaveBeenCalledWith('ignored', 'span'); }); it('sets ignored segment span onto the scope', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored'], }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'ignored-segment' }, ignoredSegmentSpan => { expect(ignoredSegmentSpan).toBeInstanceOf(SentryNonRecordingSpan); expect(getActiveSpan()).toBe(ignoredSegmentSpan); startSpan({ name: 'child' }, () => { expect(getActiveSpan()).toBe(ignoredSegmentSpan); }); }); }); it("doesn't set ignored child span onto the scope", () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored'], }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'segment' }, segmentSpan => { expect(getActiveSpan()).toBe(segmentSpan); startSpan({ name: 'ignored-child' }, () => { expect(getActiveSpan()).toBe(segmentSpan); startSpan({ name: 'ignored-child-2' }, () => { expect(getActiveSpan()).toBe(segmentSpan); startSpan({ name: 'normal-child-2' }, normalChild2Span => { expect(getActiveSpan()).toBe(normalChild2Span); }); }); startSpan({ name: 'normal-child' }, normalChildSpan => { expect(getActiveSpan()).toBe(normalChildSpan); }); }); }); }); it("assigns the parent span's trace id to the ignored segment span", () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored'], }); client = new TestClient(options); setCurrentClient(client); client.init(); startSpan({ name: 'ignored-segment' }, ignoredSegmentSpan => { startSpan({ name: 'child' }, childSpan => { expect(childSpan.spanContext().traceId).toBe(ignoredSegmentSpan.spanContext().traceId); }); }); }); it("doesn't set inactive ignored segment spans onto the scope", () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const span = startInactiveSpan({ name: 'ignored-segment' }); expect(getActiveSpan()).toBeUndefined(); span.end(); expect(getActiveSpan()).toBeUndefined(); }); it("doesn't record a client outcome for a suppressed and ignored span", () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const spyOnDroppedEvent = vi.spyOn(client, 'recordDroppedEvent'); suppressTracing(() => { startInactiveSpan({ name: 'ignored-inactive-span' }); startSpan({ name: 'ignored-active-span' }, () => {}); startSpanManual({ name: 'ignored-manual-span' }, () => {}); }); expect(spyOnDroppedEvent).not.toHaveBeenCalled(); }); it('sets the propagation context trace on ignored segment spans', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored'], }); client = new TestClient(options); setCurrentClient(client); client.init(); getCurrentScope().setPropagationContext({ traceId: 'abc', propagationSpanId: 'xxx', sampleRand: 0.5 }); const span = startInactiveSpan({ name: 'ignored-segment' }); expect(span.spanContext().traceId).toBe(getCurrentScope().getPropagationContext().traceId); expect(span.spanContext().traceId).toBe('abc'); }); it('captures scopes on an ignored streamed span so its DSC can be resolved from the scope', () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream', ignoreSpans: ['ignored'], }); client = new TestClient(options); setCurrentClient(client); client.init(); const span = startInactiveSpan({ name: 'ignored' }); expect(getCapturedScopesOnSpan(span).scope).toBe(getCurrentScope()); expect(getCapturedScopesOnSpan(span).isolationScope).toBe(getIsolationScope()); }); });