Skip to content

Commit 7ca96c1

Browse files
authored
fix: Remove undefined values from trace context (getsentry#2413)
1 parent ee3a903 commit 7ca96c1

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
## 5.12.3
6+
7+
- [apm] fix: Remove undefined keys from trace.context
8+
59
## 5.12.2
610

711
- [apm] ref: Check if Tracing integration is enabled before dropping transaction

packages/apm/src/span.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -314,35 +314,43 @@ export class Span implements SpanInterface, SpanContext {
314314
* @inheritDoc
315315
*/
316316
public getTraceContext(): object {
317-
return {
318-
data: this.data,
319-
description: this.description,
320-
op: this.op,
321-
parent_span_id: this._parentSpanId,
322-
span_id: this._spanId,
323-
// Undefined status will be dropped by `JSON.stringify` anyway so it's safe to read it directly like that
324-
status: this.tags.status,
325-
tags: this.tags,
326-
trace_id: this._traceId,
327-
};
317+
// JSON.parse + JSON.stringify to remove undefined values
318+
// tslint:disable-next-line: no-unsafe-any
319+
return JSON.parse(
320+
JSON.stringify({
321+
data: this.data,
322+
description: this.description,
323+
op: this.op,
324+
parent_span_id: this._parentSpanId,
325+
span_id: this._spanId,
326+
// Undefined status will be dropped by `JSON.stringify` anyway so it's safe to read it directly like that
327+
status: this.tags.status,
328+
tags: this.tags,
329+
trace_id: this._traceId,
330+
}),
331+
);
328332
}
329333

330334
/**
331335
* @inheritDoc
332336
*/
333337
public toJSON(): object {
334-
return {
335-
data: this.data,
336-
description: this.description,
337-
op: this.op,
338-
parent_span_id: this._parentSpanId,
339-
sampled: this.sampled,
340-
span_id: this._spanId,
341-
start_timestamp: this.startTimestamp,
342-
tags: this.tags,
343-
timestamp: this.timestamp,
344-
trace_id: this._traceId,
345-
transaction: this.transaction,
346-
};
338+
// JSON.parse + JSON.stringify to remove undefined values
339+
// tslint:disable-next-line: no-unsafe-any
340+
return JSON.parse(
341+
JSON.stringify({
342+
data: this.data,
343+
description: this.description,
344+
op: this.op,
345+
parent_span_id: this._parentSpanId,
346+
sampled: this.sampled,
347+
span_id: this._spanId,
348+
start_timestamp: this.startTimestamp,
349+
tags: this.tags,
350+
timestamp: this.timestamp,
351+
trace_id: this._traceId,
352+
transaction: this.transaction,
353+
}),
354+
);
347355
}
348356
}

0 commit comments

Comments
 (0)