Skip to content

Commit 943df92

Browse files
authored
feat(core): Add DSC to all outgoing envelopes (#7820)
1 parent 3efb556 commit 943df92

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

packages/core/src/scope.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,16 @@ export class Scope implements ScopeInterface {
482482
// errors with transaction and it relies on that.
483483
if (this._span) {
484484
event.contexts = { trace: this._span.getTraceContext(), ...event.contexts };
485-
const transactionName = this._span.transaction && this._span.transaction.name;
486-
if (transactionName) {
487-
event.tags = { transaction: transactionName, ...event.tags };
485+
const transaction = this._span.transaction;
486+
if (transaction) {
487+
event.sdkProcessingMetadata = {
488+
dynamicSamplingContext: transaction.getDynamicSamplingContext(),
489+
...event.sdkProcessingMetadata,
490+
};
491+
const transactionName = transaction.name;
492+
if (transactionName) {
493+
event.tags = { transaction: transactionName, ...event.tags };
494+
}
488495
}
489496
}
490497

packages/core/test/lib/envelope.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ const testDsn: DsnComponents = { protocol: 'https', projectId: 'abc', host: 'tes
66

77
describe('createEventEnvelope', () => {
88
describe('trace header', () => {
9-
it("doesn't add trace header if event is not a transaction", () => {
10-
const event: Event = {};
11-
const envelopeHeaders = createEventEnvelope(event, testDsn)[0];
12-
13-
expect(envelopeHeaders).toBeDefined();
14-
expect(envelopeHeaders.trace).toBeUndefined();
15-
});
16-
179
const testTable: Array<[string, Event, DynamicSamplingContext]> = [
1810
[
1911
'adds minimal baggage items',
@@ -66,6 +58,15 @@ describe('createEventEnvelope', () => {
6658
trace_id: '1234',
6759
},
6860
],
61+
[
62+
'with error event',
63+
{
64+
sdkProcessingMetadata: {
65+
dynamicSamplingContext: { trace_id: '1234', public_key: 'pubKey123' },
66+
},
67+
},
68+
{ trace_id: '1234', public_key: 'pubKey123' },
69+
],
6970
];
7071
it.each(testTable)('%s', (_: string, event, trace) => {
7172
const envelopeHeaders = createEventEnvelope(event, testDsn)[0];

packages/hub/test/scope.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ describe('Scope', () => {
354354
fake: 'span',
355355
getTraceContext: () => ({ a: 'b' }),
356356
name: 'fake transaction',
357+
getDynamicSamplingContext: () => ({}),
357358
} as any;
358359
transaction.transaction = transaction; // because this is a transaction, its `transaction` pointer points to itself
359360
scope.setSpan(transaction);
@@ -366,7 +367,7 @@ describe('Scope', () => {
366367
test('adds `transaction` tag when span on scope', async () => {
367368
expect.assertions(1);
368369
const scope = new Scope();
369-
const transaction = { name: 'fake transaction' };
370+
const transaction = { name: 'fake transaction', getDynamicSamplingContext: () => ({}) };
370371
const span = {
371372
fake: 'span',
372373
getTraceContext: () => ({ a: 'b' }),

packages/utils/src/envelope.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,13 @@ export function createEventEnvelopeHeaders(
237237
dsn: DsnComponents,
238238
): EventEnvelopeHeaders {
239239
const dynamicSamplingContext = event.sdkProcessingMetadata && event.sdkProcessingMetadata.dynamicSamplingContext;
240-
241240
return {
242241
event_id: event.event_id as string,
243242
sent_at: new Date().toISOString(),
244243
...(sdkInfo && { sdk: sdkInfo }),
245244
...(!!tunnel && { dsn: dsnToString(dsn) }),
246-
...(event.type === 'transaction' &&
247-
dynamicSamplingContext && {
248-
trace: dropUndefinedKeys({ ...dynamicSamplingContext }),
249-
}),
245+
...(dynamicSamplingContext && {
246+
trace: dropUndefinedKeys({ ...dynamicSamplingContext }),
247+
}),
250248
};
251249
}

0 commit comments

Comments
 (0)