Skip to content

Observability and Metrics

Three questions decide whether you can locate a fault in ten minutes: can you scrape metrics, can you join logs to one request, and does a broken dependency say so.

VariableDefaultMeaning
METRICS_ENABLEDtrueOff disables both instrumentation and the listener
METRICS_PORT9091Dedicated listener, path /metrics

This endpoint has no authentication, because Prometheus must scrape it anonymously, so it must only exist on the container network: compose publishes it with expose, not ports, and nginx never references 9091. No business route serves metrics — the old /api/v1/metrics required an admin session, so scrapers never got data.

A port conflict does not stop the process: the listener logs metrics listener on :9091 unavailable and the site keeps serving. “Site is fine, no metrics” means read that WARN line first.

deploy/prometheus.yml defines the jobs already: feedchamber scrapes backend:9091, feedchamber-health probes backend:8080/api/v1/health through blackbox exporter, and nats scrapes nats-exporter:7777. deploy/grafana/ holds a dashboard and datasource provisioning you can mount as-is.

AreaKey familiesWhat it tells you
HTTPfeedchamber_http_requests_total, _duration_seconds, _panics_totalpath is the collapsed route template (/api/v1/posts/:id), safe to panel
Databasefeedchamber_db_queries_total, _db_slow_queries_totalslow queries are the numerator, threshold 200 ms; without the denominator “slower” and “busier” look alike
NATSfeedchamber_nats_connected{client}, _disconnects_total, _reconnects_totalclients are only kv and eventbus; both counters climbing means flapping
KVfeedchamber_kv_operation_duration_seconds{op,outcome}, _kv_cas_exhausted_totalCAS exhaustion means hot keys fighting over one counter
Event busfeedchamber_eventbus_publish_total{event,result}, _dropped_totala rising dropped_total means notifications or XP are permanently missing
SSEfeedchamber_sse_connections{channel}, _sse_write_failures_total{channel}the gauge also drops on a normal tab close, so it cannot replace the failure counter
Webhooksfeedchamber_webhook_deliveries_total{outcome}, _pending_deliveriespending is a true COUNT(*) backlog
Authfeedchamber_login_failures_total{reason}, _permission_decisions_total{scope,result}three bounded reasons: unknown_user, no_credential, bad_password
Rate limitsfeedchamber_ratelimit_rejections_total{name}, _failopen_total{name}non-zero fail-open means protection was absent (KV failed, traffic allowed)
Read cachefeedchamber_readcache_hits_total{name}, _misses_total{name}fixed names: post_list, tag_list, space_tree, space_flat

Connection-pool metrics come from a custom collector and are not in the generated list at the end of the page: feedchamber_db_pool_max_open_connections, _open_connections, _in_use, _idle, _wait_count_total, _wait_duration_seconds_total.

Label values must stay bounded. Trace ids, user ids, raw path segments and error text are never labels, or the series count explodes.

Request logs are JSON (LOG_FORMAT=json), one line per request: trace_id, span_id, method, path, route (the template, written back after routing), status, latency_ms, user_uuid (empty when anonymous) and ip (trusts X-Forwarded-For and X-Real-IP). LOG_LEVEL accepts debug / info / warn / error and also governs legacy direct logrus calls; LOG_OUTPUT takes stdout, stderr or a file path.

To follow one request, grep its trace_id: services log through logger.FromContext(ctx), which already carries it.

middleware.Trace creates the trace and must run before Logger, or request logs have no trace id. Precedence is inbound traceparent > inbound X-Request-ID (32 lowercase hex) > fresh. X-Request-ID is always echoed; traceparent is echoed while TRACING_ENABLED=true — disabling it only stops parsing and echoing, logs still carry a trace. Events carry traceparent in the NATS header, so a notification’s log lines join the request that produced it.

GET /api/v1/health performs three real probes. db runs Ping within 3 s, kvstore writes, reads and deletes an _health_check key, storage stats the local directory or checks that the S3 bucket is configured. Any failure returns 503 while still listing all three results; a healthy DB also reports db_pool_max_open, db_pool_open, db_pool_in_use, db_pool_idle and db_pool_wait_count. Compose health checks hit this path, so a red probe also blocks depends_on startup ordering.

