Jido. Signal. Trace. Context
(Jido Signal v2.2.1)
View Source
Trace context struct for distributed tracing.
Represents W3C Trace Context compatible trace information that can be propagated across signal boundaries.
Fields
trace_id- 32 hex chars (128-bit) shared across entire workflowspan_id- 16 hex chars (64-bit) unique to this signalparent_span_id- Parent's span_id for hierarchy linkage (optional)causation_id- ID of the signal that caused this one (optional)tracestate- W3C tracestate for vendor-specific data (optional)
W3C Trace Context Compatibility
IDs are generated in W3C-compatible format:
- trace_id: 32 hex chars (128-bit)
- span_id: 16 hex chars (64-bit)
Examples
# Create a new root context
{:ok, ctx} = Context.new()
# Create with causation
{:ok, ctx} = Context.new(causation_id: "external-123")
# Create a child context
{:ok, child} = Context.child_of(parent_ctx, "parent-signal-id")
Summary
Functions
Parses a W3C traceparent header and creates a child context.
Creates a child trace context that continues the parent's trace.
Creates a child trace context, raising on error.
Creates a context from a map.
Creates a context from a map, raising on error.
Parses a W3C traceparent header into trace context.
Creates a new root trace context.
Creates a new root trace context, raising on error.
Returns the Zoi schema for Context
Converts the context to a map, filtering out nil values.
Converts context to telemetry metadata with jido_ prefixed keys.
Formats trace context as W3C traceparent header value.
Checks if a trace context is valid.
Types
Functions
@spec child_from_traceparent( String.t(), keyword() ) :: {:ok, t()} | {:error, :invalid_traceparent}
Parses a W3C traceparent header and creates a child context.
This is useful when receiving an external request with trace headers and you want to create a new span as a child of that trace.
Options
:causation_id- Optional causation ID for the child span:tracestate- Optional tracestate to associate with the child
Examples
{:ok, child} = Context.child_from_traceparent(
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
causation_id: "http-request-123"
)
Creates a child trace context that continues the parent's trace.
The child:
- Shares the parent's
trace_id - Gets a new unique
span_id - Sets
parent_span_idto the parent'sspan_id - Sets
causation_idto the causing signal's ID - Inherits
tracestate
Examples
{:ok, child} = Context.child_of(parent_ctx, parent_signal.id)
Creates a child trace context, raising on error.
Examples
child = Context.child_of!(parent_ctx, "signal-id")
Creates a context from a map.
Examples
{:ok, ctx} = Context.from_map(%{trace_id: "abc...", span_id: "def..."})
Creates a context from a map, raising on error.
Examples
ctx = Context.from_map!(%{trace_id: "abc...", span_id: "def..."})
Parses a W3C traceparent header into trace context.
Returns {:ok, context} on success, {:error, reason} on failure.
Examples
{:ok, ctx} = Context.from_traceparent("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
Creates a new root trace context.
Generates new W3C-compatible trace_id (32 hex chars) and span_id (16 hex chars).
Options
:causation_id- Optional causation reference (e.g., external request ID):tracestate- Optional W3C tracestate string for vendor-specific data
Examples
{:ok, ctx} = Context.new()
{:ok, ctx} = Context.new(causation_id: "external-123")
{:ok, ctx} = Context.new(tracestate: "vendor1=value1")
Creates a new root trace context, raising on error.
Examples
ctx = Context.new!()
Returns the Zoi schema for Context
Converts the context to a map, filtering out nil values.
Useful for storing in signal extensions where nil values may not be allowed.
Examples
Context.to_map(ctx)
#=> %{trace_id: "abc...", span_id: "def..."}
Converts context to telemetry metadata with jido_ prefixed keys.
Examples
Context.to_telemetry_metadata(ctx)
#=> %{jido_trace_id: "abc...", jido_span_id: "def...", ...}
Formats trace context as W3C traceparent header value.
Format: {version}-{trace-id}-{span-id}-{flags}
Version is always "00" (current W3C spec). Flags is "01" (sampled).
Examples
Context.to_traceparent(ctx)
#=> "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
Checks if a trace context is valid.
A valid trace context has:
trace_idthat is 32 lowercase hex charactersspan_idthat is 16 lowercase hex characters
Examples
Context.valid?(ctx) #=> true
Context.valid?(nil) #=> false