Skip to content

Commit 6125696

Browse files
author
Luca Forstner
committed
Fix and add tests
1 parent 7b09315 commit 6125696

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

packages/core/test/lib/prepareEvent.test.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,13 @@ describe('prepareEvent', () => {
228228
event_id: expect.any(String),
229229
environment: 'production',
230230
message: 'foo',
231-
sdkProcessingMetadata: {
232-
propagationContext: {
233-
spanId: expect.any(String),
234-
traceId: expect.any(String),
231+
contexts: {
232+
trace: {
233+
span_id: expect.any(String),
234+
trace_id: expect.any(String),
235235
},
236236
},
237+
sdkProcessingMetadata: {},
237238
});
238239
});
239240

@@ -309,16 +310,19 @@ describe('prepareEvent', () => {
309310
user: { id: '1', email: '[email protected]' },
310311
tags: { tag1: 'aa', tag2: 'aa' },
311312
extra: { extra1: 'aa', extra2: 'aa' },
312-
contexts: { os: { name: 'os1' }, culture: { display_name: 'name1' } },
313+
contexts: {
314+
os: { name: 'os1' },
315+
culture: { display_name: 'name1' },
316+
trace: {
317+
span_id: '1',
318+
trace_id: '1',
319+
},
320+
},
313321
fingerprint: ['dd', 'aa'],
314322
breadcrumbs: [breadcrumb4, breadcrumb2, breadcrumb3, breadcrumb1],
315323
sdkProcessingMetadata: {
316324
aa: 'aa',
317325
bb: 'bb',
318-
propagationContext: {
319-
spanId: '1',
320-
traceId: '1',
321-
},
322326
},
323327
});
324328
});
@@ -379,10 +383,15 @@ describe('prepareEvent', () => {
379383
message: 'foo',
380384
fingerprint: ['dd'],
381385
breadcrumbs: [breadcrumb3, breadcrumb1, breadcrumb2],
386+
contexts: {
387+
trace: {
388+
trace_id: isolationScope.getPropagationContext().traceId,
389+
span_id: isolationScope.getPropagationContext().spanId,
390+
},
391+
},
382392
sdkProcessingMetadata: {
383393
aa: 'aa',
384394
bb: 'bb',
385-
propagationContext: isolationScope.getPropagationContext(),
386395
},
387396
});
388397
});

packages/core/test/lib/scope.test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ describe('Scope', () => {
132132

133133
expect(event).toEqual({
134134
message: 'foo',
135-
sdkProcessingMetadata: {
136-
propagationContext: {
137-
spanId: expect.any(String),
138-
traceId: expect.any(String),
135+
contexts: {
136+
trace: {
137+
span_id: expect.any(String),
138+
trace_id: expect.any(String),
139139
},
140140
},
141+
sdkProcessingMetadata: {},
141142
});
142143
});
143144

@@ -166,15 +167,18 @@ describe('Scope', () => {
166167
user: { id: '1', email: '[email protected]' },
167168
tags: { tag1: 'aa', tag2: 'aa' },
168169
extra: { extra1: 'aa', extra2: 'aa' },
169-
contexts: { os: { name: 'os1' }, culture: { display_name: 'name1' } },
170+
contexts: {
171+
os: { name: 'os1' },
172+
culture: { display_name: 'name1' },
173+
trace: {
174+
span_id: '1',
175+
trace_id: '1',
176+
},
177+
},
170178
fingerprint: ['dd', 'aa'],
171179
breadcrumbs: [breadcrumb2, breadcrumb1],
172180
sdkProcessingMetadata: {
173181
aa: 'aa',
174-
propagationContext: {
175-
spanId: '1',
176-
traceId: '1',
177-
},
178182
},
179183
});
180184
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { tracingContextFromHeaders } from '@sentry/utils';
12
import {
23
Hub,
34
SEMANTIC_ATTRIBUTE_SENTRY_OP,
@@ -455,6 +456,7 @@ describe('continueTrace', () => {
455456
const scope = getCurrentScope();
456457

457458
expect(scope.getPropagationContext()).toEqual({
459+
dsc: {}, // DSC should be an empty object (frozen), because there was an incoming trace
458460
sampled: false,
459461
parentSpanId: '1121201211212012',
460462
spanId: expect.any(String),

packages/utils/src/tracing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function tracingContextFromHeaders(
7878

7979
return {
8080
traceparentData,
81-
dynamicSamplingContext,
81+
dynamicSamplingContext: dynamicSamplingContext || {}, // If we have traceparent data but no DSC it means we are not head of trace and we must freeze it,
8282
propagationContext,
8383
};
8484
}

packages/utils/test/tracing.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { tracingContextFromHeaders } from '../src/tracing';
2+
3+
describe('tracingContextFromHeaders()', () => {
4+
it('should produce a frozen baggage (empty object) when there is an incoming trace but no baggage header', () => {
5+
const tracingContext = tracingContextFromHeaders('12312012123120121231201212312012-1121201211212012-1', undefined);
6+
expect(tracingContext.dynamicSamplingContext).toEqual({});
7+
expect(tracingContext.propagationContext.dsc).toEqual({});
8+
});
9+
});

0 commit comments

Comments
 (0)