Bastion.Metrics
lib/metrics.march — Bastion.Metrics: lightweight request metrics.
Collects request counts, error counts, and response timing for the dev dashboard and optional production observability. All data is stored in Vault so it's zero-overhead when the middleware isn't in the pipeline.
── Usage ──────────────────────────────────────────────────────────────────────
-- In your endpoint (dev mode only, or always for production monitoring):
let c = Bastion.Metrics.record(conn)
-- Or via Middleware.pipeline:
Middleware.pipeline([Bastion.Metrics.record, router_plug], conn)
-- Query stats:
let summary = Bastion.Metrics.summary()
-- { total: 142, errors: 3, avg_ms: 1.8, recent: [...] }
-- Reset all counters (useful in tests):
Bastion.Metrics.reset()Types
Functions
Return the number of 5xx errors recorded.
Wrap a plug to record request metrics around it (measures handler duration).
Use this to instrument the router or any plug-composable handler:
let app = Bastion.Metrics.instrument(Router.to_plug(router))
HttpServer.new(4000) |> HttpServer.plug(app) |> HttpServer.listen()Return the last N recorded requests (up to 100).
Plug that records basic request info (method, path) without duration.
Use when the plug is composed into a pipeline rather than wrapping it.
Status is recorded as 0 since it's not known until after the handler runs.
For accurate status + timing, prefer instrument/1.
Middleware.pipeline([Bastion.Metrics.record, router], conn)Reset all metrics counters. Useful in tests.
Return a summary of all recorded metrics.
let s = Bastion.Metrics.summary()
s.total -- total requests
s.errors -- 5xx count
s.avg_ms -- average response time (ms)
s.recent -- last up to 100 requestsReturn the total number of requests recorded.