Skip to content

feat(admin): add Prometheus-backed /admin/metrics SSE endpoint - #662

Merged
cameri merged 3 commits into
cameri:mainfrom
Ferryx349:ADMIN-CONSOLE-2-SSE
Jul 9, 2026
Merged

feat(admin): add Prometheus-backed /admin/metrics SSE endpoint#662
cameri merged 3 commits into
cameri:mainfrom
Ferryx349:ADMIN-CONSOLE-2-SSE

Conversation

@Ferryx349

Copy link
Copy Markdown
Collaborator

Description

Add GET /admin/metrics as an authenticated SSE stream for the admin console.

Note: This PR is stacked on Phase 1 until #641 merges to main.

Related Issue

Fixes - #655

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my code changes.
  • I added a changeset, or this is docs-only and I added an empty changeset.
  • All new and existing tests passed.

@changeset-bot

changeset-botBot commented Jul 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 18d6f07

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
nostreamMinor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Ferryx349
Ferryx349 marked this pull request as draft July 1, 2026 04:18
Comment threadsrc/routes/admin/index.ts Fixed
Comment threadsrc/routes/admin/index.ts Fixed
router.post('/login', adminLoginRateLimitMiddleware, json(), withAdminController(createPostAdminLoginController))
router.get('/session', adminRateLimitMiddleware, adminAuthMiddleware, withAdminController(createGetAdminSessionController))
router.get('/health', adminRateLimitMiddleware, adminAuthMiddleware, withAdminController(createGetAdminHealthController))
router.get('/metrics', adminRateLimitMiddleware, adminAuthMiddleware, withAdminController(createGetAdminMetricsController))
@coveralls

coveralls commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 67.674% (-0.07%) from 67.747% — Ferryx349:ADMIN-CONSOLE-2-SSE into cameri:main

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a disabled-by-default admin API surface including an authenticated SSE endpoint (GET /admin/metrics) that periodically streams a cluster-wide metrics snapshot sourced from Prometheus, alongside supporting admin auth/session utilities and OTEL metric instrumentation.

Changes:

  • Introduces /admin/login, /admin/session, /admin/health, and /admin/metrics (SSE) routes guarded by enablement, auth, and admin-specific rate limiting.
  • Adds Prometheus client + metrics snapshot aggregation (PromQL) plus dependency health (DB/Redis) folded into each snapshot.
  • Instruments event acceptance/rejection and WebSocket connection counts with OpenTelemetry metric instruments.

Reviewed changes

Copilot reviewed 50 out of 50 changed files in this pull request and generated 4 comments.

