Appearance

Metrics, logs, and traces

Connect a latency spike to affected requests using metrics, logs, and traces.

What you will understand

  • Choose a metric for a symptom and avoid labels that create a series per request.
  • Explain the roles of Prometheus, Grafana, and OpenTelemetry in the example.
  • Use a trace ID and a time window to connect related evidence safely.

Builds on: Debug network and storage failures

Choose the right signal for the symptom

Checkout requests have become slower for the last ten minutes. You want to know how many requests are affected, which operation is slow, and what happened during one of those requests. Metrics are numerical measurements over time. They can show higher latency for a particular service. Logs are event records. Structured logs include fields you can filter, such as timestamp, route, status, and trace ID. Traces follow one request through its operations, represented as spans, so you can see how long checkout spent calling inventory or waiting for a database.

One slow example may explain a particular request. Aggregate metrics help establish how widespread the problem is. Try the metrics pipeline scene to see what happens when a scraper cannot reach the app or the app has yet to expose a metric.

Follow one measure from app to dashboard

A metric has to be exported, collected, stored and queried before a chart or alert can show it.

  1. Instrument the operation

    The application exports a request counter or latency histogram. Installing a dashboard does not create this instrumentation.

  2. Scrape and store samples

    Prometheus reads the endpoint at configured intervals and stores series. Each distinct label set is a separate series with a cost.

  3. Aggregate a useful query

    Grafana queries its data source using the intended labels and time range. Raw counters normally need rate-style treatment for throughput.

  4. Alert on a meaningful change

    An alert evaluates a sustained symptom and links to investigation. Keep request IDs and unbounded user labels out of metric dimensions.

Illustrative scrape-based metrics stack; installation is optional and metric names require instrumentation.

Inspect the metric scrape

Follow a counter sample from an app endpoint into Prometheus, then break the scrape path and compare the query results.

1 / 4

From Prometheus scraperTo App endpoint

Scrape the app endpoint

The configured job sends GET /metrics to the app Service. The response includes the example counter at 42.

Scrape response
Before
Waiting for the first scrape
After
app_requests_total 42
Scrape the app endpoint

The configured job sends GET /metrics to the app Service. The response includes the example counter at 42.

The lab serves a fixed counter. The trace compresses the scrape interval and groups HTTP transport, parsing, and storage.

What does up=0 tell you about the number of requests the app handled?
Reveal prediction answer

It tells you the scrape failed. You need a successful scrape and an appropriate application metric to measure traffic.

Query the counter first, then remove the app Service and query up after the next scrape.

Scrape and query the example counter
Inspect YAML or commands

Scrape and retain

scrape_interval: 15s

Query the correct aggregate

sum by (service) (rate(http_requests_total[5m]))
Explore another case

Open Inspect the metric scrape to follow one sample into Prometheus and compare it with Prometheus’s own up metric when the next scrape fails.

Collect and label application metrics

Prometheus typically scrapes instrumented endpoints on a schedule, stores samples as time series, and supports queries over their values. The example scrapes an application metrics endpoint on port 9090 inside the cluster. Save it as part of a Prometheus configuration only after you have a reachable api Service and a metrics endpoint.

scrape_configs:
  - job_name: application
    scrape_interval: 15s
    static_configs:
      - targets: ["api.learning.svc.cluster.local:9090"]
        labels:
          environment: training

A metric name and its label values identify a series. Using a request ID or email address as a label makes a new series for each value and can become expensive. Label by bounded dimensions such as service and operation, and keep request-specific IDs in logs and traces. The first query shows requests per second by service using a five-minute window if that counter and label exist. The second measures CPU consumption by Pod when a collector exposes the named metric. Grafana can plot the results from a configured data source.

# Example application metric: only works if instrumentation exports it.
sum by (service) (rate(http_requests_total[5m]))
# A resource signal, when a suitable collector exports this metric.
sum by (pod) (rate(container_cpu_usage_seconds_total[5m]))

Follow one metric through a real scrape

Source lab-env.sh in the kind lab. This exercise serves a fixed counter so the sample value is easy to recognize. Prometheus runs in an isolated namespace and uses the text fallback for this simple HTTP fixture.

