Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add DSC to all outgoing envelopes #7820

Merged
merged 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }),
}),
};
}