BE9: Testing and running in production

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Logs, metrics and traces

🎯You'll be able to

Add structured logs, a few key metrics and request IDs, so you can tell what a live service is doing and why a request was slow.

🏠The everyday picture

A hospital has three ways to watch a patient: the notes nurses write (logs), the monitor showing heart rate and blood pressure over time (metrics), and the record of every department the patient visited and how long each took (a trace). Each answers a different question.

📘What it really is

📝A worked example

Input: a customer reports that ordering took 4 seconds at 19:42.

Steps (an illustrative investigation):

  1. Metrics: the POST /v1/orders p95 graph shows a spike from 19:40 to 19:45; error rate is normal.
  2. Traces: a slow trace from 19:42 shows 3.8 s in the span SELECT ... FROM menu_items WHERE id = ANY(...).
  3. Logs: filtered by that trace's request ID, a warning shows the database pool was waiting for a free connection.

Output: the cause is pool exhaustion during a peak (BE3 Lesson 4), not the order code itself. In the kit, /health on the orders service already reports outbox_pending, a simple metric.

🌍Where it's used

📝Case scenario

A school results site was slow on results day, and the team spent three hours reading plain-text logs from 12 servers with no request IDs. After adding request IDs, RED metrics per route and tracing, a similar slowdown the next term was traced to one uncached query in under ten minutes.

🖱️Try it

Add a small middleware to the finished API that gives each request an ID (from an X-Request-Id header, or a new random one), returns it in the response, and logs one JSON line per request with method, path, status and milliseconds. Success: every log line has a request ID, and no line contains a token.

⚠️Common mistakes

🛠️For your project

Your capstone adds request IDs and one JSON log line per request to the API, and shows a sample in the demo.

✅Quick check

What are the RED metrics?

Show the answer

Rate, errors and duration, measured per route.

What does a trace show that logs don't?

Show the answer

How long each step of one request took across services, in one picture.

← PreviousContract tests between services
ProLearners Assist