Bandera.Usage (bandera v0.5.0)

Copy Markdown View Source

Optional last-evaluated tracker. Attaches to [:bandera, :enabled?] and [:bandera, :variant] telemetry events and records, in ETS, the last time each flag was checked — the signal for Bandera.stale_flags/1.

When the Ecto persistence adapter is configured, evaluation history is also persisted to a bandera_usage DB table so it survives restarts and pod recycling. The DB is seeded into ETS once the Repo is available, and the whole ETS table is flushed back every flush_interval seconds (default 600 / 10 minutes). At 30-day stale granularity the flush cadence is irrelevant, so the flush is a simple last-writer-wins upsert — no per-write bookkeeping on the hot path.

Just add it to your supervision tree — it attaches its own telemetry handler in init/1 and detaches on shutdown, so the handler's lifecycle follows the process (a crash-and-restart re-attaches against a fresh ETS table):

children = [
  ...,
  Bandera.Usage
]

Create the usage table with Bandera.Ecto.Migrations.up_usage/0 from a migration before enabling DB persistence.

Summary

Functions

Registers the telemetry handler.

Returns a specification to start this module under a supervisor.

Unregisters the telemetry handler. Called automatically on shutdown.

Immediately flushes ETS to the DB. Useful in tests and clean shutdowns.

Returns the last UTC DateTime flag_name was evaluated, or nil if never seen.

Starts the Usage tracker. Add to your supervision tree.

Functions

attach()

@spec attach() :: :ok | {:error, :already_exists}

Registers the telemetry handler.

Called automatically from the GenServer's init/1; you do not need to call it yourself. Exposed mainly for tests.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

detach()

@spec detach() :: :ok | {:error, :not_found}

Unregisters the telemetry handler. Called automatically on shutdown.

flush()

@spec flush() :: :ok

Immediately flushes ETS to the DB. Useful in tests and clean shutdowns.

last_evaluated(flag_name)

@spec last_evaluated(atom()) :: DateTime.t() | nil

Returns the last UTC DateTime flag_name was evaluated, or nil if never seen.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the Usage tracker. Add to your supervision tree.