TraceGuard.Storage.SQLite (trace_guard v0.1.0)

Copy Markdown View Source

Experimental in 0.1.0 (the SQLite adapter): the API may change before 1.0.

A persistent TraceGuard.Storage adapter for local development and testing: reports are kept in a SQLite file and survive restarts.

Requires the optional dependency {:exqlite, "~> 0.42"} (the driver behind ecto_sqlite3). It opens its own database file and needs no Ecto repo.

config :trace_guard,
  storage: {TraceGuard.Storage.SQLite, database: "priv/trace_guard_reports.db"}

Options

  • :database (required): path of the SQLite file (created if missing).
  • :max_reports: retained reports (default: the :max_reports config).
  • :max_age: if set, reports older than this many milliseconds are removed by cleanup (wall-clock time, so it survives restarts).
  • :cleanup_interval: periodic cleanup in ms (default 60_000).
  • :busy_timeout: how long a write waits for a SQLite lock, in ms (default 1_000).
  • :call_timeout: default timeout for reads, in ms (default 5_000).
  • :file_mode: permissions given to the database file when the adapter creates it (default 0o600, owner read/write only). SQLite gives its -wal and -shm files the same mode. An existing file keeps its permissions.

Unknown or invalid options raise ArgumentError when the adapter starts.

Writes never block requests

put/2 runs in the request process and only does admission (an :atomics increment against :max_pending_reports) and a send/2 to the single writer process. That is the same bounded-queue contract as TraceGuard.ReportStore, so the writer's mailbox never holds more than :max_pending_reports reports, even while the database is locked or missing. The writer does the I/O.

Storage format (schema version 1)

CREATE TABLE trace_guard_reports (
  trace_id  TEXT PRIMARY KEY NOT NULL,  -- 32 hex chars
  seq       INTEGER NOT NULL,           -- insertion order (newest = highest)
  stored_at INTEGER NOT NULL,           -- unix ms when written
  summary   TEXT NOT NULL,              -- JSON, TraceGuard.Storage.summary/1
  report    TEXT NOT NULL               -- JSON, the redacted report
);
CREATE INDEX trace_guard_reports_seq ON trace_guard_reports (seq);

The version is kept in PRAGMA user_version. A database with a newer version is not touched; the adapter reports itself unavailable instead. Every statement uses bound parameters.

Transactions, retention and recovery

  • Each report is one transaction: upsert by trace_id (a resubmitted trace replaces its row), then delete everything but the newest :max_reports rows. A report is announced as stored only after COMMIT.
  • Any write error (locked database, I/O error, encoding failure) rolls back and emits [:trace_guard, :report, :dropped] with {:sqlite_error, message} or :serialization_error, never a false :stored.
  • If the database can't be opened (at start, or after an error closed it), the writer keeps running with no connection. Admitted reports are dropped with reason {:sqlite, :unavailable}, and it retries opening every second. Requests are never affected.
  • After a writer restart, committed reports are read back from the file. Reports admitted but not yet committed are recorded as :lost.
  • Rows that fail to decode return {:error, :corrupt} from get and are skipped by list. Each such read is counted in stats as :corrupt_reads.
  • cleanup/0 applies :max_age and :max_reports on demand. It also runs every :cleanup_interval.

WAL journal mode with synchronous = NORMAL: a power loss can lose the most recent commits but doesn't corrupt the file.

Deleted data

PRAGMA secure_delete = ON makes SQLite zero deleted content, and every delete, clear and cleanup is followed by PRAGMA wal_checkpoint(TRUNCATE), so removed reports don't linger in the database or WAL files. (Copies made outside SQLite, such as backups and filesystem snapshots, are out of scope.)

Summary

Functions

Returns a specification to start this module under a supervisor.

Applies :max_age and :max_reports now. Returns {:ok, deleted_rows} or {:error, :unavailable}.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

cleanup(timeout \\ 5000)

@spec cleanup(timeout()) :: {:ok, non_neg_integer()} | {:error, :unavailable}

Applies :max_age and :max_reports now. Returns {:ok, deleted_rows} or {:error, :unavailable}.