TraceGuard.Storage behaviour (trace_guard v0.1.0)

Copy Markdown View Source

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/2 receives the final, sanitized and redacted report (a JSON-safe, string-keyed map). Adapters must not need the raw data and never receive it.
  • put/2 must 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 call stored/1 once it is retained, or dropped/2 if 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/2 returns {:ok, report}, :error or {:error, :unavailable}; list/1 returns summaries (see summary/1), newest first. When the facade passes timeout: ms in 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

put_result()

@type put_result() :: {:ok, :stored | :queued} | {:error, :overloaded | term()}

report()

@type report() :: %{required(String.t()) => term()}

summary()

@type summary() :: %{required(String.t()) => term()}

Callbacks

child_spec(keyword)

(optional)
@callback child_spec(keyword()) :: Supervisor.child_spec()

clear(keyword)

@callback clear(keyword()) :: :ok | {:error, :unavailable}

delete(t, keyword)

@callback delete(String.t(), keyword()) :: :ok | {:error, :unavailable}

get(t, keyword)

@callback get(String.t(), keyword()) :: {:ok, report()} | :error | {:error, :unavailable}

list(keyword)

@callback list(keyword()) :: [summary()] | {:error, :unavailable}

list_matching(list, keyword)

(optional)
@callback list_matching([{String.t(), term()}], keyword()) ::
  [summary()] | {: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.

put(report, keyword)

@callback put(report(), keyword()) :: put_result()

stats(keyword)

@callback stats(keyword()) :: map()

Functions

clear()

@spec clear() :: :ok | {:error, :unavailable}

Deletes all reports.

delete(trace_id)

@spec delete(String.t()) :: :ok | {:error, :unavailable}

Deletes one report.

dropped(trace_id, reason)

@spec dropped(String.t(), term()) :: :ok

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].

get(trace_id, opts \\ [])

@spec get(String.t(), keyword()) :: {:ok, report()} | :error | {:error, :unavailable}

Fetches a report from the configured adapter. opts may contain timeout: ms, which adapters honor for the read.

list()

@spec list() :: [summary()] | {:error, :unavailable}

Lists report summaries from the configured adapter, newest first.

list_matching(criteria)

@spec list_matching([{String.t(), term()}]) :: [summary()] | {:error, :unavailable}

Summaries matching all {field, value} criteria exactly, newest first. Uses the adapter's list_matching/2 when it has one, else list/1.

lost(n)

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

lost_pending(adapter)

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

put(report)

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

stats()

@spec stats() :: map()

Submission counters (since the application started) merged with the adapter's own statistics.

stored(report)

@spec stored(report()) :: :ok

For adapters that returned {:ok, :queued}: call once the report is retained. Emits [:trace_guard, :report, :stored].

summary(report)

@spec summary(report()) :: summary()

The summary of a report used by list/0 and discovery filters. Built only from fields that are already in the (redacted) report.