Internal module. Not part of TraceGuard's public API: it may change in
any release. Use the functions in TraceGuard instead.
Request-local trace context.
Correlation strategy
A Phoenix request is handled from start to finish by a single process (one
Cowboy stream process, or one Bandit connection/stream process), and
:telemetry handlers run synchronously in the process that emits the
event. TraceGuard relies on exactly these two facts:
TraceGuard.Plugstores a fresh context in the request process's process dictionary under a private key.- A telemetry handler appends an event only if the emitting process has an active context. The event therefore belongs to that request by construction, not by timing heuristics.
Consequences, which are deliberate and documented:
- Events emitted by other processes (for example a
Taskstarted by a controller, or a query run inside aGenServer) find no context and are ignored. The process dictionary does not propagate across processes and TraceGuard does not pretend otherwise. - In-flight state is only reachable from its own process, so one request can never read or append to another request's trace.
- Bandit reuses a process for keep-alive requests on the same connection. Each new request replaces any stale context, and contexts are removed when the request completes.
Summary
Functions
Returns the calling process's active context, or nil.
Generates a new trace ID: 128 random bits as 32 lowercase hex characters (the same shape as a W3C Trace Context trace-id).
Stores an updated context for the calling process.
Starts a new context in the calling process, replacing any stale one.
Removes and returns the calling process's context.
Returns true if value has the shape of a TraceGuard trace ID.
Types
@type t() :: %TraceGuard.Context{ dropped: non_neg_integer(), events: [map()], failure: map() | nil, request: map(), retained: non_neg_integer(), role: :request | :task, seq: non_neg_integer(), spans: %{optional(atom()) => [integer()]}, started_at: DateTime.t() | nil, started_mono: integer(), tasks: TraceGuard.Propagation.Token.t() | :unavailable | map() | nil, trace_id: String.t() }
Functions
@spec current() :: t() | nil
Returns the calling process's active context, or nil.
@spec generate_trace_id() :: String.t()
Generates a new trace ID: 128 random bits as 32 lowercase hex characters (the same shape as a W3C Trace Context trace-id).
@spec put(t()) :: :ok
Stores an updated context for the calling process.
Starts a new context in the calling process, replacing any stale one.
@spec take() :: t() | nil
Removes and returns the calling process's context.
Returns true if value has the shape of a TraceGuard trace ID.