Otel.API.Trace (otel v0.2.0)

Copy Markdown View Source

Trace API facade — TracerProvider, Context Interaction, and Span Creation entry points (OTel trace/api.md §TracerProvider L88-L157, §Context Interaction L159-L183, §Span Creation L378-L414).

Thin dispatcher over Otel.API.Trace.TracerProvider and the registered Tracer implementation. Context Interaction functions read/write span-in-context using a module-private key so user code never needs to know the Context Key (spec L171-L173 "API users SHOULD NOT have access to the Context Key").

Span Creation dispatches through the Tracer's start_span/4; the new Span is not automatically set as the active span (spec L382). Use with_span/4,5 for automatic context management including exception recording (spec L385 "MAY be offered additionally as a separate operation").

All functions are safe for concurrent use (spec trace/api.md L843-L853 "all methods MUST be documented that implementations need to be safe for concurrent use by default").

Public API

FunctionRole
current_span/1Application (OTel API MUST) — Extract Span from Context (L167)
set_current_span/2Application (OTel API MUST) — Combine Span with Context (L168)
start_span/3, start_span/4Application (OTel API MUST) — Span Creation (L378-L414)
current_span/0Application (OTel API SHOULD) — Get active span from implicit context (L177)
set_current_span/1Application (OTel API SHOULD) — Set active span into implicit context (L178)
with_span/4, with_span/5Application (OTel API MAY) — closure-form separate operation (L385)
make_current/1Application (OTel API MAY) — manual "set active span" (L384-L386)
detach/1Application (OTel API MAY) — revert make_current/1 (L384-L386)
get_tracer/1Application (Convenience) — facade over TracerProvider

References

  • OTel Trace API §TracerProvider: opentelemetry-specification/specification/trace/api.md L88-L157
  • OTel Trace API §Context Interaction: opentelemetry-specification/specification/trace/api.md L159-L183
  • OTel Trace API §Span Creation: opentelemetry-specification/specification/trace/api.md L378-L414
  • Reference impl: opentelemetry-erlang/apps/opentelemetry_api/src/otel_tracer.erl

Summary

Types

Options for span creation. See Otel.API.Trace.Span.start_opts/0.

Functions

Application (OTel API SHOULD) — "Get the currently active span from the implicit context" (trace/api.md L177).

Application (OTel API MUST) — "Extract the Span from a Context instance" (trace/api.md L167).

Application (OTel API MAY) — revert a make_current/1 call using the token it returned (trace/api.md L384-L386).

Application (Convenience) — facade over Otel.API.Trace.TracerProvider.

Application (OTel API MAY) — set span_ctx as the currently active span and return a detach token (trace/api.md L384-L386 "this functionality MAY be offered additionally as a separate operation").

Application (OTel API SHOULD) — "Set the currently active span into a new context, and make that the implicit context" (trace/api.md L178).

Application (OTel API MUST) — "Combine the Span with a Context instance, creating a new Context instance" (trace/api.md L168).

Application (OTel API MUST) — Span Creation using the implicit (process-local) context as parent (trace/api.md L378-L414).

Application (OTel API MUST) — Span Creation with an explicit parent context (trace/api.md L378-L414).

Application (OTel API MAY) — dispatches to the Tracer's with_span/5 callback using the implicit (process-local) context as parent (trace/api.md L385).

Application (OTel API MAY) — same as with_span/4 but with an explicit parent context (trace/api.md L385).

Types

start_opts()

@type start_opts() :: Otel.API.Trace.Span.start_opts()

Options for span creation. See Otel.API.Trace.Span.start_opts/0.

Functions

current_span()

@spec current_span() :: Otel.API.Trace.SpanContext.t()

Application (OTel API SHOULD) — "Get the currently active span from the implicit context" (trace/api.md L177).

Equivalent to current_span(Otel.API.Ctx.current()) reading from the process-local ambient context.

current_span(ctx)

@spec current_span(ctx :: Otel.API.Ctx.t()) :: Otel.API.Trace.SpanContext.t()

Application (OTel API MUST) — "Extract the Span from a Context instance" (trace/api.md L167).

Reads the SpanContext stored in ctx. When no span has been set, returns an empty SpanContext struct — the invalid sentinel (W3C §trace-id L103 + §parent-id L114).

detach(token)

@spec detach(token :: Otel.API.Ctx.t()) :: :ok

Application (OTel API MAY) — revert a make_current/1 call using the token it returned (trace/api.md L384-L386).

