Skip to content

feat: add Prometheus metrics endpoint #595

Done

Description

@Anshumancanrock

Description

Right now there's no way to know what's happening on a running nostream relay without connecting directly to the database. No connection counts, no event throughput, no latency data. You're flying blind.

Proposal

Two things:

A GET /metrics endpoint that returns standard Prometheus text format. Operators who already run Prometheus + Grafana can point a scrape config at it and get dashboards immediately with no extra work.

Example output:

# HELP nostream_connections_active Current number of active WebSocket connections
# TYPE nostream_connections_active gauge
nostream_connections_active 42

# HELP nostream_connections_total Total WebSocket connections since process start
# TYPE nostream_connections_total counter
nostream_connections_total 18340

# HELP nostream_events_received_total Events received from clients
# TYPE nostream_events_received_total counter
nostream_events_received_total{kind="1"} 52341
nostream_events_received_total{kind="7"} 12032

# HELP nostream_events_accepted_total Events written to the database
# TYPE nostream_events_accepted_total counter
nostream_events_accepted_total 48210

# HELP nostream_events_rejected_total Events rejected before storage
# TYPE nostream_events_rejected_total counter
nostream_events_rejected_total{reason="rate-limited"} 2891
nostream_events_rejected_total{reason="invalid"} 340
nostream_events_rejected_total{reason="blocked"} 900

# HELP nostream_subscriptions_active Current active REQ subscriptions
# TYPE nostream_subscriptions_active gauge
nostream_subscriptions_active 156

# HELP nostream_eose_duration_seconds Time from REQ received to EOSE sent
# TYPE nostream_eose_duration_seconds histogram
nostream_eose_duration_seconds_bucket{le="0.01"} 18200
nostream_eose_duration_seconds_bucket{le="0.05"} 31200
nostream_eose_duration_seconds_bucket{le="0.1"} 38900
nostream_eose_duration_seconds_bucket{le="0.5"} 42100
nostream_eose_duration_seconds_bucket{le="1"} 42800
nostream_eose_duration_seconds_bucket{le="5"} 42990
nostream_eose_duration_seconds_bucket{le="+Inf"} 43001
nostream_eose_duration_seconds_sum 4312.94
nostream_eose_duration_seconds_count 43001

# HELP nostream_db_query_duration_seconds Database query latency
# TYPE nostream_db_query_duration_seconds histogram
nostream_db_query_duration_seconds_bucket{le="0.005"} 39100
nostream_db_query_duration_seconds_bucket{le="0.01"} 45210
nostream_db_query_duration_seconds_bucket{le="0.025"} 47300
nostream_db_query_duration_seconds_bucket{le="0.05"} 48100
nostream_db_query_duration_seconds_bucket{le="0.1"} 48900
nostream_db_query_duration_seconds_bucket{le="0.5"} 49150
nostream_db_query_duration_seconds_bucket{le="+Inf"} 49200
nostream_db_query_duration_seconds_sum 621.33
nostream_db_query_duration_seconds_count 49200

Also a simple GET /stats page for operators who don't run Grafana , just a server-rendered HTML page using the same Bootstrap template pattern as the existing /, /invoices, and /terms pages.

Both endpoints are disabled by default and opt-in via settings.yaml

How it works

A MetricsStore singleton holds all counters and histograms in memory. Each component calls into it directly:

  • WebSocketServerAdapter increments connection counters on open/close
  • WebSocketAdapter tracks active subscription count
  • EventMessageHandler increments event counters per kind and per rejection reason
  • SubscribeMessageHandler records EOSE latency
  • EventRepository records query latency

Plan

I'll split this into smaller PRs:

PR 1 : MetricsStore + settings flag + tests. No routes yet, just the data structure.

PR 2 : GET /metrics route + connection and subscription counters wired up. At this point you can actually curl the endpoint.

PR 3 : Event counters: received/accepted/rejected by kind and reason, wired into EventMessageHandler.