The thresholds in deploy/prometheus/rules/feedchamber.yml are starting points for a small self-hosted forum, not SLAs.

AlertConditionForSeverity
High5xxErrorRate5xx share > 5%5mpage
HighP99LatencyP99 > 1s10mticket
BackendMetricsEndpointDownup{job="feedchamber"} == 02mpage
HealthEndpointDownblackbox probe fails2mpage
DBPoolSaturationin_use / max_open > 0.9 or > 10 waits in 5m5mticket
NATSDisconnectedfeedchamber_nats_connected == 02mpage
EventBusDropsincrease(dropped_total[5m]) > 0immediatepage
RateLimiterFailOpenany increase over 5mimmediateticket

Three rules learned the hard way: histograms can only be probed via _count (absent() on the base name is always true), backlog gauges need a real COUNT(*) rather than the batch size, and login-failure alerts need a relative threshold — 5× the instance’s own 7-day mean with a floor of 10. A NATS partition never exits the process (reconnects are unlimited), which is exactly why it needs an alert: sessions, rate limits, events and permission sync all degrade silently.

Generated from the Name / Help declarations in the source and refreshed with the code. The connection pool and Go runtime collectors are not part of it.

31metrics

MetricTypeDescription
feedchamber_backup_job_duration_secondsHistogramBackup operation latency in seconds.
feedchamber_backup_jobs_totalCounterBackup operations by kind and outcome.
feedchamber_db_queries_totalCounterQueries executed through GORM.
feedchamber_db_slow_queries_totalCounterQueries slower than the configured slow-query threshold.
feedchamber_eventbus_dispatch_duration_secondsHistogramEvent handler execution time in seconds.
feedchamber_eventbus_dispatch_errors_totalCounterEvent handler panics recovered during dispatch.
feedchamber_eventbus_dropped_totalCounterEvents dropped because the dispatch buffer was full.
feedchamber_eventbus_publish_totalCounterEvents published to NATS by event type and result.
feedchamber_eventbus_queue_depthGaugeNumber of events buffered and not yet dispatched.
feedchamber_http_panics_totalCounterTotal count of panics recovered while serving HTTP requests.
feedchamber_http_request_duration_secondsHistogramHTTP request latency in seconds.
feedchamber_http_request_size_bytesHistogramHTTP request body size in bytes.
feedchamber_http_requests_totalCounterTotal count of HTTP requests.
feedchamber_http_response_size_bytesHistogramHTTP response body size in bytes.
feedchamber_kv_cas_exhausted_totalCounterKV increment operations that exhausted all CAS attempts.
feedchamber_kv_operation_duration_secondsHistogramKV store operation latency in seconds.
feedchamber_login_failures_totalCounterRejected login attempts by failure reason.
feedchamber_nats_connectedGauge1 while the NATS client connection is usable, 0 while disconnected.
feedchamber_nats_disconnects_totalCounterNATS connection losses, by client.
feedchamber_nats_reconnects_totalCounterSuccessful NATS reconnections, by client.
feedchamber_permission_cache_entriesGaugeEntries held in each permission cache.
feedchamber_permission_decisions_totalCounterPermission checks by scope (global|space) and result (allow|deny).
feedchamber_ratelimit_failopen_totalCounterRate limiter fail-open occurrences, by policy name.
feedchamber_ratelimit_rejections_totalCounterRequests rejected by a rate limiter, by policy name.
feedchamber_readcache_hits_totalCounterRead-through cache hits, labelled by cache name.
feedchamber_readcache_misses_totalCounterRead-through cache misses, labelled by cache name.
feedchamber_sse_connectionsGaugeCurrently open SSE streams by channel.
feedchamber_sse_write_failures_totalCounterFailed SSE writes that terminated a stream, by channel.
feedchamber_webhook_deliveries_totalCounterWebhook delivery attempts by outcome.
feedchamber_webhook_delivery_duration_secondsHistogramWebhook delivery request latency in seconds.
feedchamber_webhook_pending_deliveriesGaugeWebhook deliveries currently pending (true backlog).