Skip to content

feat(api): Add Prometheus metrics endpoint (issue #219)#282

Merged
Xhristin3 merged 2 commits into
XStreamRollz:mainfrom
Richardkingz2019:feat/prometheus-metrics-219
Jun 17, 2026
Merged

feat(api): Add Prometheus metrics endpoint (issue #219)#282
Xhristin3 merged 2 commits into
XStreamRollz:mainfrom
Richardkingz2019:feat/prometheus-metrics-219

Conversation

@Richardkingz2019

Copy link
Copy Markdown
Contributor

Summary

Closes #219

Adds a Prometheus-compatible GET /metrics endpoint to the NestJS API, providing full production observability.

Changes

New files

  • api/src/metrics/metrics.service.tsMetricsService wraps a dedicated prom-client Registry and exposes:
    • http_requests_total (Counter, labels: method/path/status_code)
    • http_request_duration_seconds (Histogram, 10-bucket, same labels)
    • websocket_connections_total (Counter)
    • websocket_active_connections (Gauge)
    • Node.js default metrics via collectDefaultMetrics
  • api/src/metrics/metrics.controller.tsGET /metrics returns Prometheus text format with correct Content-Type
  • api/src/metrics/metrics.module.ts — NestJS module exporting MetricsService
  • api/src/metrics/metrics.service.spec.ts — unit tests for all counters, histogram, gauge, and default metrics
  • api/src/metrics/metrics.controller.spec.ts — unit tests for response headers and body

Modified files

  • api/package.json — add prom-client@15.1.3, @nestjs/testing@10.3.0 (devDep)
  • api/src/app.module.ts — import MetricsModule
  • api/src/gateways/gateways.module.ts — import MetricsModule so StreamsGateway can inject MetricsService
  • api/src/gateways/streams.gateway.ts — inject MetricsService (optional) and track WS connect/disconnect
  • api/src/middleware/request-logger.middleware.ts — inject MetricsService (optional) and record HTTP counters/histogram on response finish

Acceptance criteria

Criterion Status
GET /metrics returns Prometheus text format
Default Node.js process metrics included
HTTP request counter incremented per route
HTTP request duration histogram populated
WebSocket connection counter tracked

Testing

cd api && npm test   # 21 tests, 4 suites — all pass
cd api && npx tsc --noEmit   # exit 0
cd api && npm run lint        # exit 0

- Install prom-client@15.1.3
- Add MetricsService with Registry, counters, histogram, and gauge
- Add MetricsController exposing GET /metrics in Prometheus text format
- Add MetricsModule exporting MetricsService
- Register collectDefaultMetrics for Node.js process/GC/heap metrics
- Track http_requests_total and http_request_duration_seconds in RequestLoggerMiddleware
- Track websocket_connections_total and websocket_active_connections in StreamsGateway
- Import MetricsModule in AppModule and GatewaysModule
- Add unit tests: metrics.service.spec.ts, metrics.controller.spec.ts
- All 21 tests pass, TypeScript clean, ESLint clean

Closes XStreamRollz#219

@Xhristin3 Xhristin3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the clean separation — metrics module isolated from app code, /metrics excluded from request logging so we don't pollute our own data, and tests cover gauges + counters. LGTM, merging 🎉

@Xhristin3 Xhristin3 merged commit a56215c into XStreamRollz:main Jun 17, 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

Development

Successfully merging this pull request may close these issues.

obs: Add Prometheus metrics endpoint to API

2 participants