Errors, Metrics, Traces — Know What's Happening
Monitoring
Three pillars of observability. Sentry for errors, Prometheus for metrics, OpenTelemetry for traces.
What you'll learn
- Capture uncaught errors with Sentry
- Expose Prometheus metrics
- Send traces via OpenTelemetry
Production without monitoring is flying blind. Three pillars:
- Logs — what happened (previous lesson)
- Metrics — how often, how fast
- Traces — what called what
Plus: error tracking — basically free in setup, huge in value.
Error Tracking — Sentry
The biggest ROI tool for any new Express app.
npm install @sentry/node import * as Sentry from "@sentry/node";
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 0.1,
environment: process.env.NODE_ENV,
});
// must be the FIRST middleware
Sentry.setupExpressErrorHandler(app);
// ... your routes
// Sentry hooks into your error middleware
app.use((err, req, res, next) => {
res.status(500).json({ error: { message: "internal" } });
}); Every uncaught exception goes to Sentry with full context — stack trace, request URL, user info, breadcrumbs. You’ll catch bugs before users report them.
Metrics — Prometheus
npm install prom-client import client from "prom-client";
client.collectDefaultMetrics(); // node + process metrics
const httpDuration = new client.Histogram({
name: "http_request_duration_seconds",
help: "duration",
labelNames: ["method", "route", "status"],
buckets: [0.01, 0.05, 0.1, 0.3, 1, 3, 10],
});
app.use((req, res, next) => {
const end = httpDuration.startTimer();
res.on("finish", () => {
end({
method: req.method,
route: req.route?.path ?? "unknown",
status: res.statusCode,
});
});
next();
});
app.get("/metrics", async (req, res) => {
res.set("content-type", client.register.contentType);
res.end(await client.register.metrics());
}); Scrape /metrics from Prometheus / Grafana. Useful dashboards:
RPS, p95 latency, error rate.
Traces — OpenTelemetry
For multi-service apps:
npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node import { NodeSDK } from "@opentelemetry/sdk-node";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
const sdk = new NodeSDK({
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start(); Auto-instrumentation hooks Express, fetch, pg, ioredis, and many more out of the box. Ship traces to Jaeger / Honeycomb / Datadog / your provider.
Uptime Monitoring
Separate from your infra (so you find out when your infra is on fire):
- BetterStack
- UptimeRobot
- Pingdom
Configure them to hit /healthz every 30s and page on failure.
What To Watch
The four golden signals (from Google SRE):
- Latency — p50, p95, p99
- Traffic — RPS
- Errors — 4xx, 5xx counts
- Saturation — CPU, memory, DB pool, queue depth
Grafana dashboards built around these tell you the system’s health at a glance.
Pragmatic Minimum
For a small project, day one:
- Sentry for errors (free tier is generous)
- Uptime monitor for liveness
- Platform-native metrics (Render / Fly / Vercel ship basics)
Add Prometheus + tracing when you actually need them.
Debugging →