Skip to content

Commit

Permalink
fix(apm): Add trace context to all events (#2486)
Browse files Browse the repository at this point in the history
* fix: Add trace context to all events

* meta: Changelog

* meta: Add tests
  • Loading branch information
HazAT committed Mar 11, 2020
1 parent 268c7e7 commit 57fe8c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
- [apm] feat: Add a simple heartbeat check, if activities don't change in 3 beats, finish the transaction (#2478)
- [apm] feat: Make use of the `performance` browser API to provide better instrumentation (#2474)
- [browser] ref: Move global error handler + unhandled promise rejection to instrument (#2475)
- [apm] fix: Add trace context to all events (#2486)

## 5.13.2

Expand Down
8 changes: 4 additions & 4 deletions packages/apm/src/span.ts
Expand Up @@ -359,13 +359,13 @@ export class Span implements SpanInterface, SpanContext {
*/
public getTraceContext(): object {
return dropUndefinedKeys({
data: this.data,
data: Object.keys(this.data).length > 0 ? this.data : undefined,
description: this.description,
op: this.op,
parent_span_id: this._parentSpanId,
span_id: this._spanId,
status: this.tags.status,
tags: this.tags,
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
trace_id: this._traceId,
});
}
Expand All @@ -375,14 +375,14 @@ export class Span implements SpanInterface, SpanContext {
*/
public toJSON(): object {
return dropUndefinedKeys({
data: this.data,
data: Object.keys(this.data).length > 0 ? this.data : undefined,
description: this.description,
op: this.op,
parent_span_id: this._parentSpanId,
sampled: this.sampled,
span_id: this._spanId,
start_timestamp: this.startTimestamp,
tags: this.tags,
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
timestamp: this.timestamp,
trace_id: this._traceId,
transaction: this.transaction,
Expand Down
4 changes: 0 additions & 4 deletions packages/apm/test/span.test.ts
Expand Up @@ -167,11 +167,9 @@ describe('Span', () => {
expect(serialized).toHaveProperty('start_timestamp');
delete (serialized as { start_timestamp: number }).start_timestamp;
expect(serialized).toStrictEqual({
data: {},
parent_span_id: 'b',
sampled: false,
span_id: 'd',
tags: {},
trace_id: 'c',
});
});
Expand Down Expand Up @@ -257,9 +255,7 @@ describe('Span', () => {
const spanB = new Span({ spanId: 'd', traceId: 'c' });
const context = spanB.getTraceContext();
expect(context).toStrictEqual({
data: {},
span_id: 'd',
tags: {},
trace_id: 'c',
});
});
Expand Down
3 changes: 3 additions & 0 deletions packages/hub/src/scope.ts
Expand Up @@ -332,6 +332,9 @@ export class Scope implements ScopeInterface {
if (this._transaction) {
event.transaction = this._transaction;
}
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 @@ -245,6 +245,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 57fe8c6

Please sign in to comment.