-
-
Notifications
You must be signed in to change notification settings - Fork 463
Open
Description
Summary
OtelTracer.currentOtelSpan always fails with NoSuchElementException when using the lightweight OTLP module (OtlpTracer.layer), even though Effect.currentSpan works correctly.
Current Behavior
When using the OTLP module:
Effect.currentSpan✅ returns the current spanOtelTracer.currentOtelSpan❌ always returnsNoSuchElementException
Root Cause
The currentOtelSpan implementation checks for a special symbol marker:
// packages/opentelemetry/src/internal/tracer.ts:241-249
export const currentOtelSpan = Effect.flatMap(
Effect.currentSpan,
(span) => {
if (OtelSpanTypeId in span) {
return Effect.succeed((span as OtelSpan).span)
}
return Effect.fail(new Cause.NoSuchElementException())
}
)- Official OpenTelemetry approach (
NodeSdk,Tracer.layer): CreatesOtelSpanclass instances with theOtelSpanTypeIdsymbol → works - OTLP module (
OtlpTracer.layer): Creates plain objects viaObject.create(SpanProto)without the symbol → always fails
Workaround
// Use Effect.currentSpan instead and manually construct the span context
export const getTracingBackendUrlEffect = (span: Tracer.Span) => {
const otelSpan = {
spanContext: () => ({
traceId: span.traceId,
spanId: span.spanId,
traceFlags: 0,
}),
}
return getTracingBackendUrl(otelSpan)
}Suggested Fix
At minimum, the JSDoc for currentOtelSpan should document this limitation:
/**
* Get the current OpenTelemetry span.
*
* **Note:** This only works when using the official OpenTelemetry API
* (via `Tracer.layer`, `NodeSdk.layer`, etc.).
* It does NOT work with the lightweight OTLP module (`OtlpTracer.layer`).
* When using OTLP, use `Effect.currentSpan` instead.
*/A more complete fix could provide an alternative API that works with both approaches, returning a unified interface with traceId, spanId, etc.
🤖 Generated with Claude Code
Metadata
Metadata
Assignees
Labels
No labels