Skip to content

Commit

Permalink
fix: Re-add TraceContext for all events (#2656)
Browse files Browse the repository at this point in the history
* feat: Re-add TraceContext

* meta: Changelog

* ref: Remove unused import
  • Loading branch information
HazAT committed Jun 8, 2020
1 parent 5b479e2 commit 5091fbf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions packages/apm/src/integrations/tracing.ts
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions packages/hub/src/scope.ts
Expand Up @@ -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);

Expand Down
32 changes: 32 additions & 0 deletions packages/hub/test/scope.test.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 5091fbf

Please sign in to comment.