Skip to content

Commit

Permalink
feat(sdk-trace-base): improved logging mechanism for operations on en…
Browse files Browse the repository at this point in the history
…ded spans, making it easier to trace issues in both Node.js and Browser environments

Reviewer: pichlermarc
Refs: open-telemetry#5113

feat(sdk-trace-base): improved logging mechanism for operations on ended spans, making it easier to trace issues in both Node.js and Browser environments
Reviewer: pichlermarc
Refs: open-telemetry#5113

feat(sdk-trace-base): improved logging mechanism for operations on ended spans, making it easier to trace issues in both Node.js and Browser environments

Reviewer: pichlermarc
Refs: open-telemetry#5113
  • Loading branch information
Victorsesan authored and neilfordyce committed Jan 23, 2025
1 parent 3447582 commit 8b2ce1c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
"karma-webpack": "5.0.1",
"lerna": "6.6.2",
"linkinator": "6.1.2",
"markdownlint-cli2": "0.17.2",
"prettier": "3.4.2",
"markdownlint-cli2": "0.15.0",
"prettier": "3.0.3",
"process": "0.11.10",
"semver": "7.6.3",
"ts-node": "10.9.2",
Expand Down
14 changes: 10 additions & 4 deletions packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,18 @@ export class SpanImpl implements Span {

private _isSpanEnded(): boolean {
if (this._ended) {
diag.warn(
`Can not execute the operation on ended Span {traceId: ${this._spanContext.traceId}, spanId: ${this._spanContext.spanId}}`
);
const error = new Error(`Operation attempted on ended Span {traceId: ${this._spanContext.traceId}, spanId: ${this._spanContext.spanId}}`);

diag.warn(
`Cannot execute the operation on ended Span {traceId: ${this._spanContext.traceId}, spanId: ${this._spanContext.spanId}}. Change log level to debug for stack trace.`,
error // Pass the error object as the second argument
);

// Optionally, you can still log the stack trace for additional context
diag.debug(`Stack trace for ended span operation: ${error.stack}`);
}
return this._ended;
}
}

// Utility function to truncate given value within size
// for value type of string, will truncate to given limit
Expand Down
26 changes: 26 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 @@ -280,6 +280,32 @@ describe('Span', () => {
assert.ok(span.isRecording() === false);
});
});
it('should log a warning and a debug message when span is ended', () => {
const span = new Span(
tracer,
ROOT_CONTEXT,
name,
spanContext,
SpanKind.CLIENT
);
span.end(); // End the span to set it to ended state

const warnStub = sinon.spy(diag, 'warn');
const debugStub = sinon.spy(diag, 'debug');

// Call the method that checks if the span is ended
span['_isSpanEnded']();

// Assert that the warning log was called with the expected message and an Error object
sinon.assert.calledOnce(warnStub);
sinon.assert.calledWith(warnStub, sinon.match(/Cannot execute the operation on ended Span/), sinon.match.instanceOf(Error));

// Assert that the debug log was called and contains a stack trace
sinon.assert.calledOnce(debugStub);
const debugMessage = debugStub.getCall(0).args[0];
assert.ok(debugMessage.startsWith('Stack trace for ended span operation:'));
});
});

describe('setAttribute', () => {
describe('when default options set', () => {
Expand Down

0 comments on commit 8b2ce1c

Please sign in to comment.