Skip to content

Commit f7e8d2b

Browse files
committed
feat: Re-add TraceContext
1 parent 6aa2ece commit f7e8d2b

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

packages/apm/src/integrations/tracing.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hub } from '@sentry/hub';
1+
import { Hub, Scope } from '@sentry/hub';
22
import { Event, EventProcessor, Integration, Severity, Span, SpanContext, TransactionContext } from '@sentry/types';
33
import {
44
addInstrumentationHandler,
@@ -436,6 +436,9 @@ export class Tracing implements Integration {
436436
...transactionContext,
437437
}) as Transaction;
438438

439+
// We set the transaction here on the scope so error events pick up the trace context and attach it to the error
440+
hub.configureScope(scope => scope.setSpan(Tracing._activeTransaction));
441+
439442
// The reason we do this here is because of cached responses
440443
// If we start and transaction without an activity it would never finish since there is no activity
441444
const id = Tracing.pushActivity('idleTransactionStarted');

packages/hub/src/scope.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ export class Scope implements ScopeInterface {
377377
if (this._transaction) {
378378
event.transaction = this._transaction;
379379
}
380+
// We want to set the trace context for normal events only if there isn't already
381+
// a trace context on the event. There is a product feature in place where we link
382+
// errors with transaction and it relys on that.
383+
if (this._span) {
384+
event.contexts = { trace: this._span.getTraceContext(), ...event.contexts };
385+
}
380386

381387
this._applyFingerprint(event);
382388

packages/hub/test/scope.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,38 @@ describe('Scope', () => {
265265
});
266266
});
267267

268+
test('applyToEvent trace context', async () => {
269+
expect.assertions(1);
270+
const scope = new Scope();
271+
const span = {
272+
fake: 'span',
273+
getTraceContext: () => ({ a: 'b' }),
274+
} as any;
275+
scope.setSpan(span);
276+
const event: Event = {};
277+
return scope.applyToEvent(event).then(processedEvent => {
278+
expect((processedEvent!.contexts!.trace as any).a).toEqual('b');
279+
});
280+
});
281+
282+
test('applyToEvent existing trace context in event should be stronger', async () => {
283+
expect.assertions(1);
284+
const scope = new Scope();
285+
const span = {
286+
fake: 'span',
287+
getTraceContext: () => ({ a: 'b' }),
288+
} as any;
289+
scope.setSpan(span);
290+
const event: Event = {
291+
contexts: {
292+
trace: { a: 'c' },
293+
},
294+
};
295+
return scope.applyToEvent(event).then(processedEvent => {
296+
expect((processedEvent!.contexts!.trace as any).a).toEqual('c');
297+
});
298+
});
299+
268300
test('clear', () => {
269301
const scope = new Scope();
270302
scope.setExtra('a', 2);

0 commit comments

Comments
 (0)