Data and monitoring · Beginner · 14 min
Understand metrics, logs and traces and when to use each
By Padding · Updated 18 Nov 2026
Any web service · OpenTelemetry and Prometheus concepts · November 2026
You will have followed one failing checkout request through a metric, a log line and a trace, and written a one-page plan for which signal your own system should reach for first.
On this page
When something breaks in production, people often open whatever tool they used last. Sometimes that works. More often they scroll through thousands of log lines looking for a pattern a graph would have shown in a second, or stare at a graph that cannot tell them which customer was affected.
Metrics, logs and traces are three different records of the same events. Each answers a different kind of question, costs a different amount to keep, and breaks in a different way when you collect too much. Once you know which question each one answers, you know where to look first.
This tutorial follows one example all the way through: a shop's checkout starts failing at 14:02, and a customer's order A-10293 does not go through. You see that one request as a metric, a log line and a trace, then look at the costs of each and how OpenTelemetry links them. At the end you write a short plan for your own system. There are no commands to run; every check asks you something about a system you know.
Step 1 · Picture one request in all three signals
The checkout request travels from the browser to the shop's API, which loads the cart, then calls an outside card payment provider, here called CardPay. CardPay is having a bad afternoon and returns HTTP 502 errors. The API tries CardPay three times, then gives up and shows the customer an error.
That single event leaves three kinds of evidence:
- A metric: a number counted over time. The checkout error rate goes from about 0.4% to 7.8% in a few minutes. The metric does not know about order
A-10293; it only knows that more checkouts failed. - A log: a timestamped record the code wrote at the moment it happened. One line says the payment was declined, by which provider, with which status code, for which order.
- A trace: the path of one request through your system, broken into timed pieces called spans. It shows the whole checkout took 4.2 seconds, of which 3.8 seconds went on three attempts to call CardPay.
A short way to remember the difference: metrics tell you that something is wrong and how big it is, logs tell you what happened in one place, and traces tell you where in a chain of calls it happened and how long each part took.
Check it worked
In your document, write down one real incident or bug from your own system, even a small one. Next to it, write one sentence for each signal: what a metric would have shown, what a log line would have said, and what a trace would have shown. If you cannot fill in one of the three, note that; it is a gap you come back to in the last step.
Step 2 · Read the metric first when you need to know if something is wrong
A metric is a name, a set of labels and a stream of numbers. For the checkout, it might be a counter called checkout_requests_total with labels status="ok" or status="error". Your monitoring system stores how many requests had each status, sampled every 15 or 30 seconds, and works out a rate from it.
Because a metric is just numbers, it is small. Storing one more data point every 15 seconds costs almost nothing, which is why metrics are what you build alerts and dashboards on. In the example, the alert that fires at 14:04 is a rule on this metric: "error rate above 2% for 2 minutes".
What a metric cannot do is tell you about one request. It has already added order A-10293 into a total along with every other checkout. You will never get the order ID back out of it.
Good questions for a metric:
- Is the error rate higher than normal right now?
- How many requests per second are we handling, compared with last Tuesday?
- Is the 95th percentile response time creeping up week by week?
- Should someone be woken up?
Check it worked
For your own system, write down the one metric that would tell you most quickly that customers are affected. Name it and its labels, for example "orders failed per minute, by payment method". Then answer: does an alert exist on it today? If you do not know, write "unknown" and treat finding out as your first task.
Step 3 · Read the log when you need the details of one event
A log line is a record your code writes on purpose: "payment declined, provider cardpay, status 502, attempt 3, order A-10293". It carries the specific details a metric throws away, such as IDs, error messages and the values that led to a decision.
Logs are most useful when they are structured, meaning each line is a set of named fields (often JSON) rather than a sentence. With fields you can search for order_id=A-10293 or count every line where provider=cardpay and status=502. With free text you are left matching words and hoping the wording never changed. The tutorial on structured JSON logs in Python shows how to set this up.
The cost of logs grows with traffic. Every request may write several lines, each a few hundred bytes, and you pay to send, index and keep all of them. A busy service can produce gigabytes a day. That is why teams set log levels (debug lines off in production), keep detailed logs for days or weeks rather than months, and avoid logging inside tight loops.
Never put passwords, card numbers, access tokens or full personal records in a log line. Logs get copied to more places and kept by more people than your database, so treat them as semi-public.
Good questions for a log:
- Why did this specific order, user or job fail?
- What did the code decide, and with which inputs?
- Who changed this setting, and when?
Check it worked
Open the logs of a service you know and find one error line. Answer three questions about it in your document: can you tell which request or user it belongs to, can you search for it by a field rather than by words, and does it contain anything that should not be there, such as a token or an email address? Each "no" or "yes, sadly" is a concrete fix to add to your plan.
Step 4 · Read the trace when you need to know where the time went
A trace is made of spans. Each span is one unit of work, such as "handle POST /checkout", "load cart from database" or "HTTP POST to CardPay", with a start time, a duration, a status and some attributes. Spans nest: the checkout span is the parent, and the database and payment calls are its children. Drawn on a timeline, they make a waterfall.
In the example, the waterfall answers in one glance what the logs would make you piece together: the database was fine (0.3 seconds), and nearly all of the 4.2 seconds went on the payment call, which was tried three times and ended in a 502 each time. If CardPay had been slow without failing, the trace would still show it, while the error-rate metric would have stayed flat.
Traces earn their keep when a request crosses several services. Without them, each service's logs show only its own part, and you are left lining up timestamps by hand. With them, one view shows the whole chain.
Good questions for a trace:
- Why is this endpoint slow, and which part of it is slow?
- Which service in a chain of calls returned the error first?
- Are we making the same database query 40 times in one request?
Check it worked
Pick the slowest page or endpoint in your system. Write down, from memory or from your tools, the calls it makes: database queries, other internal services, outside APIs. Then answer: if it got twice as slow tomorrow, how would you find out which of those calls caused it? If the honest answer is "add timing logs and redeploy", tracing would save you that step.
Step 5 · Weigh cardinality, retention and cost
Each signal has its own way of getting expensive, and the fixes differ.
Cardinality in metrics
Cardinality is the number of distinct combinations of label values. Every combination is a separate time series that your metrics system stores and indexes. status with two values and payment_method with four values gives at most eight series. Add a user_id label with 200,000 users and you can have over a million series from one metric, which slows queries and can run your monitoring server out of memory. The Prometheus documentation warns against labels with unbounded values such as user IDs or email addresses.
The rule: labels on metrics should have a small, known set of values. Put IDs in logs and traces, where each record is expected to be different.
Retention
Retention is how long you keep data. Metrics are small, so keeping them for months is normal and lets you compare this December with last. Prometheus keeps 15 days of local data by default unless you change it. Detailed logs and traces are bulky, so most teams keep them for days or a few weeks and keep a summary metric for longer.
Sampling in traces
Recording a trace for every request on a busy service is costly, so tracing systems sample. Head sampling decides at the start of a request, for example keep 10% at random. It is cheap but may throw away the one failing request you needed. Tail sampling waits until a trace is complete and then decides, so it can keep every trace with an error or over 2 seconds and only a few of the healthy ones. It catches more of what matters but needs a component that holds traces in memory while it decides, such as an OpenTelemetry Collector.
Logs and labels
Log systems have a cardinality trap too. Grafana Loki, for example, indexes only a few labels per stream and advises keeping those low-cardinality; you search the rest of the line's contents at query time. Putting an order ID in a Loki label creates a new stream per order.
If you run services on prepaid or metered connections, as many teams across Africa and elsewhere do, remember that sending telemetry uses bandwidth as well as storage. Sampling traces and dropping debug logs at the source reduces both bills.
Check it worked
Look at one metric in your system and list its labels. For each label, estimate how many values it can take. Multiply them. Write the number down; if any label could grow without limit (IDs, URLs with IDs in them, free-text messages), mark it to move into logs or traces instead. Then write down how long you keep logs today, and whether anyone has ever needed one older than that.
Step 6 · See how OpenTelemetry ties the three together
The signals are most useful when you can move between them: from the spike on the graph to an example trace, from the trace to the log lines for that request. What makes that possible is shared context, and OpenTelemetry is the open standard most tools now use to carry it.
It works like this. When the checkout request arrives, the first service creates a trace ID, a random 32-character hex string such as 4bf92f3577b34da6a3ce929d0e0e4736. Every span in the request records that ID. When the API calls another service, it passes the ID along in an HTTP header called traceparent, defined by the W3C Trace Context standard, so the next service joins the same trace instead of starting a new one. This passing along is called context propagation.
Three links then become possible:
- Logs to traces. OpenTelemetry's logging integrations can add the current trace ID and span ID to each log record once you turn them on; how you do that depends on the language (in Python, for example, you enable the logging instrumentation or the log bridge). In the example, the "payment declined" line carries
trace_id=4bf9…4736, so one click takes you from the log line to the waterfall. - Traces to logs. From the slow payment span, you search the logs for the same trace ID and see every line written during that request, across every service.
- Metrics to traces. An exemplar is a trace ID attached to a single metric data point. Prometheus can store exemplars when you enable the feature, and Grafana can show them as dots on a graph. The dot on the error-rate spike in the diagram is an exemplar: a real failing request you can open.
OpenTelemetry also gives the three signals the same resource attributes, such as service.name and deployment.environment.name, so "checkout-api in production" means the same thing in every tool. The tutorial on tracing a Python web app with OpenTelemetry sets this up in code.
Check it worked
Find one error log line in your system and answer: does it contain a trace ID or a request ID? If it does, try searching your other tools for that ID and note which ones find it. If it does not, write "add trace IDs to logs" as an item in your plan. Then check whether your services pass a traceparent (or any request ID) header when they call each other.
Step 7 · Decide which signal to reach for first
Most investigations use all three, but the first one you open decides how fast you get to an answer. Start from the question, then follow the shared IDs to the next signal.
The same table in words:
- Is anything broken right now? Metric first, then follow an exemplar into a trace.
- Should this wake someone up? Metric, with an alert rule on it. Do not alert on individual log lines unless the event is rare and always serious.
- Why did this one order, user or job fail? Log first, searched by its ID, then the trace for that request.
- Why is this endpoint slow? Trace first, then a latency metric to see how many requests are affected.
- Which service in the chain failed? Trace first, then that service's logs.
- Who changed what, and when? Log (an audit log), then the metric around that time to see the effect.
- Is it getting worse over weeks or months? Metric alone; it is the only signal you keep for that long.
For the checkout incident, the path was: the error-rate alert fires (metric), the on-call engineer clicks an exemplar on the spike (trace), sees the payment span failing with 502 after three attempts, and opens the logs for that trace ID to read CardPay's error body (log). Three tools, one ID, a few minutes.
Check it worked
Write your one-page plan. It has three short sections, one per signal. Under each, write what you collect today, the one gap you found in the earlier checks, and the first change you will make. For example: "Metrics: we have CPU but no checkout error rate; add a counter by status and payment method. Logs: free text, no request ID; switch to JSON and add trace IDs. Traces: none; add OpenTelemetry to the API and the payment client." If you can read the plan aloud to a teammate and they know what to build first, it is done.
Next steps
Put the plan into practice one signal at a time:
- Start with metrics and alerts in Monitor a server with Prometheus and Node Exporter.
- Make your logs searchable by field in Write structured JSON logs in Python.
- Add spans and shared trace IDs in Trace a Python web app with OpenTelemetry.
- Turn the metric from your plan into a page your team watches in Build a Grafana dashboard.
Related tutorials
- How to Monitor a Linux Server with Prometheus and Node ExporterYou will install Node Exporter 1.12 and Prometheus 3.16 from the official release files on Ubuntu 26.04, run both as locked-down systemd services that listen on localhost only, and query your server's CPU, memory and uptime with PromQL.
- Write structured JSON logs in Python with structlogYou will switch a FastAPI app to structlog 26.1, ending with one JSON object per line that carries a request ID, the route, the OpenTelemetry trace ID and redacted secrets, plus readable output for development and jq queries that answer real questions.
- Trace a Python web app with OpenTelemetryYou will add OpenTelemetry tracing to a FastAPI app that calls a second service with httpx, with named resources, your own spans, events and recorded errors, exported first to the terminal and then to a Collector, with sampling set from environment variables.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials