Prometheus metrics, /metrics & /health endpoints, DB instrumentation, pool monitor, user-activity middleware, Prometheus rules and Grafana dashboard #84
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
) This PR adds a minimal, opinionated observability layer to the project so we can collect query performance metrics, monitor connection pools, track user activity, expose system health, and provide Prometheus alerting and a starter Grafana dashboard.
What I changed (summary)
Added a Prometheus-backed metrics collector with histograms/counters/gauges and a simple MetricsCollector class.
app/lib/monitoring/metrics.ts
Exposed metrics and health endpoints:
app/api/metrics.ts
app/api/health.ts
DB instrumentation wrapper to measure query duration and status:
app/lib/db/instrumentedQuery.ts
Connection pool monitor to periodically push pool
Related Issue
Closes #80
How to install & test locally
Install runtime dependency and dev types: npm install prom-client npm install --save-dev @types/express @types/node
Note: @types/prom-client does not exist — do NOT try to install it. Instead either upgrade prom-client (recent versions bundle types) or add a minimal declaration file:
Create types/prom-client.d.ts containing: declare module "prom-client";
Run the TypeScript check: npx tsc --noEmit
Wire into your Express app:
Mount metrics and health routers (e.g., app.use(metricsRouter); app.use(healthRouter);)
Wrap DB calls with instrumentedQuery or patch pool.query
Start pool monitor with a getPoolStats function for your DB pools
Verify:
curl http://localhost/:/metrics → should show mcp_ prefixed metrics
curl http://localhost/:/health → should return basic health JSON
Simulate queries and check histogram/counter updates
Notes and important considerations
Protect /metrics in production (network-only access or strip sensitive labels). High-cardinality labels (raw SQL strings, full user IDs) will explode Prometheus series — prefer canonicalized query names and anonymized user IDs.
If prom-client types are missing, add types/prom-client.d.ts as described or update prom-client to a version that ships types.
I included a sample Prometheus rules file for alerting and a small Grafana dashboard JSON as a starting point — adjust thresholds/labels for your environment.