Logging, Metrics and Tracing
Observability means you can tell what your backend is doing in production from the outside. It rests on structured logs, metrics and distributed traces. Interviewers ask how you would debug a slow or failing endpoint you cannot reproduce locally.
What you will learn
- Write structured JSON logs with levels and request ids
- Know the RED and USE metrics
- Explain distributed tracing and OpenTelemetry
- Add health and readiness endpoints
Key concepts
Structured logs
Logs as JSON objects ({ level, msg, reqId, userId, durationMs }) instead of free text, so a log platform can search and aggregate them. Pino is the common fast logger for Node.js.
RED metrics
For services: Rate (requests per second), Errors (failed requests), Duration (latency percentiles such as p50, p95, p99).
Distributed tracing
A trace id travels with a request through every service (W3C traceparent header). Each step records a span, so you can see which hop was slow. OpenTelemetry is the standard SDK.
Liveness vs readiness
Liveness: is the process alive (restart if not). Readiness: can it serve traffic now (database connected, not shutting down). Load balancers route only to ready instances.
Step by step
Debugging a slow endpoint
- Check the p95/p99 latency graph: when did it start, which route?
- Open a slow trace: is the time in the database, another service or your code?
- Search logs by request id for errors and timings
- Correlate with deploys, traffic spikes and database metrics
Examples
Pino with a request id
import pino from 'pino'
import pinoHttp from 'pino-http'
import { randomUUID } from 'node:crypto'
export const logger = pino({ level: process.env.LOG_LEVEL || 'info', redact: ['req.headers.authorization', 'req.headers.cookie'] })
app.use(pinoHttp({ logger, genReqId: (req) => req.get('x-request-id') || randomUUID() }))
app.get('/healthz', (req, res) => res.send('ok'))
app.get('/readyz', async (req, res) => {
try { await db.query('SELECT 1'); res.send('ready') } catch { res.status(503).send('not ready') }
})Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Add a request-duration histogram per route with prom-client.
- List five things you should never write to logs.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Why use p99 latency instead of the average?
Averages hide the slow tail. If 1% of requests take 5 seconds, the average still looks fine, but a page that makes 50 calls will hit the slow one most of the time. Percentiles show what real users feel.
What should never go into logs?
Passwords, tokens, cookies, full card numbers, OTPs, and personal data you do not need. Redact them in the logger configuration so a mistake cannot leak them.
Summary
Structured logs with request ids, RED metrics with percentiles, OpenTelemetry traces across services, and separate liveness and readiness checks let you debug production quickly.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus