From 0ae25f191ea4ad2509ced1488b7a13c68b7f5bca Mon Sep 17 00:00:00 2001 From: Neil Fordyce Date: Thu, 23 Jan 2025 20:15:04 +0000 Subject: [PATCH] feat(sdk-trace-base): add stack trace to operation on ended Span warning (#5363) Co-authored-by: Victor --- CHANGELOG.md | 2 ++ .../opentelemetry-sdk-trace-base/src/Span.ts | 7 +++++- .../test/common/Span.test.ts | 25 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7aa1c8704d8..47c473998ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se ### :rocket: (Enhancement) +* feat(sdk-trace-base): add stack trace warning to debug instrumentation [#5363](https://github.com/open-telemetry/opentelemetry-js/pull/5363) @neilfordyce + ### :bug: (Bug Fix) * fix(sdk-metrics): do not export from `PeriodicExportingMetricReader` when there are no metrics to export. [#5288](https://github.com/open-telemetry/opentelemetry-js/pull/5288) @jacksonweber diff --git a/packages/opentelemetry-sdk-trace-base/src/Span.ts b/packages/opentelemetry-sdk-trace-base/src/Span.ts index f28f74a3e1e..c19aefe1d58 100644 --- a/packages/opentelemetry-sdk-trace-base/src/Span.ts +++ b/packages/opentelemetry-sdk-trace-base/src/Span.ts @@ -369,8 +369,13 @@ export class SpanImpl implements Span { private _isSpanEnded(): boolean { if (this._ended) { + const error = new Error( + `Operation attempted on ended Span {traceId: ${this._spanContext.traceId}, spanId: ${this._spanContext.spanId}}` + ); + diag.warn( - `Can not execute the operation on ended Span {traceId: ${this._spanContext.traceId}, spanId: ${this._spanContext.spanId}}` + `Cannot execute the operation on ended Span {traceId: ${this._spanContext.traceId}, spanId: ${this._spanContext.spanId}}`, + error ); } return this._ended; diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts index cff451c43c2..18b78728b21 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -281,6 +281,31 @@ describe('Span', () => { }); }); + it('should log a warning attempting to add event to ended span', () => { + const span = new SpanImpl({ + scope: tracer.instrumentationScope, + resource: tracer['_resource'], + context: ROOT_CONTEXT, + spanContext, + name, + kind: SpanKind.CLIENT, + spanLimits: tracer.getSpanLimits(), + spanProcessor: tracer['_spanProcessor'], + }); + span.end(); + + const warnStub = sinon.spy(diag, 'warn'); + + span.addEvent('oops, too late'); + + sinon.assert.calledOnce(warnStub); + sinon.assert.calledWith( + warnStub, + sinon.match(/Cannot execute the operation on ended Span/), + sinon.match.instanceOf(Error) + ); + }); + describe('setAttribute', () => { describe('when default options set', () => { it('should set an attribute', () => {