Skip to content

Commit 57fe8c6

Browse files
authored
fix(apm): Add trace context to all events (getsentry#2486)
* fix: Add trace context to all events * meta: Changelog * meta: Add tests
1 parent 268c7e7 commit 57fe8c6

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [apm] feat: Add a simple heartbeat check, if activities don't change in 3 beats, finish the transaction (#2478)
88
- [apm] feat: Make use of the `performance` browser API to provide better instrumentation (#2474)
99
- [browser] ref: Move global error handler + unhandled promise rejection to instrument (#2475)
10+
- [apm] fix: Add trace context to all events (#2486)
1011

1112
## 5.13.2
1213

packages/apm/src/span.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,13 @@ export class Span implements SpanInterface, SpanContext {
359359
*/
360360
public getTraceContext(): object {
361361
return dropUndefinedKeys({
362-
data: this.data,
362+
data: Object.keys(this.data).length > 0 ? this.data : undefined,
363363
description: this.description,
364364
op: this.op,
365365
parent_span_id: this._parentSpanId,
366366
span_id: this._spanId,
367367
status: this.tags.status,
368-
tags: this.tags,
368+
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
369369
trace_id: this._traceId,
370370
});
371371
}
@@ -375,14 +375,14 @@ export class Span implements SpanInterface, SpanContext {
375375
*/
376376
public toJSON(): object {
377377
return dropUndefinedKeys({
378-
data: this.data,
378+
data: Object.keys(this.data).length > 0 ? this.data : undefined,
379379
description: this.description,
380380
op: this.op,
381381
parent_span_id: this._parentSpanId,
382382
sampled: this.sampled,
383383
span_id: this._spanId,
384384
start_timestamp: this.startTimestamp,
385-
tags: this.tags,
385+
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
386386
timestamp: this.timestamp,
387387
trace_id: this._traceId,
388388
transaction: this.transaction,

packages/apm/test/span.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,9 @@ describe('Span', () => {
167167
expect(serialized).toHaveProperty('start_timestamp');
168168
delete (serialized as { start_timestamp: number }).start_timestamp;
169169
expect(serialized).toStrictEqual({
170-
data: {},
171170
parent_span_id: 'b',
172171
sampled: false,
173172
span_id: 'd',
174-
tags: {},
175173
trace_id: 'c',
176174
});
177175
});
@@ -257,9 +255,7 @@ describe('Span', () => {
257255
const spanB = new Span({ spanId: 'd', traceId: 'c' });
258256
const context = spanB.getTraceContext();
259257
expect(context).toStrictEqual({
260-
data: {},
261258
span_id: 'd',
262-
tags: {},
263259
trace_id: 'c',
264260
});
265261
});

packages/hub/src/scope.ts

+3
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ export class Scope implements ScopeInterface {
332332
if (this._transaction) {
333333
event.transaction = this._transaction;
334334
}
335+
if (this._span) {
336+
event.contexts = { trace: this._span.getTraceContext(), ...event.contexts };
337+
}
335338

336339
this._applyFingerprint(event);
337340

packages/hub/test/scope.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,38 @@ describe('Scope', () => {
245245
});
246246
});
247247

248+
test('applyToEvent trace context', async () => {
249+
expect.assertions(1);
250+
const scope = new Scope();
251+
const span = {
252+
fake: 'span',
253+
getTraceContext: () => ({ a: 'b' }),
254+
} as any;
255+
scope.setSpan(span);
256+
const event: Event = {};
257+
return scope.applyToEvent(event).then(processedEvent => {
258+
expect((processedEvent!.contexts!.trace as any).a).toEqual('b');
259+
});
260+
});
261+
262+
test('applyToEvent existing trace context in event should be stronger', async () => {
263+
expect.assertions(1);
264+
const scope = new Scope();
265+
const span = {
266+
fake: 'span',
267+
getTraceContext: () => ({ a: 'b' }),
268+
} as any;
269+
scope.setSpan(span);
270+
const event: Event = {
271+
contexts: {
272+
trace: { a: 'c' },
273+
},
274+
};
275+
return scope.applyToEvent(event).then(processedEvent => {
276+
expect((processedEvent!.contexts!.trace as any).a).toEqual('c');
277+
});
278+
});
279+
248280
test('clear', () => {
249281
const scope = new Scope();
250282
scope.setExtra('a', 2);

0 commit comments

Comments
 (0)