diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d78a36367ca..7f9e48beb9dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## 5.12.3 + +- [apm] fix: Remove undefined keys from trace.context + ## 5.12.2 - [apm] ref: Check if Tracing integration is enabled before dropping transaction diff --git a/packages/apm/src/span.ts b/packages/apm/src/span.ts index 2ea97b86ed4d..d1a6c1203aec 100644 --- a/packages/apm/src/span.ts +++ b/packages/apm/src/span.ts @@ -314,35 +314,43 @@ export class Span implements SpanInterface, SpanContext { * @inheritDoc */ public getTraceContext(): object { - return { - data: this.data, - description: this.description, - op: this.op, - parent_span_id: this._parentSpanId, - span_id: this._spanId, - // Undefined status will be dropped by `JSON.stringify` anyway so it's safe to read it directly like that - status: this.tags.status, - tags: this.tags, - trace_id: this._traceId, - }; + // JSON.parse + JSON.stringify to remove undefined values + // tslint:disable-next-line: no-unsafe-any + return JSON.parse( + JSON.stringify({ + data: this.data, + description: this.description, + op: this.op, + parent_span_id: this._parentSpanId, + span_id: this._spanId, + // Undefined status will be dropped by `JSON.stringify` anyway so it's safe to read it directly like that + status: this.tags.status, + tags: this.tags, + trace_id: this._traceId, + }), + ); } /** * @inheritDoc */ public toJSON(): object { - return { - data: this.data, - 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, - timestamp: this.timestamp, - trace_id: this._traceId, - transaction: this.transaction, - }; + // JSON.parse + JSON.stringify to remove undefined values + // tslint:disable-next-line: no-unsafe-any + return JSON.parse( + JSON.stringify({ + data: this.data, + 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, + timestamp: this.timestamp, + trace_id: this._traceId, + transaction: this.transaction, + }), + ); } }