How to monitor Node.js application health
· Category: Node.js
Short answer
Expose a health check endpoint, collect metrics with libraries like prom-client, and integrate with monitoring tools like Prometheus, Grafana, or Datadog.
Steps
- Create a
/healthroute that checks database connectivity and returns 200 or 503. - Instrument your app with
prom-clientto track request latency, error rates, and event loop lag. - Scrape metrics with Prometheus and build dashboards in Grafana.
- Set up alerts for high memory usage, error spikes, and response time degradation.
- Use APM tools like New Relic or Elastic APM for distributed tracing.
Tips
- Health checks should be lightweight and not perform expensive operations.
- Separate liveness probes (is the app running?) from readiness probes (is it ready to serve traffic?).
Common issues
- Including dependency health checks that timeout can cause cascading failures in orchestrators.
- Over-instrumentation adds overhead that degrades performance under load.