Skip to content

feat(metrics): add prometheus latency histogram for /api/rate-limit - #698

Merged
greatest0fallt1me merged 1 commit into
Predictify-org:mainfrom
Ajibola6921:fix/rate-limit-histogram-642
Jul 28, 2026
Merged

feat(metrics): add prometheus latency histogram for /api/rate-limit#698
greatest0fallt1me merged 1 commit into
Predictify-org:mainfrom
Ajibola6921:fix/rate-limit-histogram-642

Conversation

@Ajibola6921

Copy link
Copy Markdown
Contributor

Overview

This PR adds a Prometheus latency histogram for the /api/rate-limit endpoint, tracking request duration segmented by route template and status code with explicit buckets. The middleware is registered on the router ahead of auth so that latency for rejected requests (e.g. 403) is captured as well, not just successful 200s.

Related Issue

Closes #642

Changes

Metrics

  • [ADD] rate_limit_request_duration_seconds histogram in src/metrics/registry.ts with explicit buckets [0.01, 0.05, 0.1, 0.5, 1, 2, 5, 10] and label names ["route", "status"]
  • [ADD] rateLimitMetricsMiddleware in src/routes/rate-limit.ts that records request duration on every response using process.hrtime.bigint(), registered before the admin auth check
  • [ADD] Focused test suite in tests/rate-limit.test.ts covering:
    • Route behavior (paginated listing, cursor pagination)
    • Input validation (invalid limit, empty cursor, negative limit)
    • Error handling (service failures -> 500)
    • Histogram verification (registration, sample observation, Prometheus exposition format, count increments)

Verification Results

npx jest --no-coverage tests/rate-limit.test.ts
- 12/12 passed

npx eslint src/routes/rate-limit.ts src/metrics/registry.ts
- Clean (no lint errors)
Acceptance Criteria Status
Histogram registered with explicit buckets and route label rate_limit_request_duration_seconds with [0.01..10] buckets, route and status labels
Latency captured for all requests including rejected ones Middleware registered before requireAdmin on the router
Prometheus exposition format works Verified via register.metrics()
90% test coverage on changed lines 12 tests covering all code paths

@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@Ajibola6921 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Adds a `rate_limit_request_duration_seconds` histogram to track request
latency for the /api/rate-limit endpoint, segmented by route and status
code, with explicit buckets. The middleware is registered on the router
before auth so that latency for rejected requests is also captured.

Closes Predictify-org#642
@Ajibola6921
Ajibola6921 force-pushed the fix/rate-limit-histogram-642 branch from f7cfa03 to 9f89774 Compare July 28, 2026 13:20
@greatest0fallt1me
greatest0fallt1me merged commit 8c975e7 into Predictify-org:main Jul 28, 2026
@greatest0fallt1me

Copy link
Copy Markdown
Contributor

LGTM — thoughtful approach, thanks for taking this on!

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.

Add prometheus latency histogram for /api/rate-limit [b#068]

2 participants