Skip to content

Replace koa-prometheus-exporter with prom-client#79

Merged
mjradwin merged 1 commit into
mainfrom
claude/koa-prom-client-middleware-6kjlf9
Jun 29, 2026
Merged

Replace koa-prometheus-exporter with prom-client#79
mjradwin merged 1 commit into
mainfrom
claude/koa-prom-client-middleware-6kjlf9

Conversation

@mjradwin

Copy link
Copy Markdown
Member

Summary

Replaces the @echo-health/koa-prometheus-exporter dependency with the standard prom-client library, implementing a custom metrics endpoint middleware instead of relying on the wrapper package.

Changes

  • Dependency update: Removed @echo-health/koa-prometheus-exporter, added prom-client (^15.1.3)
  • Metrics initialization: Added promClient.collectDefaultMetrics() at module load to collect Node.js runtime metrics (event-loop lag, GC duration, heap usage, etc.)
  • Custom middleware: Replaced prometheus.middleware({}) with a custom metricsEndpoint middleware that:
    • Checks for GET requests to /metrics
    • Sets the appropriate Content-Type header from the prom-client registry
    • Returns metrics via promClient.register.metrics()
    • Delegates other requests to the next middleware
  • Direct client usage: Removed the intermediate const promClient = prometheus.client assignment and now use prom-client directly

Implementation Details

The custom middleware approach provides more explicit control over the metrics endpoint while maintaining the same functionality. The collectDefaultMetrics() call is placed at module load in app-common.js (which is cached) so that in production, where www and download run as separate processes, each process gets its own collector instance.

https://claude.ai/code/session_015kkNPwFkKLCTmbRYerVwSy

The @echo-health/koa-prometheus-exporter wrapper hard-pinned prom-client
^10.2.3, which only emits the mean event-loop lag gauge
(nodejs_eventloop_lag_seconds) and routed GC through the legacy
prometheus-gc-stats package. As a result the perf_hooks-based collectors
were missing: event-loop lag percentiles (nodejs_eventloop_lag_p50/p90/p99
_seconds) and the nodejs_gc_duration_seconds histogram, both added in
prom-client v12.

Drop the wrapper (and its prometheus-gc-stats dependency) and depend on
prom-client ^15.1.3 directly. collectDefaultMetrics() is called once at
module load, and useObservability() serves /metrics from the default
registry. The existing http_requests_total counter (labels method,status)
is preserved unchanged; nothing in prom-client's default metrics replaces
it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kkNPwFkKLCTmbRYerVwSy
@mjradwin mjradwin merged commit b79c937 into main Jun 29, 2026
6 checks passed
@mjradwin mjradwin deleted the claude/koa-prom-client-middleware-6kjlf9 branch June 29, 2026 18:15
@sonarqubecloud

Copy link
Copy Markdown

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.

2 participants