|
| 1 | +import type { Client } from '@sentry/types'; |
| 2 | +import { Scope, SentrySpan, getCurrentScope, getGlobalScope, getIsolationScope, setCurrentClient } from '../../../src'; |
| 3 | +import { freezeDscOnSpan } from '../../../src/tracing/dynamicSamplingContext'; |
| 4 | +import { getSentryHeaders } from '../../../src/tracing/sentryHeaders'; |
| 5 | +import type { TestClientOptions } from '../../mocks/client'; |
| 6 | +import { TestClient, getDefaultTestClientOptions } from '../../mocks/client'; |
| 7 | + |
| 8 | +const dsn = 'https://[email protected]/42'; |
| 9 | + |
| 10 | +const SCOPE_TRACE_ID = '12345678901234567890123456789012'; |
| 11 | +const SCOPE_SPAN_ID = '1234567890123456'; |
| 12 | + |
| 13 | +function setupClient(opts?: TestClientOptions): Client { |
| 14 | + getCurrentScope().clear(); |
| 15 | + getIsolationScope().clear(); |
| 16 | + getGlobalScope().clear(); |
| 17 | + |
| 18 | + getCurrentScope().setPropagationContext({ |
| 19 | + traceId: SCOPE_TRACE_ID, |
| 20 | + spanId: SCOPE_SPAN_ID, |
| 21 | + }); |
| 22 | + |
| 23 | + const options = getDefaultTestClientOptions({ |
| 24 | + dsn, |
| 25 | + ...opts, |
| 26 | + }); |
| 27 | + const client = new TestClient(options); |
| 28 | + setCurrentClient(client); |
| 29 | + client.init(); |
| 30 | + |
| 31 | + return client; |
| 32 | +} |
| 33 | + |
| 34 | +describe('getSentryHeaders', () => { |
| 35 | + beforeEach(() => {}); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('works with a minimal client', () => { |
| 42 | + const client = setupClient(); |
| 43 | + |
| 44 | + const { sentryTrace, baggage } = getSentryHeaders({ client }); |
| 45 | + |
| 46 | + expect(sentryTrace).toEqual(`${SCOPE_TRACE_ID}-${SCOPE_SPAN_ID}`); |
| 47 | + expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${SCOPE_TRACE_ID}`); |
| 48 | + }); |
| 49 | + |
| 50 | + it('allows to pass a specific scope', () => { |
| 51 | + const client = setupClient(); |
| 52 | + |
| 53 | + const traceId = '12345678901234567890123456789099'; |
| 54 | + const spanId = '1234567890123499'; |
| 55 | + const scope = new Scope(); |
| 56 | + scope.setPropagationContext({ |
| 57 | + traceId, |
| 58 | + spanId, |
| 59 | + }); |
| 60 | + |
| 61 | + const { sentryTrace, baggage } = getSentryHeaders({ client, scope }); |
| 62 | + |
| 63 | + expect(sentryTrace).toEqual(`${traceId}-${spanId}`); |
| 64 | + expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`); |
| 65 | + }); |
| 66 | + |
| 67 | + it('uses DSC from scope, if available', () => { |
| 68 | + const client = setupClient(); |
| 69 | + |
| 70 | + const traceId = '12345678901234567890123456789099'; |
| 71 | + const spanId = '1234567890123499'; |
| 72 | + const scope = new Scope(); |
| 73 | + scope.setPropagationContext({ |
| 74 | + traceId, |
| 75 | + spanId, |
| 76 | + dsc: { |
| 77 | + environment: 'test-dev', |
| 78 | + public_key: '456', |
| 79 | + trace_id: '12345678901234567890123456789088', |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const { sentryTrace, baggage } = getSentryHeaders({ client, scope }); |
| 84 | + |
| 85 | + expect(sentryTrace).toEqual(`${traceId}-${spanId}`); |
| 86 | + expect(baggage).toEqual( |
| 87 | + 'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088', |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('works with a minimal unsampled span', () => { |
| 92 | + const client = setupClient(); |
| 93 | + |
| 94 | + const traceId = '12345678901234567890123456789099'; |
| 95 | + const spanId = '1234567890123499'; |
| 96 | + |
| 97 | + const span = new SentrySpan({ |
| 98 | + traceId, |
| 99 | + spanId, |
| 100 | + sampled: false, |
| 101 | + }); |
| 102 | + |
| 103 | + const { sentryTrace, baggage } = getSentryHeaders({ client, span }); |
| 104 | + |
| 105 | + expect(sentryTrace).toEqual(`${traceId}-${spanId}-0`); |
| 106 | + expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`); |
| 107 | + }); |
| 108 | + |
| 109 | + it('works with a minimal sampled span', () => { |
| 110 | + const client = setupClient(); |
| 111 | + |
| 112 | + const traceId = '12345678901234567890123456789099'; |
| 113 | + const spanId = '1234567890123499'; |
| 114 | + |
| 115 | + const span = new SentrySpan({ |
| 116 | + traceId, |
| 117 | + spanId, |
| 118 | + sampled: true, |
| 119 | + }); |
| 120 | + |
| 121 | + const { sentryTrace, baggage } = getSentryHeaders({ client, span }); |
| 122 | + |
| 123 | + expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`); |
| 124 | + expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`); |
| 125 | + }); |
| 126 | + |
| 127 | + it('works with a SentrySpan with frozen DSC', () => { |
| 128 | + const client = setupClient(); |
| 129 | + |
| 130 | + const traceId = '12345678901234567890123456789099'; |
| 131 | + const spanId = '1234567890123499'; |
| 132 | + |
| 133 | + const span = new SentrySpan({ |
| 134 | + traceId, |
| 135 | + spanId, |
| 136 | + sampled: true, |
| 137 | + }); |
| 138 | + |
| 139 | + freezeDscOnSpan(span, { |
| 140 | + environment: 'test-dev', |
| 141 | + public_key: '456', |
| 142 | + trace_id: '12345678901234567890123456789088', |
| 143 | + }); |
| 144 | + |
| 145 | + const { sentryTrace, baggage } = getSentryHeaders({ client, span }); |
| 146 | + |
| 147 | + expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`); |
| 148 | + expect(baggage).toEqual( |
| 149 | + 'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088', |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('works with an OTEL span with frozen DSC in traceState', () => { |
| 154 | + const client = setupClient(); |
| 155 | + |
| 156 | + const traceId = '12345678901234567890123456789099'; |
| 157 | + const spanId = '1234567890123499'; |
| 158 | + |
| 159 | + const span = new SentrySpan({ |
| 160 | + traceId, |
| 161 | + spanId, |
| 162 | + sampled: true, |
| 163 | + }); |
| 164 | + |
| 165 | + span.spanContext = () => { |
| 166 | + const traceState = { |
| 167 | + set: () => traceState, |
| 168 | + unset: () => traceState, |
| 169 | + get: (key: string) => { |
| 170 | + if (key === 'sentry.dsc') { |
| 171 | + return 'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088'; |
| 172 | + } |
| 173 | + return undefined; |
| 174 | + }, |
| 175 | + serialize: () => '', |
| 176 | + }; |
| 177 | + |
| 178 | + return { |
| 179 | + traceId, |
| 180 | + spanId, |
| 181 | + sampled: true, |
| 182 | + traceFlags: 1, |
| 183 | + traceState, |
| 184 | + }; |
| 185 | + }; |
| 186 | + |
| 187 | + const { sentryTrace, baggage } = getSentryHeaders({ client, span }); |
| 188 | + |
| 189 | + expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`); |
| 190 | + expect(baggage).toEqual( |
| 191 | + 'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088', |
| 192 | + ); |
| 193 | + }); |
| 194 | +}); |
0 commit comments