# `TraceGuard.Storage`
[🔗](https://github.com/HataluliRandima/trace_guard/blob/v0.1.0/lib/trace_guard/storage.ex#L1)

**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.** `c: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.
* **`c: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`.** `c:get/2` returns
  `{:ok, report}`, `:error` or `{:error, :unavailable}`; `c: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.

# `put_result`

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

# `report`

```elixir
@type report() :: %{required(String.t()) =&gt; term()}
```

# `summary`

```elixir
@type summary() :: %{required(String.t()) =&gt; term()}
```

# `child_spec`
*optional* 

```elixir
@callback child_spec(keyword()) :: Supervisor.child_spec()
```

# `clear`

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

# `delete`

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

# `get`

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

# `list`

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

# `list_matching`
*optional* 

```elixir
@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`

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

# `stats`

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

# `clear`

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

Deletes all reports.

# `delete`

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

Deletes one report.

# `dropped`

```elixir
@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`

```elixir
@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`

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

Lists report summaries from the configured adapter, newest first.

# `list_matching`

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

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

# `lost`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@spec stats() :: map()
```

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

# `stored`

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

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

# `summary`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
