Electric.Plug.TraceContextPlug (electric v1.7.4)

Copy Markdown View Source

A plug that extracts trace context from incoming HTTP headers and sets it as the parent span.

In addition to the standard W3C traceparent extraction, this plug parses Electric's sample-rate hint from the tracestate header. An upstream proxy or gateway that head-samples requests at a rate of 1:N tells us about that rate via a tracestate member of the form:

tracestate: electric=rate:<N>

The hint, together with the remote parent span context and its sampled flag, is stored in the conn so that downstream plugs can stamp the SampleRate attribute on exported spans, letting tracing backends that understand sampling weights scale aggregates by the upstream sampling rate (see sample_rate_attrs/2);

Hints that are missing, unparseable or have a rate below 1 are ignored.

Summary

Types

Remote trace context extracted from the request headers.

Functions

Callback implementation for Plug.call/2.

Callback implementation for Plug.init/1.

The name of the span attribute that carries the sampling weight.

Span attributes carrying the sampling weight for a response with the given status.

The remote trace context extracted from the request headers by this plug, or nil when the request did not carry a (valid) traceparent header.

Types

trace_context()

@type trace_context() :: %{sample_rate_hint: pos_integer() | nil}

Remote trace context extracted from the request headers.

  • :sample_rate_hint - the upstream 1:N sampling rate parsed from tracestate, or nil when absent/invalid

Functions

call(conn, opts)

Callback implementation for Plug.call/2.

init(opts)

Callback implementation for Plug.init/1.

sample_rate_attr()

@spec sample_rate_attr() :: String.t()

The name of the span attribute that carries the sampling weight.

sample_rate_attrs(conn, status)

@spec sample_rate_attrs(Plug.Conn.t(), integer() | nil) :: %{
  optional(String.t()) => pos_integer()
}

Span attributes carrying the sampling weight for a response with the given status.

Tracing backends that support weighted sampling read an integer span attribute named SampleRate and scale aggregates by it. Successful responses inherit the upstream sampling rate from the tracestate hint, while error (>= 500) responses are stamped with a rate of 1: they mirror the upstream's keep-all-errors-at-rate-1 semantics.

Returns an empty map when the request carried no usable rate hint, leaving the spans unweighted as before.

trace_context(conn)

@spec trace_context(Plug.Conn.t()) :: trace_context() | nil

The remote trace context extracted from the request headers by this plug, or nil when the request did not carry a (valid) traceparent header.