Experimental in 0.1.0 (the storage behaviour): the API may change before 1.0.
Where finished reports go: a small behaviour plus the facade that the tracing engine and the public API call.
The tracing engine only ever calls put/1 and never knows which adapter is
configured. The default adapter is TraceGuard.ReportStore (bounded,
in-memory). Select another with:
config :trace_guard, storage: MyAdapter
config :trace_guard, storage: {MyAdapter, some: :option}Adapter contract
- Input is already redacted.
put/2receives the final, sanitized and redacted report (a JSON-safe, string-keyed map). Adapters must not need the raw data and never receive it. put/2must not block for long and must never raise on overload. It returns:{:ok, :stored}when the report was retained before returning;{:ok, :queued}when it was accepted and will be retained asynchronously. The adapter must then callstored/1once it is retained, ordropped/2if it is lost;{:error, :overloaded}when it was refused to protect memory;{:error, reason}for any other failure. Returning{:ok, _}for a report that was refused is a contract violation: the facade counts and reports on what adapters return.
- Retention is bounded. An adapter keeps at most a configured number of reports and evicts the oldest first (or documents its own policy). It must also bound any queue in front of its storage.
- Reads return, never exit, and honor
:timeout.get/2returns{:ok, report},:erroror{:error, :unavailable};list/1returns summaries (seesummary/1), newest first. When the facade passestimeout: msin the options, reads must return within about that time.
The facade adds what every adapter would otherwise have to repeat:
exception safety (an adapter that raises or exits is reported as
{:error, {:adapter_failed, kind}} and never reaches the request),
counters, and telemetry events.
Telemetry events
[:trace_guard, :report, :stored]: measurements%{}, metadata%{trace_id: String.t(), summary: map()}. Emitted when a report is retained (by the facade for{:ok, :stored}, by the adapter for{:ok, :queued}).[:trace_guard, :report, :dropped]: metadata%{trace_id: String.t(), reason: term()}. Emitted when a report is not retained.
TraceGuard's own handlers never subscribe to these events, and a request's
trace context is removed before put/1 runs, so storing a report can never
produce further diagnostic events.
Summary
Callbacks
Optional (Phase 6): summaries matching every {field, value} criterion
exactly (fields are summary keys such as "incoming_id"), newest first.
Lets an adapter filter in its database instead of returning every summary.
The facade re-applies the criteria to whatever is returned, so an adapter
can narrow results but never widen them.
Functions
Deletes all reports.
Deletes one report.
For adapters that returned {:ok, :queued}: call if an accepted report is
lost before being retained. Also used by the facade for refusals.
Emits [:trace_guard, :report, :dropped].
Fetches a report from the configured adapter. opts may contain
timeout: ms, which adapters honor for the read.
Lists report summaries from the configured adapter, newest first.
Summaries matching all {field, value} criteria exactly, newest first.
Uses the adapter's list_matching/2 when it has one, else list/1.
For adapters that discover accepted reports were lost without knowing
their trace IDs (for example, queued submissions that died with a crashed
process). Only counted, as :lost in stats/0.
For a queueing adapter starting (or restarting): submissions still marked
pending for adapter died with its previous incarnation. Each is recorded
as a drop with reason :lost (so TraceGuard.report_status/1 can say
so) and counted in :lost. Returns the number.
Submits a finished report to the configured adapter.
Submission counters (since the application started) merged with the adapter's own statistics.
For adapters that returned {:ok, :queued}: call once the report is
retained. Emits [:trace_guard, :report, :stored].
The summary of a report used by list/0 and discovery filters. Built only
from fields that are already in the (redacted) report.
Types
Callbacks
@callback child_spec(keyword()) :: Supervisor.child_spec()
@callback clear(keyword()) :: :ok | {:error, :unavailable}
Optional (Phase 6): summaries matching every {field, value} criterion
exactly (fields are summary keys such as "incoming_id"), newest first.
Lets an adapter filter in its database instead of returning every summary.
The facade re-applies the criteria to whatever is returned, so an adapter
can narrow results but never widen them.
@callback put(report(), keyword()) :: put_result()
Functions
@spec clear() :: :ok | {:error, :unavailable}
Deletes all reports.
@spec delete(String.t()) :: :ok | {:error, :unavailable}
Deletes one report.
For adapters that returned {:ok, :queued}: call if an accepted report is
lost before being retained. Also used by the facade for refusals.
Emits [:trace_guard, :report, :dropped].
Fetches a report from the configured adapter. opts may contain
timeout: ms, which adapters honor for the read.
@spec list() :: [summary()] | {:error, :unavailable}
Lists report summaries from the configured adapter, newest first.
Summaries matching all {field, value} criteria exactly, newest first.
Uses the adapter's list_matching/2 when it has one, else list/1.
@spec lost(pos_integer()) :: :ok
For adapters that discover accepted reports were lost without knowing
their trace IDs (for example, queued submissions that died with a crashed
process). Only counted, as :lost in stats/0.
@spec lost_pending(module()) :: non_neg_integer()
For a queueing adapter starting (or restarting): submissions still marked
pending for adapter died with its previous incarnation. Each is recorded
as a drop with reason :lost (so TraceGuard.report_status/1 can say
so) and counted in :lost. Returns the number.
@spec put(report()) :: put_result()
Submits a finished report to the configured adapter.
Never raises and never blocks beyond what the adapter's contract allows.
Returns the adapter's outcome; any failure is also counted and emitted as a
[:trace_guard, :report, :dropped] event.
@spec stats() :: map()
Submission counters (since the application started) merged with the adapter's own statistics.
@spec stored(report()) :: :ok
For adapters that returned {:ok, :queued}: call once the report is
retained. Emits [:trace_guard, :report, :stored].
The summary of a report used by list/0 and discovery filters. Built only
from fields that are already in the (redacted) report.