An immutable context that carries trace and span identifiers through the call stack.
The Context is the core mechanism for correlating telemetry data. It contains:
trace_id: A unique identifier for the entire trace/transactionspan_id: The current span within the trace (nil if not in a span)export_profile: Export sinks captured when the trace context is createdruntime_identity: Runtime identity captured when the context is created- governed-effect refs: effect, command, authority, and receipt refs carried explicitly
metadata: Additional key-value metadata for the trace
Summary
Functions
Returns trace metadata derived from the explicit context.
Retrieves a metadata value by key.
Returns the governed-effect refs carried by a context.
Creates a new context with a generated trace_id.
Creates a new context with the provided trace_id.
Returns a new context with the export profile captured for trace finish.
Returns a new context with governed-effect refs carried as explicit fields.
Returns a new context with merged metadata.
Returns a new context with the updated span_id.
Types
@type t() :: %AITrace.Context{ authority_ref: String.t() | nil, command_ref: String.t() | nil, effect_ref: String.t() | nil, export_profile: AITrace.ExportProfile.t() | nil, metadata: map(), receipt_ref: String.t() | nil, runtime_identity: AITrace.RuntimeIdentity.snapshot(), span_id: String.t() | nil, trace_id: String.t(), trace_id_source: map() }
Functions
Returns trace metadata derived from the explicit context.
@spec generate_id() :: String.t()
Retrieves a metadata value by key.
Examples
iex> ctx = AITrace.Context.new() |> AITrace.Context.with_metadata(%{user_id: 42})
iex> AITrace.Context.get_metadata(ctx, :user_id)
42
iex> AITrace.Context.get_metadata(ctx, :missing, :default)
:default
Returns the governed-effect refs carried by a context.
@spec new() :: t()
Creates a new context with a generated trace_id.
Examples
iex> ctx = AITrace.Context.new()
iex> is_binary(ctx.trace_id)
true
iex> ctx.span_id
nil
Creates a new context with the provided trace_id.
Examples
iex> ctx = AITrace.Context.new("my_trace_123")
iex> ctx.trace_id
"my_trace_123"
@spec with_export_profile(t(), AITrace.ExportProfile.t()) :: t()
Returns a new context with the export profile captured for trace finish.
Returns a new context with governed-effect refs carried as explicit fields.
Returns a new context with merged metadata.
Examples
iex> ctx = AITrace.Context.new()
iex> new_ctx = AITrace.Context.with_metadata(ctx, %{user_id: 42})
iex> new_ctx.metadata
%{user_id: 42}
Returns a new context with the updated span_id.
Examples
iex> ctx = AITrace.Context.new()
iex> new_ctx = AITrace.Context.with_span_id(ctx, "span_123")
iex> new_ctx.span_id
"span_123"