AuditTrail.Adapter behaviour (audit_trail v0.1.1)

Copy Markdown

Behaviour for AuditTrail storage backends.

The library ships with three adapters:

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.

Functions

Resolve the configured adapter module and its options. Returns {module, keyword_opts}.

Callbacks

child_spec(opts)

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

get_logs(filters)

@callback get_logs(filters :: map()) :: {:ok, [map()]} | {:error, term()}

Query log entries from the storage backend. Returns {:ok, [log]} or {:error, reason}. Each log map must have keys: timestamp, labels, payload.

push(logs)

@callback push(logs :: [map()]) :: :ok | {:error, term()}

Push a batch of sanitized log entry maps to the storage backend. Called by the buffer on flush. Must be safe to retry.

Functions

resolve()

Resolve the configured adapter module and its options. Returns {module, keyword_opts}.