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
  • Loading branch information
Victorsesan committed Nov 17, 2024
1 parent 4446508 commit 8a71fbb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,34 @@
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "5.59.11",
"@typescript-eslint/parser": "5.59.11",
"@typescript-eslint/eslint-plugin": "8.14.0",
"@typescript-eslint/parser": "8.14.0",
"assert": "2.1.0",
"benchmark": "2.1.4",
"eslint": "8.44.0",
"eslint-config-prettier": "9.0.0",
"eslint": "9.15.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-header": "3.1.1",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "5.0.1",
"gh-pages": "6.0.0",
"eslint-plugin-prettier": "5.2.1",
"gh-pages": "6.2.0",
"glob": "^11.0.0",
"karma": "6.4.4",
"karma-chrome-launcher": "3.1.0",
"karma-chrome-launcher": "3.2.0",
"karma-coverage": "2.2.1",
"karma-mocha": "2.0.1",
"karma-mocha-webworker": "1.3.0",
"karma-spec-reporter": "0.0.36",
"karma-webpack": "5.0.1",
"lerna": "6.6.2",
"linkinator": "6.0.6",
"markdownlint-cli2": "0.13.0",
"prettier": "3.0.3",
"lerna": "8.1.9",
"linkinator": "6.1.2",
"markdownlint-cli2": "0.15.0",
"prettier": "3.3.3",
"process": "0.11.10",
"semver": "7.6.3",
"typedoc": "0.22.18",
"typedoc-plugin-missing-exports": "1.0.0",
"typedoc-plugin-resolve-crossmodule-references": "0.2.2",
"typescript": "4.4.4",
"typedoc": "0.26.11",
"typedoc-plugin-missing-exports": "3.0.2",
"typedoc-plugin-resolve-crossmodule-references": "0.3.3",
"typescript": "5.6.3",
"util": "0.12.5"
},
"changelog": {
Expand Down
4 changes: 3 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,10 @@ export class Span implements APISpan, ReadableSpan {
private _isSpanEnded(): boolean {
if (this._ended) {
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}}. Change log level to debug for stack trace.`
);
// Adding a debug log to capture the stack trace
diag.debug(`Stack trace for ended span operation: ${new Error().stack}`);
}
return this._ended;
}
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 @@ -250,6 +250,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
sinon.assert.calledOnce(warnStub);
sinon.assert.calledWith(warnStub, sinon.match(/Cannot execute the operation on ended Span/));

// 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 8a71fbb

Please sign in to comment.