Skip to content

Commit

Permalink
feat(sdk-trace-base): add stack trace to operation on ended Span warn…
Browse files Browse the repository at this point in the history
…ing (#5363)

Co-authored-by: Victor <[email protected]>
  • Loading branch information
neilfordyce and Victorsesan authored Jan 23, 2025
1 parent b4ae9ca commit 0ae25f1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 0ae25f1

Please sign in to comment.