March Docs

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

typeRequestEntryRequestEntry = {#
typeMetricsSummaryMetricsSummary = {#

Functions

fnerror_counterror_count() : Int#

Return the number of 5xx errors recorded.

fninstrumentinstrument(plug_fn)#

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()
fnrecent_requestsrecent_requests() : List(RequestEntry)#

Return the last N recorded requests (up to 100).

fnrecordrecord(conn)#

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)
fnresetreset()#

Reset all metrics counters. Useful in tests.

fnsummarysummary() : MetricsSummary#

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 requests
fntotaltotal() : Int#

Return the total number of requests recorded.