Restores the previous ambient context, undoing the most recent make_current/1 whose token is passed. Delegates to Otel.API.Ctx.detach/1.

get_tracer(instrumentation_scope)

@spec get_tracer(instrumentation_scope :: Otel.API.InstrumentationScope.t()) ::
  Otel.API.Trace.Tracer.t()

Application (Convenience) — facade over Otel.API.Trace.TracerProvider.

Returns a Tracer for the given instrumentation scope. Equivalent to calling Otel.API.Trace.TracerProvider.get_tracer/1 directly but exposed on Otel.API.Trace as the user-facing entry point.

make_current(span_ctx)

@spec make_current(span_ctx :: Otel.API.Trace.SpanContext.t()) :: Otel.API.Ctx.t()

Application (OTel API MAY) — set span_ctx as the currently active span and return a detach token (trace/api.md L384-L386 "this functionality MAY be offered additionally as a separate operation").

For most call sites, prefer with_span/4,5 — it handles attach, exception recording, detach, and end_span in a single closure-safe call. make_current/1 exists for cases where the active span must outlive a single function scope — async handoff, test fixtures, or non-closure interop.

Callers MUST pair each make_current/1 with a detach/1 on every exit path. Use try/after or equivalent to guarantee revert on exceptions:

token = Otel.API.Trace.make_current(span_ctx)
try do
  # ... work ...
after
  Otel.API.Trace.detach(token)
end

Internally composes set_current_span/2 with Otel.API.Ctx.attach/1. Stacked calls are LIFO — nested make_current/detach pairs restore the prior active span in reverse order.

set_current_span(span_ctx)

@spec set_current_span(span_ctx :: Otel.API.Trace.SpanContext.t()) :: :ok

Application (OTel API SHOULD) — "Set the currently active span into a new context, and make that the implicit context" (trace/api.md L178).

Writes span_ctx under the Tracing API's private key in the process-local ambient context.

set_current_span(ctx, span_ctx)

@spec set_current_span(
  ctx :: Otel.API.Ctx.t(),
  span_ctx :: Otel.API.Trace.SpanContext.t()
) ::
  Otel.API.Ctx.t()

Application (OTel API MUST) — "Combine the Span with a Context instance, creating a new Context instance" (trace/api.md L168).

Returns a new context with span_ctx stored under the Tracing API's private key. ctx is not modified.

start_span(tracer, name, opts \\ [])

@spec start_span(
  tracer :: Otel.API.Trace.Tracer.t(),
  name :: String.t(),
  opts :: start_opts()
) ::
  Otel.API.Trace.SpanContext.t()

Application (OTel API MUST) — Span Creation using the implicit (process-local) context as parent (trace/api.md L378-L414).

Per spec L382, the newly created span is not automatically set as the current span. Use with_span/4,5 for automatic context management.

Per spec L403, adding attributes via opts[:attributes] at creation is preferred over Span.set_attribute/3 later — samplers can only consider information already present during creation.

start_span(ctx, tracer, name, opts)

@spec start_span(
  ctx :: Otel.API.Ctx.t(),
  tracer :: Otel.API.Trace.Tracer.t(),
  name :: String.t(),
  opts :: start_opts()
) :: Otel.API.Trace.SpanContext.t()

Application (OTel API MUST) — Span Creation with an explicit parent context (trace/api.md L378-L414).

Per spec L391-L392, only a full Context is accepted as the parent — not a raw Span or SpanContext. Per spec L382 the newly created span is not set as the current span.

with_span(tracer, name, opts \\ [], fun)

@spec with_span(
  tracer :: Otel.API.Trace.Tracer.t(),
  name :: String.t(),
  opts :: start_opts(),
  fun :: (Otel.API.Trace.SpanContext.t() -> result)
) :: result
when result: term()

Application (OTel API MAY) — dispatches to the Tracer's with_span/5 callback using the implicit (process-local) context as parent (trace/api.md L385).

Forwards to the registered Tracer implementation which owns the full span lifecycle (attach/fun/detach/end). See Otel.API.Trace.Tracer.with_span/5 callback contract for lifecycle-ownership rationale.

with_span(ctx, tracer, name, opts, fun)

@spec with_span(
  ctx :: Otel.API.Ctx.t(),
  tracer :: Otel.API.Trace.Tracer.t(),
  name :: String.t(),
  opts :: start_opts(),
  fun :: (Otel.API.Trace.SpanContext.t() -> result)
) :: result
when result: term()

Application (OTel API MAY) — same as with_span/4 but with an explicit parent context (trace/api.md L385).