Show a summary per file
FileDescription
test/unit/utils/prometheus-client.spec.tsUnit coverage for Prometheus URL handling, scalar parsing, and instant-query behavior.
test/unit/utils/http.spec.tsAdds tests for new isSecureRequest() behavior behind trusted proxies.
test/unit/utils/admin-session.spec.tsUnit coverage for admin session token/cookie helpers and TTL fallback behavior.
test/unit/utils/admin-rate-limit.spec.tsUnit coverage for admin/login scoped rate limiting and key construction.
test/unit/utils/admin-metrics.spec.tsUnit coverage for metrics snapshot composition and interval parsing.
test/unit/routes/admin.spec.tsRouter-level tests for admin enablement, auth, login, health, and metrics wiring.
src/utils/prometheus-client.tsPrometheus base URL resolution, instant query execution, and scalar parsing.
src/utils/http.tsAdds isSecureRequest() helper for Secure-cookie decisions behind trusted proxies.
src/utils/admin-session.tsSigned admin session tokens + cookie header construction.
src/utils/admin-rate-limit.tsAdmin/login scoped rate limit decision logic using existing limiter.
src/utils/admin-password.tsPassword hashing + verification utilities (scrypt).
src/utils/admin-metrics.tsPromQL snapshot aggregation + SSE interval configuration.
src/utils/admin-health.tsDB/Redis dependency health snapshot for admin endpoints.
src/telemetry/event-metrics.tsOTEL counters/gauges updates for event results and WS connection tracking.
src/schemas/admin-login-schema.tsZod schema for admin login request body.
src/routes/index.tsMounts the admin router at /admin.
src/routes/admin/index.tsDefines admin routes and middleware ordering (rate limit, enablement, auth).
src/handlers/request-handlers/with-admin-controller-request-handler.tsAdmin controller wrapper that normalizes errors to JSON 500.
src/handlers/request-handlers/admin-rate-limit-middleware.tsExpress middleware applying admin-specific rate limiting.
src/handlers/request-handlers/admin-enabled-middleware.tsFeature gate middleware returning 404 when admin is disabled.
src/handlers/request-handlers/admin-auth-middleware.tsAuth middleware using the admin auth provider and returning 401/500 on failure.
src/handlers/event-strategies/vanish-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/timestamp-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/replaceable-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/parameterized-replaceable-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/group-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/gift-wrap-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/ephemeral-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/delete-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-strategies/default-event-strategy.tsSwitches to telemetry-aware command result emission.
src/handlers/event-message-handler.tsSwitches to telemetry-aware command result emission; small control-flow formatting tweak.
src/factories/controllers/post-admin-login-controller-factory.tsWires login controller with auth provider.
src/factories/controllers/get-admin-session-controller-factory.tsWires session controller with auth provider.
src/factories/controllers/get-admin-metrics-controller-factory.tsWires metrics SSE controller.
src/factories/controllers/get-admin-health-controller-factory.tsWires health controller.
src/factories/admin-auth-provider-factory.tsFactory for the password-based admin auth provider.
src/controllers/admin/post-login-controller.tsDelegates login handling to the auth provider.
src/controllers/admin/get-session-controller.tsReturns authenticated session info (expiresAt).
src/controllers/admin/get-metrics-controller.tsSSE controller that streams periodic admin metrics snapshots.
src/controllers/admin/get-health-controller.tsReturns dependency health snapshot.
src/admin/password-admin-auth-provider.tsImplements password login + session cookie issuance + request auth.
src/adapters/web-socket-adapter.tsAdds WS connection opened/closed metric tracking.
src/@types/settings.tsExtends settings types for admin config + admin limits.
src/@types/admin.tsDefines the IAdminAuthProvider interface.
resources/default-settings.yamlAdds default admin limits and default disabled admin settings.
docker-compose.ymlAdds Prometheus URL + SSE interval env wiring for local compose.
.env.exampleDocuments new Prometheus and SSE interval environment variables.
.changeset/admin-metrics-sse-endpoint.mdChangeset for the admin metrics SSE endpoint.
.changeset/admin-metrics-handler-instrumentation.mdChangeset for OTEL instrumentation additions.
.changeset/admin-console-phase-1.mdChangeset for the admin API foundation (enablement/auth/session/health).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadsrc/utils/prometheus-client.ts
Comment threadsrc/controllers/admin/get-metrics-controller.ts
Comment threadtest/unit/utils/prometheus-client.spec.ts
Comment threadtest/unit/utils/admin-metrics.spec.ts Outdated
@Ferryx349
Ferryx349 marked this pull request as ready for review July 4, 2026 02:13
@Ferryx349

Copy link
Copy Markdown
CollaboratorAuthor

"Missing rate limiting" on admin routes, likely seems a false positive.

Rate limiting is already in place:-

Router-level :- rateLimiterMiddleware (connection limits)
Per-route:- adminRateLimitMiddleware / adminLoginRateLimitMiddleware before auth
Backed by Redis via isAdminRateLimited() (same approach as invoice/admission controllers).

CodeQL doesn’t recognize our custom middleware, it does only for packages like express-rate-limit.

@cameri should we dismiss this as false positive, or would u prefer something else.
Happy to go ahead with whatever you prefer.

@Ferryx349
Ferryx349force-pushed the ADMIN-CONSOLE-2-SSE branch from 6503bef to abdb6abCompareJuly 4, 2026 11:32
@Ferryx349

Copy link
Copy Markdown
CollaboratorAuthor

This PR is now based on top of #654.

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Comment threadsrc/controllers/admin/get-metrics-controller.ts Outdated
Comment threadsrc/controllers/admin/get-metrics-controller.ts
Comment threadsrc/utils/prometheus-client.ts
Comment threadsrc/utils/prometheus-client.ts Outdated
@cameri
cameri merged commit 36d95cc into cameri:mainJul 9, 2026
14 of 18 checks passed
@Ferryx349
Ferryx349 deleted the ADMIN-CONSOLE-2-SSE branch July 10, 2026 04:32
Ferryx349 added a commit to Ferryx349/nostream that referenced this pull request Jul 10, 2026
Resolve conflicts by keeping dashboard SSE fallback snapshots and snapshot
timeout on top of the merged /admin/metrics SSE foundation from cameri#662.
Sign up for freeto 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.

5 participants

@Ferryx349@coveralls@cameri@github-advanced-security