Skip to content

feat(metrics): comprehensive Prometheus metrics interceptor for API latency and error tracking - #470

Merged
Cjay-Cyber-2 merged 1 commit into
ASTROIDX556:mainfrom
goodness-cpu:add-comprehensive-prometheus
Oct 22, 2026
Merged

Cjay-Cyber-2 merged 1 commit into
ASTROIDX556:mainfrom
goodness-cpu:add-comprehensive-prometheus

Conversation

@goodness-cpu

Copy link
Copy Markdown
Contributor

Summary

Implements comprehensive Prometheus metrics instrumentation for the Astroid API, closing the observability gap for API latency, error rates, and throughput.

What this PR does

  • MetricsInterceptor (src/common/interceptors/metrics.interceptor.ts) is the single authoritative instrumentation path for HTTP metrics:
    • Records the http_request_duration_seconds histogram (seconds) per method / route / status_code.
    • Increments the http_requests_total counter with low-cardinality labels (route paths are normalized, e.g. /api/users/:id).
    • Tracks in-flight requests (activeRequests) and exposes getActiveRequestCount() for load monitoring.
    • Skips the /metrics scrape endpoint to prevent self-instrumentation.
    • Uses rxjs finalize so each request is recorded exactly once, on both success and error, using the final response status code.
    • Swallows and logs metric-recording failures so observability can never fail a request.
  • /metrics endpoint (src/modules/metrics/metrics.controller.ts + metrics.service.ts) exposes the Prometheus text exposition format (text/plain; version=0.0.4) from the prom-client registry, with IP-restricted access (METRICS_ALLOWED_IPS) and exemption from throttle/audit/rate-limit policies.
  • Removed the redundant RequestMetricsMiddleware instrumentation path from the global wiring so the global MetricsInterceptor is the single source of HTTP request metrics (prevents double counting of the counter/histogram).

Acceptance criteria

  • MetricsInterceptor created in src/common/interceptors/metrics.interceptor.ts.
  • /metrics endpoint returning Prometheus-formatted text metrics.
  • Unit tests verifying metric increments on successful and failed requests.

Testing & validation

  • npm run test — full unit suite passes (181 test files / 1,893 tests).
  • npm run typecheck — clean.
  • npm run lint — clean.
  • Manual validation: run npm run start:dev and curl -H "Authorization: ..." http://localhost:<port>/metrics (scrape path is global-prefix excluded, i.e. /metrics).

Related issues

Closes #421

Closes #416

…atency and error tracking

- Record each HTTP request exactly once via the global MetricsInterceptor
  using rxjs finalize, so the duration histogram and status counter observe
  the final outcome (success or error) of every request.
- Remove the redundant RequestMetricsMiddleware instrumentation path to
  prevent double counting of http_requests_total / http_request_duration_seconds.
- Keep the /metrics scrape endpoint (Prometheus text format), route
  normalization, worker and stream metrics intact.

Closes ASTROIDX556#421
@Cjay-Cyber-2
Cjay-Cyber-2 merged commit f8cb378 into ASTROIDX556:main Oct 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants