Skip to article content
FrontendXLesson 12 / 16

Observability: logs, metrics and traces

Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.

intermediate · about 55 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Explain the roles of logs, metrics and traces and when to use each.
  • Emit structured logs with correlation identifiers.
  • Choose metrics that detect problems without exploding cardinality.

Overview

#

Observability is the ability to answer new questions about a running system without shipping new code. It rests on three signals: logs (discrete events), metrics (numeric aggregates over time) and traces (the path of one request across services).

Structured logs

Emit logs as structured objects with a consistent schema, not free-form strings. Include a request or trace identifier so you can correlate events.

snippet.js
logger.info({
  event: 'order_created',
  orderId: order.id,
  userId: order.userId,
  traceId: req.traceId,
  durationMs: elapsed,
});

Unstructured logs are hard to query; structured logs let you filter by field.

Metrics

Metrics are cheap to store and alert on. The RED method for services: Rate (requests per second), Errors (failed requests), Duration (latency distribution). The USE method for resources: Utilization, Saturation, Errors.

snippet.txt
http_requests_total{route="/orders",status="500"}
http_request_duration_seconds_bucket{route="/orders",le="0.5"}

Keep label values bounded. A label such as userId or a raw URL path creates unbounded cardinality and can take down the metrics backend.

Traces

A trace follows one request through every service and shows where time was spent. Spans nest: the root span covers the request, child spans cover database calls and outbound HTTP. Sampling keeps cost bounded; keep all errors and a fraction of successes.

Common pitfalls

  • Logging secrets or personal data.
  • Using a high-cardinality value as a metric label.
  • Alerting on a symptom with no runbook, so the alert is noise.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Write a structured log line for a failed login that includes the event name, a hashed user identifier, the client IP and the trace id, without logging the password or the raw email.

    starter.javascript
    // emit one structured log object
    

    Reveal the reference answer
    reference.javascript
    logger.warn({
      event: 'login_failed',
      userIdHash: hash(user.id),
      clientIp: req.ip,
      traceId: req.traceId,
      reason: 'bad_credentials',
    });

    Authored expected output: One structured log entry with the event, a hashed user id, client IP, trace id and reason, and no credential or raw email.

    Hashing the identifier keeps correlation possible while reducing exposure. The reason is a bounded enum, not free text, so it is queryable and safe.

  2. Exercise 2Predict

    A team adds a metric http_requests_total with a label userId. Traffic grows to millions of users. What breaks?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.txt
    http_requests_total{userId="..."}
    
    Choose the best answer
  3. Exercise 3Debug

    An alert fires for high error rate but the on-call engineer cannot tell which endpoint or dependency is failing. Logs are unstructured strings and there are no traces. What should be added first?

    starter.text
    alert: HighErrorRate
    # logs: "ERROR something went wrong"
    

    Reveal the reference answer
    reference.text
    Add structured logs with the route, status and trace id, and add per-route error metrics so the alert can name the failing endpoint. Add tracing to see which dependency is slow or failing. Start with the structured log fields and a route-labelled error counter, which are the cheapest to add and answer the first question.

    Authored expected output: The next alert names the failing route and dependency instead of a vague error.

    The alert detects a symptom; the missing signals are what explain it. Structured logs and route-labelled metrics close the gap fastest.

Common misconceptions

#
  • Metrics replace logs: metrics show that something changed, logs and traces show why.
  • Any label is fine in metrics: unbounded label values break the metrics store.

Follow-up practice

#
  • devops-rollback

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.