TraceGuard.Context (trace_guard v0.1.0)

Copy Markdown View Source

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:

  1. TraceGuard.Plug stores a fresh context in the request process's process dictionary under a private key.
  2. 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 Task started by a controller, or a query run inside a GenServer) 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

t()

@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

current()

@spec current() :: t() | nil

Returns the calling process's active context, or nil.

generate_trace_id()

@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).

put(ctx)

@spec put(t()) :: :ok

Stores an updated context for the calling process.

start(trace_id, request)

@spec start(String.t(), map()) :: t()

Starts a new context in the calling process, replacing any stale one.

take()

@spec take() :: t() | nil

Removes and returns the calling process's context.

valid_trace_id?(value)

@spec valid_trace_id?(term()) :: boolean()

Returns true if value has the shape of a TraceGuard trace ID.