AuditTrail. Adapter behaviour
(audit_trail v0.1.1)
Copy Markdown
Behaviour for AuditTrail storage backends.
The library ships with three adapters:
AuditTrail.Adapters.Loki(default) — Grafana Loki via HTTP push/queryAuditTrail.Adapters.Postgres— PostgreSQL via your app's Ecto RepoAuditTrail.Adapters.TimescaleDB— TimescaleDB (Postgres + hypertable)
Choosing an adapter
Configure in config.exs:
# Loki (default — no extra config needed if loki_push_url is set)
config :audit_trail, storage_adapter: AuditTrail.Adapters.Loki
# Postgres
config :audit_trail,
storage_adapter: {AuditTrail.Adapters.Postgres, repo: MyApp.Repo}
# TimescaleDB
config :audit_trail,
storage_adapter: {AuditTrail.Adapters.TimescaleDB, repo: MyApp.Repo}Implementing a custom adapter
defmodule MyApp.AuditAdapter do
@behaviour AuditTrail.Adapter
@impl true
def push(logs), do: ...
@impl true
def get_logs(filters), do: ...
@impl true
def child_spec(_opts), do: nil
end
Summary
Callbacks
Optional OTP child spec to be started under the AuditTrail supervision tree.
Return nil if no supervised process is needed (most adapters).
Query log entries from the storage backend.
Returns {:ok, [log]} or {:error, reason}.
Each log map must have keys: timestamp, labels, payload.
Push a batch of sanitized log entry maps to the storage backend. Called by the buffer on flush. Must be safe to retry.
Callbacks
@callback child_spec(opts :: keyword()) :: Supervisor.child_spec() | nil
Optional OTP child spec to be started under the AuditTrail supervision tree.
Return nil if no supervised process is needed (most adapters).
Query log entries from the storage backend.
Returns {:ok, [log]} or {:error, reason}.
Each log map must have keys: timestamp, labels, payload.
Push a batch of sanitized log entry maps to the storage backend. Called by the buffer on flush. Must be safe to retry.