Skip to content

Commit

Permalink
feat(core): Add DSC to all outgoing envelopes (#7820)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad authored Apr 12, 2023
1 parent 3efb556 commit 943df92
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
13 changes: 10 additions & 3 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,16 @@ export class Scope implements ScopeInterface {
// errors with transaction and it relies on that.
if (this._span) {
event.contexts = { trace: this._span.getTraceContext(), ...event.contexts };
const transactionName = this._span.transaction && this._span.transaction.name;
if (transactionName) {
event.tags = { transaction: transactionName, ...event.tags };
const transaction = this._span.transaction;
if (transaction) {
event.sdkProcessingMetadata = {
dynamicSamplingContext: transaction.getDynamicSamplingContext(),
...event.sdkProcessingMetadata,
};
const transactionName = transaction.name;
if (transactionName) {
event.tags = { transaction: transactionName, ...event.tags };
}
}
}

Expand Down
17 changes: 9 additions & 8 deletions packages/core/test/lib/envelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ const testDsn: DsnComponents = { protocol: 'https', projectId: 'abc', host: 'tes

describe('createEventEnvelope', () => {
describe('trace header', () => {
it("doesn't add trace header if event is not a transaction", () => {
const event: Event = {};
const envelopeHeaders = createEventEnvelope(event, testDsn)[0];

expect(envelopeHeaders).toBeDefined();
expect(envelopeHeaders.trace).toBeUndefined();
});

const testTable: Array<[string, Event, DynamicSamplingContext]> = [
[
'adds minimal baggage items',
Expand Down Expand Up @@ -66,6 +58,15 @@ describe('createEventEnvelope', () => {
trace_id: '1234',
},
],
[
'with error event',
{
sdkProcessingMetadata: {
dynamicSamplingContext: { trace_id: '1234', public_key: 'pubKey123' },
},
},
{ trace_id: '1234', public_key: 'pubKey123' },
],
];
it.each(testTable)('%s', (_: string, event, trace) => {
const envelopeHeaders = createEventEnvelope(event, testDsn)[0];
Expand Down
3 changes: 2 additions & 1 deletion packages/hub/test/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ describe('Scope', () => {
fake: 'span',
getTraceContext: () => ({ a: 'b' }),
name: 'fake transaction',
getDynamicSamplingContext: () => ({}),
} as any;
transaction.transaction = transaction; // because this is a transaction, its `transaction` pointer points to itself
scope.setSpan(transaction);
Expand All @@ -366,7 +367,7 @@ describe('Scope', () => {
test('adds `transaction` tag when span on scope', async () => {
expect.assertions(1);
const scope = new Scope();
const transaction = { name: 'fake transaction' };
const transaction = { name: 'fake transaction', getDynamicSamplingContext: () => ({}) };
const span = {
fake: 'span',
getTraceContext: () => ({ a: 'b' }),
Expand Down
8 changes: 3 additions & 5 deletions packages/utils/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,13 @@ export function createEventEnvelopeHeaders(
dsn: DsnComponents,
): EventEnvelopeHeaders {
const dynamicSamplingContext = event.sdkProcessingMetadata && event.sdkProcessingMetadata.dynamicSamplingContext;

return {
event_id: event.event_id as string,
sent_at: new Date().toISOString(),
...(sdkInfo && { sdk: sdkInfo }),
...(!!tunnel && { dsn: dsnToString(dsn) }),
...(event.type === 'transaction' &&
dynamicSamplingContext && {
trace: dropUndefinedKeys({ ...dynamicSamplingContext }),
}),
...(dynamicSamplingContext && {
trace: dropUndefinedKeys({ ...dynamicSamplingContext }),
}),
};
}

0 comments on commit 943df92

Please sign in to comment.