Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(otel): don't throw when calling setActiveSpan at root #28323

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ext/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function hrToMs(hr: [number, number]): number {

export function enterSpan(span: Span): Context | undefined {
if (!span.isRecording()) return undefined;
const context = (CURRENT.get() || ROOT_CONTEXT).setValue(SPAN_KEY, span);
const context = (CURRENT.get() ?? ROOT_CONTEXT).setValue(SPAN_KEY, span);
return CURRENT.enter(context);
}

Expand Down Expand Up @@ -254,9 +254,9 @@ class Tracer {
throw new Error("startActiveSpan requires a function argument");
}
if (options?.root) {
context = undefined;
context = ROOT_CONTEXT;
} else {
context = context ?? CURRENT.get();
context = context ?? CURRENT.get() ?? ROOT_CONTEXT;
}
const span = this.startSpan(name, options, context);
const ctx = CURRENT.enter(context.setValue(SPAN_KEY, span));
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/cli/otel_basic/__test__.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"links": {
"args": "run -A main.ts links.ts",
"output": "links.out"
},
"start_active_span": {
"args": "run -A main.ts start_active_span.ts",
"output": "start_active_span.out"
}
}
}
48 changes: 48 additions & 0 deletions tests/specs/cli/otel_basic/start_active_span.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"spans": [
{
"traceId": "00000000000000000000000000000001",
"spanId": "0000000000000001",
"traceState": "",
"parentSpanId": "",
"flags": 1,
"name": "top level span",
"kind": 1,
"startTimeUnixNano": "[WILDCARD]",
"endTimeUnixNano": "[WILDCARD]",
"attributes": [],
"droppedAttributesCount": 0,
"events": [],
"droppedEventsCount": 0,
"links": [],
"droppedLinksCount": 0,
"status": {
"message": "",
"code": 0
}
},
{
"traceId": "00000000000000000000000000000002",
"spanId": "0000000000000002",
"traceState": "",
"parentSpanId": "",
"flags": 1,
"name": "root span",
"kind": 1,
"startTimeUnixNano": "[WILDCARD]",
"endTimeUnixNano": "[WILDCARD]",
"attributes": [],
"droppedAttributesCount": 0,
"events": [],
"droppedEventsCount": 0,
"links": [],
"droppedLinksCount": 0,
"status": {
"message": "",
"code": 0
}
}
],
"logs": [],
"metrics": []
}
12 changes: 12 additions & 0 deletions tests/specs/cli/otel_basic/start_active_span.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2018-2025 the Deno authors. MIT license.

import { trace } from "npm:@opentelemetry/[email protected]";

const tracer = trace.getTracer("example-tracer");

tracer.startActiveSpan("top level span", (span) => {
span.end();
});
tracer.startActiveSpan("root span", { root: true }, (span) => {
span.end();
});
Loading