Trace context management for request correlation across telemetry events.
Provides trace_id and span_id generation and propagation for debugging and observability. Trace context flows through the process dictionary for automatic inclusion in telemetry events.
Usage
# Start a new trace for a request/operation
TraceContext.start_trace()
# Or with a custom trace_id (for distributed tracing)
TraceContext.start_trace(trace_id: "abc123")
# Start a child span within a trace
TraceContext.start_span("render_component")
# Get current context for logging/telemetry
ctx = TraceContext.current()
# => %{trace_id: "abc123", span_id: "def456", parent_span_id: nil}
# End the current span
TraceContext.end_span()Process Dictionary Keys
:raxol_trace_id- Current trace identifier:raxol_span_id- Current span identifier:raxol_parent_span_id- Parent span for nested operations:raxol_span_stack- Stack of span IDs for nesting:raxol_causation_id- Id of the event/span that caused the current work
Causation vs parent span
parent_span_id reflects synchronous nesting (a span starts inside
another). causation_id reflects asynchronous handoff (an event
caused this work to happen, possibly across processes or messages).
Both can hold values simultaneously and serve different debugging
needs.
Integration
The TelemetryAdapter automatically includes trace context in all events when available. No manual instrumentation needed for basic tracing.
Summary
Functions
Checks if there is an active trace context.
Clears the current trace context.
Returns the current trace context, or empty context if none active.
Ends the current span and restores the parent span.
Formats the current context as a string for logging.
Creates context from HTTP headers (for distributed tracing).
Sets the causation id for the current process.
Starts a child span within the current trace.
Starts a new trace, optionally with a provided trace_id.
Returns context as a map suitable for HTTP headers or metadata.
Executes a function within a new span.
Executes a function within a new trace.
Types
Functions
@spec active?() :: boolean()
Checks if there is an active trace context.
@spec clear() :: :ok
Clears the current trace context.
Call this when a request/operation completes to clean up.
@spec current() :: context()
Returns the current trace context, or empty context if none active.
Examples
ctx = TraceContext.current()
%{trace_id: "abc", span_id: "def", parent_span_id: nil}
@spec end_span() :: context()
Ends the current span and restores the parent span.
@spec format() :: String.t()
Formats the current context as a string for logging.
Examples
TraceContext.format()
# => "[trace:abc123 span:def456]"
Creates context from HTTP headers (for distributed tracing).
Examples
TraceContext.from_headers(%{"x-trace-id" => "abc123"})
@spec set_causation(causation_id() | nil) :: context()
Sets the causation id for the current process.
Use this when handing work off across messages or processes: the
sender's span_id becomes the receiver's causation_id, forming a
chain independent of synchronous span nesting.
Passing nil clears the causation id.
Starts a child span within the current trace.
Returns the new context with the child span active. The parent span is preserved on a stack for proper nesting.
Examples
TraceContext.start_span("database_query")
# ... do work ...
TraceContext.end_span()
Starts a new trace, optionally with a provided trace_id.
Options
:trace_id- Use existing trace_id (for distributed tracing):span_name- Name for the root span (default: "root")
Examples
TraceContext.start_trace()
TraceContext.start_trace(trace_id: "external-123")
Returns context as a map suitable for HTTP headers or metadata.
Examples
headers = TraceContext.to_headers()
# => %{"x-trace-id" => "abc123", "x-span-id" => "def456"}
Executes a function within a new span.
Automatically starts the span before execution and ends it after, even if the function raises an exception.
Examples
result = TraceContext.with_span("render", fn ->
render_component()
end)
@spec with_trace((-> result)) :: result when result: term()
Executes a function within a new trace.
Creates a new trace context, executes the function, and clears the context after completion.
Examples
result = TraceContext.with_trace(fn ->
# All telemetry events here will have trace_id
process_request()
end)