From 5091fbffd01d02a16decb75773018eeef156f8f9 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Mon, 8 Jun 2020 14:06:07 +0200 Subject: [PATCH] fix: Re-add TraceContext for all events (#2656) * feat: Re-add TraceContext * meta: Changelog * ref: Remove unused import --- CHANGELOG.md | 1 + packages/apm/src/integrations/tracing.ts | 3 +++ packages/hub/src/scope.ts | 6 +++++ packages/hub/test/scope.test.ts | 32 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c39c9ecb43c6..ff276499f8d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott - [react] feat: Add @sentry/react package (#2631) - [browser] Change XHR instrumentation order to handle `onreadystatechange` breadcrumbs correctly (#2643) +- [apm] fix: Re-add TraceContext for all events (#2656) ## 5.16.1 diff --git a/packages/apm/src/integrations/tracing.ts b/packages/apm/src/integrations/tracing.ts index 72a19de83d55..d30758e2fb98 100644 --- a/packages/apm/src/integrations/tracing.ts +++ b/packages/apm/src/integrations/tracing.ts @@ -436,6 +436,9 @@ export class Tracing implements Integration { ...transactionContext, }) as Transaction; + // We set the transaction here on the scope so error events pick up the trace context and attach it to the error + hub.configureScope(scope => scope.setSpan(Tracing._activeTransaction)); + // The reason we do this here is because of cached responses // If we start and transaction without an activity it would never finish since there is no activity const id = Tracing.pushActivity('idleTransactionStarted'); diff --git a/packages/hub/src/scope.ts b/packages/hub/src/scope.ts index 3fdbd20a65cc..194595bc6133 100644 --- a/packages/hub/src/scope.ts +++ b/packages/hub/src/scope.ts @@ -377,6 +377,12 @@ export class Scope implements ScopeInterface { if (this._transaction) { event.transaction = this._transaction; } + // We want to set the trace context for normal events only if there isn't already + // a trace context on the event. There is a product feature in place where we link + // errors with transaction and it relys on that. + if (this._span) { + event.contexts = { trace: this._span.getTraceContext(), ...event.contexts }; + } this._applyFingerprint(event); diff --git a/packages/hub/test/scope.test.ts b/packages/hub/test/scope.test.ts index e69856487b09..f49da6cce595 100644 --- a/packages/hub/test/scope.test.ts +++ b/packages/hub/test/scope.test.ts @@ -265,6 +265,38 @@ describe('Scope', () => { }); }); + test('applyToEvent trace context', async () => { + expect.assertions(1); + const scope = new Scope(); + const span = { + fake: 'span', + getTraceContext: () => ({ a: 'b' }), + } as any; + scope.setSpan(span); + const event: Event = {}; + return scope.applyToEvent(event).then(processedEvent => { + expect((processedEvent!.contexts!.trace as any).a).toEqual('b'); + }); + }); + + test('applyToEvent existing trace context in event should be stronger', async () => { + expect.assertions(1); + const scope = new Scope(); + const span = { + fake: 'span', + getTraceContext: () => ({ a: 'b' }), + } as any; + scope.setSpan(span); + const event: Event = { + contexts: { + trace: { a: 'c' }, + }, + }; + return scope.applyToEvent(event).then(processedEvent => { + expect((processedEvent!.contexts!.trace as any).a).toEqual('c'); + }); + }); + test('clear', () => { const scope = new Scope(); scope.setExtra('a', 2);