PR 4 : Latency histograms: EOSE duration in the subscribe handler, query duration in the event repository.

PR 5 : GET /stats HTML page for operators who prefer a browser over Prometheus.

PR 6 : Docs: metric reference + a docker-compose example with a Prometheus + Grafana sidecar for operators who want to set up the full stack.

Activity

  1. added
    enhancement
    on May 2, 2026
  2. chappie-daemon commented on Oct 24, 2026

    @chappie-daemon
    Collaborator

    Decision (2026-10-24): into Phase 6, as part of #868

    Filed on Phase 6: Hardening + benchmark and delivered inside #868.

    The reason is that #868's 72-hour soak cannot be observed without it: the soak's whole acceptance is that the relay stays healthy under sustained load, and "healthy" is currently unmeasurable from outside the process — this issue's own opening line, that there is no way to know what is happening on a running relay without connecting to the database, is precisely what makes the soak unfalsifiable.

    It is the one item in this set that cannot be deferred past the phase that uses it: connections, throughput and queue depth at soak time have to be scraped live, or the soak produces a log rather than a measurement. Costed as one build pass inside #868's estimate rather than as a row of its own.

  3. added
    rewrite
    on Oct 24, 2026
  4. chappie-daemon commented on Nov 29, 2026

    @chappie-daemon
    Collaborator

    Row 1 of #868's Phase 6 split, taken 2026-11-29 as factory job 042 on 5c6dc917.

    Recon and spec are done (spec digest 67b0562a, three passes through the red team); build is running. What recon measured and the spec decides:

    • the only HTTP surface is the admin router on the relay's single listener, and <prefix>/admin/metrics is already the console's SSE stream — so the scrape path is <prefix>/metrics, mounted outside the admin layers;
    • the surface's only credential is a 24 h HMAC cookie a Prometheus target cannot hold — so auth is METRICS_TOKEN + Authorization: Bearer, constant-time, with METRICS_ENABLED as the switch (ADR 0021 records the cost of each);
    • no metrics crate and no exposition encoder exist in the workspace, so the text format is hand-written, with the bucket boundaries and the zero-state body pinned byte for byte;
    • counters live in nostream-relay beside ConnectionCap, with AUTH's OK frames carved out of both the accepted and rejected families so an authentication is not an event.

    The admin SSE snapshot's upgrade (its seven keys need a rate window and process statistics the tree has no source for) is its own row of #868, not this one.

    Note for the soak and benchmark rows: nostreamd still composes unavailable::stores() (#1206), so an accepted-throughput counter reads 0 at this commit. This row is honest about that; the two measurement rows cannot be until #1206 lands.

  5. chappie-daemon commented on Nov 29, 2026

    @chappie-daemon
    Collaborator

    Build and review are done (job 042, branch factory/042-prometheus-metrics rebased onto 1514e7f2). Approval is running.

    Round 1 ran code, security, architecture and performance behind the code gate; security and architecture dissented, and their three findings are fixed and confirmed in round 2:

    • the bearer token was printable through a derived Debug on PrometheusMetrics (the same class the admin state already redacts) — now a redaction mask with a case that fails without it;
    • the switch's unit case sat at the crate root because --exact matches the full test path — now beside its function in admin_http.rs, with the criterion's command amended (spec A19);
    • send_ok and send_auth_ok built the same OK frame twice — now one private queue_ok, with AUTH's carve-out and wire bytes unchanged.

    The code lens executed 20 inversions rather than asserting the tests could fail, and the performance lens measured rather than argued: 32.9 ns per EVENT frame on the kind counter (0.6% of a core at 50k events/s), and a render that holds the counter lock for 173.8 µs at the 1024-kind cap (5.9 µs at a realistic 18). Static analysis found nothing at either head.

  6. chappie-daemon commented on Nov 29, 2026

    @chappie-daemon
    Collaborator

    Closed by bb00f82 (#1282).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementrewrite

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions