Skip to content

Expert · Performance and reliability · topic 43 of 54 · 30 min

Logging and monitoring

Explanation

Once your app is live, you cannot watch it with your eyes. Logs record what happened, metrics count how the app is doing (requests per second, error rate, slow responses), and traces follow one request through every service. Good teams write structured JSON logs with a request id, never log secrets, add a /health check, and set alerts so a person is told when something breaks.

Three ways to see inside a running app

SignalWhat it isAnswers questions like
LogsLines of text or JSON, one for each eventWhat exactly happened to order 9001 at 14:02?
MetricsNumbers measured over timeHow many requests per second? How many errors?
TracesThe path of one request through all services, with timingsWhich step made this checkout take 4 seconds?
Think of it like this: Think of a patient in a hospital. The nurse's notes are logs: what happened and when. The heart-rate and BP monitor shows metrics: numbers over time, with an alarm. The patient's file that follows them from reception to X-ray to doctor is a trace: one journey through many departments.

Log levels

  • debug: very detailed, for developers. Usually off in production.
  • info: normal events. "Order 9001 placed".
  • warn: something odd but handled. "Payment retry 2 of 3".
  • error: something failed. "Could not reach SMS provider".
  • fatal: the app cannot continue and is stopping.

Structured logs and request ids

A structured log is a log line written as JSON with named fields, instead of a free sentence. Machines can search and filter it: "show all error lines for userId 42 in the last hour".

json
{"level":"error","time":"2026-03-14T08:21:05.120Z","msg":"payment failed","requestId":"req-7f3a","orderId":9001,"provider":"upi","durationMs":5020}
One structured log line. Every field can be searched.

A request id is a unique id given to each incoming request, often sent back in a response header too. Every log line written while handling that request includes it. When a user reports a problem, you search one id and see the whole story.

Careful: Never log passwords, OTPs, tokens, API keys, full card numbers or Aadhaar numbers. Logs are copied to many places and read by many people. Log only the last 4 digits of a card if you need it, and drop or mask secret fields automatically.

Metrics that matter

Three numbers tell you most of what you need. Requests per second (traffic): how busy the app is. Error rate: the share of requests that fail, usually counted as 5xx responses. Latency: how long responses take.

For latency, an average hides problems. p95 latency means 95% of requests were faster than this number, and 5% were slower. If p95 is 2 seconds, one user in twenty waits 2 seconds or more, even if the average looks fine. To find it, sort all the times and pick the value at the 95% position.

Health checks, alerts and on-call

javascript
app.get('/health', async (req, res) => {
  try {
    await db.query('SELECT 1');           // can we reach the database?
    res.json({ status: 'ok' });
  } catch {
    res.status(503).json({ status: 'error', db: 'down' });
  }
});
A health check endpoint that load balancers and monitors can call every few seconds (runs on a server with Node.js and Express).

An alert is an automatic message (phone call, SMS, chat) sent when a metric crosses a line, for example "error rate above 2% for 5 minutes" or "/health failing". On-call means one team member is responsible for answering alerts during a period, taking turns. Alert only on things that need a human, or people start ignoring alerts.

Tip: Common tools: OpenTelemetry is an open standard and set of libraries for collecting traces, metrics and logs. Prometheus stores metrics and checks alert rules. Grafana draws dashboards. Sentry collects errors with their stack traces. Hosting platforms and clouds also have built-in log and metric views.

Watch the lesson

Logging and monitoring · 1/7

Logs Metrics Traces

When your app is live, you need ways to see inside it. There are three: logs, metrics and traces.

Prefer a video by a teacher? Find free videos on “logging monitoring observability explained logs metrics traces” on YouTube ↗

In real life: Slow UPI payments on a Saturday

On a Saturday evening, a shopping app's p95 checkout latency jumped from 800 ms to 6 seconds, and an alert called the on-call developer. The average was only 1.2 seconds, so the dashboard looked almost normal, but p95 showed one user in twenty was waiting a long time. She searched the logs for error and warn lines in the checkout route, picked one requestId, and saw the whole journey: the call to the payment provider was timing out and retrying. The trace confirmed it. The team switched to the backup provider and posted a status update, all within 20 minutes. No passwords or card numbers were in any log line.

Practice (0/3 done)

Task 1 · Code · Javascript

Write percentile(values, p) using the nearest-rank method: sort a COPY of the numbers ascending, rank = Math.ceil(p * n / 100), and return the value at position rank (counting from 1). If rank is 0 use 1. If values is empty return null. Do not change the original array.

Task 2 · Code · Javascript

Write logLine(level, msg, context, now) that returns ONE JSON line (a string). Keys in this order: level, time (now().toISOString()), msg, requestId (from context), then every other field of context in its order, EXCEPT fields whose lower-case name contains any word in SECRET_WORDS.

Task 3 · Practice

Plan monitoring for a food delivery API before launch. List what you will log, which metrics you will watch, what /health checks, and two alerts with their thresholds.

Quick quiz

Question 1

1.Which signal shows the path of ONE request through several services with timings?

Question 2

2.Why add a requestId to every log line?

Question 3

3.p95 latency is 1.8 seconds. What does that mean?

Question 4

4.Which of these is safe to put in a log line?

Question 5

5.What is the main job of a /health endpoint?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Rate limiting