8000 fix: Re-add TraceContext for all events (#2656) · leesx/sentry-javascript@5091fbf · GitHub
[go: up one dir, main page]

Skip to content

Commit 5091fbf

Browse files
authored
fix: Re-add TraceContext for all events (getsentry#2656)
* feat: Re-add TraceContext * meta: Changelog * ref: Remove unused import
1 parent 5b479e2 commit 5091fbf

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
66
- [react] feat: Add @sentry/react package (#2631)
77
- [browser] Change XHR instrumentation order to handle `onreadystatechange` breadcrumbs correctly (#2643)
8+
- [apm] fix: Re-add TraceContext for all events (#2656)
89

910
## 5.16.1
1011

packages/apm/src/integrations/tracing.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
0