kubectl --context kind-kv-lab create namespace kv-metrics-trace
kubectl --context kind-kv-lab -n kv-metrics-trace run app --image=busybox:1.37.0 --restart=Never \
  --command -- sh -c 'mkdir -p /www; printf "%s\n" "# TYPE app_requests_total counter" "app_requests_total 42" >/www/metrics; exec httpd -f -p 8080 -h /www'
kubectl --context kind-kv-lab -n kv-metrics-trace expose pod app --port=8080
kubectl --context kind-kv-lab -n kv-metrics-trace create configmap scrape --from-file=prometheus.yml=/dev/stdin <<'YAML'
scrape_configs:
  - job_name: app
    scrape_interval: 5s
    fallback_scrape_protocol: PrometheusText0.0.4
    static_configs:
      - targets: ["app:8080"]
YAML
kubectl --context kind-kv-lab -n kv-metrics-trace apply -f - <<'YAML'
apiVersion: v1
kind: Pod
metadata:
  name: prometheus
  labels:
    app: prometheus
spec:
  containers:
    - name: server
      image: prom/prometheus:v3.5.0
      args: ["--config.file=/etc/trace/prometheus.yml", "--storage.tsdb.path=/prometheus"]
      volumeMounts:
        - name: config
          mountPath: /etc/trace
  volumes:
    - name: config
      configMap:
        name: scrape
YAML
kubectl --context kind-kv-lab -n kv-metrics-trace expose pod prometheus --port=9090

Run this query until it returns 42.

kubectl --context kind-kv-lab -n kv-metrics-trace exec app -- \
  wget -qO- 'http://prometheus:9090/api/v1/query?query=app_requests_total'

Remove the app Service to break the scrape path while leaving the app Pod available as a query client.

kubectl --context kind-kv-lab -n kv-metrics-trace delete service app

Run the next query until it returns 0 for job="app".

kubectl --context kind-kv-lab -n kv-metrics-trace exec app -- \
  wget -qO- 'http://prometheus:9090/api/v1/query?query=up'
kubectl --context kind-kv-lab delete namespace kv-metrics-trace

Follow a slow request across services

Find the time range of the checkout spike. Pick a slow request from a trace and follow its spans to the slow dependency. If the app records the trace ID in structured logs, search that ID to find the matching error. OpenTelemetry provides instrumentation and context propagation so spans can share a trace across services. An OpenTelemetry Collector can receive, process, and export telemetry to the destination you configure. The second scene shows why carrying trace context across a request matters.

Follow one slow checkout through its dependencies

A shared trace ID connects the request’s spans and lets you find related structured logs.

  1. Checkout serviceStart a request span
    Trace contextThe request has a trace ID and a parent span.
  2. Checkout clientPropagate the trace context
    Inventory serviceThe dependency can create a span in the same trace.
  3. Inventory serviceRecord a relevant log with the trace ID
    Logs and tracesThe request’s log event can be found from its trace.
  4. OpenTelemetry CollectorExport configured telemetry
    Observability backendThe viewer can compare dependency duration and errors for the request.
Follow the sequence

A shared trace ID connects the request’s spans and lets you find related structured logs.

Instrumented services propagate compatible context; traces can be sampled.

Explore another case
kubectl top pods -n learning
kubectl logs deployment/api -n learning --since=10m --tail=100
kubectl get events -n learning --sort-by=.metadata.creationTimestamp
# Inspect only the telemetry components actually installed in the lab.
kubectl get pods -A

Begin with the debugging and observability lab. Use logs and Events on the baseline, and run kubectl top only if a resource metrics pipeline is installed. Prometheus and tracing require their own components and app instrumentation.

Check whether the telemetry pipeline works

Missing spans can mean sampling or missing propagation. Missing metric samples can mean a failed scrape. Inspect the telemetry pipeline before treating a blank graph as a healthy system. Keep credentials and personal data out of application output, labels, and trace attributes.

Check your understanding

Error rate doubled after a release. Which signal can help find the failing operation without reading the request payload?

Use metrics to locate the affected service and time range. Then find a representative failed trace or structured log entry and inspect operation names, response codes, and trace IDs. Those fields usually narrow the question without capturing the body.

Primary documentation

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu