Skip to main content
Welcome. In this lesson we explore AWS X-Ray for distributed tracing: what problems it solves, how traces are structured, common instrumentation strategies, sampling and cost control, and how annotations and metadata make traces searchable. Why distributed tracing matters
  • Modern microservice architectures execute a single user action across many services. Observability needs to show not only metrics and logs, but the exact path a request took and where latency or errors occurred.
  • Traditional monitoring (for example, CloudWatch metrics and isolated logs) gives useful signals, but distributed tracing provides the end-to-end view required to locate performance bottlenecks and correlate failures across services.
  • AWS X-Ray delivers end-to-end request traces, per-service performance breakdowns, and the ability to drill into specific operations to find root causes.
Trace components
  • Trace: the full journey of a single request across services.
  • Segment: data produced by a single service handling part of the trace (timing, errors, metadata).
  • Subsegment: finer-grained operations within a segment (for example, a downstream API call, a database query, or a cache lookup).
A presentation slide titled "Understanding Traces, Segments, and Subsegments" with three colored panels. Each panel defines: Trace (the entire journey of a request and service interactions), Segment (data from a single service like timing, metadata, errors), and Subsegment (granular operations and downstream calls).
Example: If a request traverses five services, the overall trace contains five segments (one per service). Each segment can contain multiple subsegments representing internal operations such as DB queries, cache hits, or downstream HTTP calls. Service maps and visualization
  • X-Ray automatically builds a service map showing nodes (services) and edges (calls) annotated by latency, error rates, and traffic volume.
  • Use the service map to quickly surface bottlenecks, unexpected dependencies, and cascading failures.
A slide titled "X‑Ray Service Map" showing three colored cards—Topology Discovery (globe icon), Latency Analysis (stopwatch icon), and Traffic Volume (up/down arrows)—each with a short description beneath. A © Copyright KodeKloud note appears at the bottom.
When to inspect the map:
  • Discover the highest-latency nodes.
  • Inspect flows with abnormal patterns or unexpected callers.
  • Identify services with elevated error rates for deeper trace inspection.
Instrumentation strategies
  • Managed AWS services: many require only enabling active tracing (a checkbox). Examples: Lambda and API Gateway.
  • Containers and VMs: for ECS, EKS, or EC2, you usually add an X-Ray SDK, run the X-Ray daemon as a sidecar/agent, or configure the runtime to propagate trace context.
  • Auto-instrumentation: available for many frameworks and SDKs; when present it reduces or eliminates code changes by automatically creating segments and subsegments.
Best practices:
  • Propagate trace context across service boundaries to maintain a single trace ID end-to-end.
  • Prefer auto-instrumentation where possible, and add manual subsegments for critical internal operations (e.g., slow database queries).
A slide titled "Instrumentation Strategies" showing two columns: Auto‑Instrumentation (listing AWS Lambda, API Gateway, App Runner, Elastic Beanstalk) and Manual Instrumentation (listing EC2 instances, ECS containers, EKS pods) with corresponding AWS icons.
Sampling and cost control
  • X-Ray has ingestion costs (for example, typically dollars per million traces). Use sampling to limit data volume while retaining actionable visibility.
  • Common sampling patterns:
    • Trace 100% of error traces.
    • Trace 100% of requests that exceed a latency threshold (for example, > 2000 ms).
    • Use environment-based sampling (higher in staging/dev, lower in production).
    • Apply path-specific sampling for critical flows (checkout, payment, etc.).
A presentation slide titled "Sampling Rules for Cost Control" showing a side-by-side comparison of "Without Sampling" (gray) and "With Sampling" (blue). The left lists "Every request traced" and "Expensive + overwhelming," while the right lists "Only representative requests traced" and "Maintains visibility."
A simple sampling strategy:
  • Trace the first request(s) in a period at 100% to build representative samples, then sample 1 out of N subsequent requests (for example, 1 out of 20).
  • Always ensure full coverage for errors and slow responses to avoid missing critical incidents.
A presentation slide titled "Sampling Rules for Cost Control" that lists four guidelines: sample 100% of errors, sample 100% of slow requests, use environment-based rates, and apply path-specific sampling, each with a short description.
Annotations vs. metadata
  • Annotations are indexed, searchable key/value pairs attached to traces. Use annotations for fields you will filter on frequently (for example, customer_tier, transaction_id, payment_method, feature_flag).
  • Metadata is additional contextual information stored with traces but not indexed. Use metadata for large or rarely searched payloads (full request/response bodies, configuration snapshots, large arrays) that are useful when inspecting a trace but should not be used for filtering.
A slide titled "Annotations and Metadata" comparing two boxes: the left explains annotations as searchable key-value pairs used for filtering and indexed in X‑Ray, and the right describes metadata as extra context stored with traces that is not indexed and is display-only.
Example annotation payload:
Example metadata payload:
Guidelines:
  • Keep indexed annotations narrow and low-cardinality to control indexing costs and query performance.
  • Use metadata for verbose context that helps root cause analysis during trace inspection.
AWS service integrations
  • Many AWS services integrate with X-Ray. When supported, the SDKs and service integrations create subsegments and propagate trace context automatically (for example, calls to DynamoDB, S3, SNS, etc.).
  • These integrations often require minimal configuration and let you correlate a single request across services using the trace ID.
An infographic titled "AWS Service Integration" showing columns of AWS service icons grouped by category. It lists services under Compute, Application, Data Stores, Messaging, and Other Services (e.g., Lambda, API Gateway, DynamoDB, SNS, S3).
A slide titled "AWS Service Integration" explaining "How Integration Works" with two highlighted features: "Automatic Subsegments" and "Trace ID Propagation." The subtext notes SDKs create subsegments automatically (no code changes) and trace context flows across services automatically.
Using X-Ray for root cause analysis (RCA)
  • Combine the service map, a filtered trace list, and detailed trace inspection for effective RCA.
  • X-Ray enables correlation between slow or error traces and related CloudWatch logs and metrics, making it easier to find the underlying cause.
  • Drill down from a failing request to the segment/subsegment that introduced latency or an exception, then use annotations/metadata to retrieve contextual data (transaction ID, user, feature flag).
Use cases summary
  • Typical X-Ray use cases:
    1. Request flow tracing — end-to-end visualization of a request across services.
    2. Latency analysis — identify which components add the most delay.
    3. Dependency mapping — visualize and analyze service topology.
    4. Distributed debugging and root cause analysis — correlate traces with logs and metrics.
A presentation slide titled "X‑Ray Use Cases" showing four numbered cards. The cards list: Request Flow Tracing, Latency Analysis, Dependency Mapping, and Distributed Debugging with brief descriptions of each.
Next steps
  • Enable X-Ray on a small, non-critical service first (for example, a development Lambda or a staging API Gateway) to view traces and service maps.
  • Configure sampling rules to balance cost and visibility, and add a few targeted annotations (transaction ID, customer tier, feature flag) to make trace searches efficient.
  • Integrate trace IDs into logs to correlate X-Ray traces with CloudWatch logs and metrics.
Start with a low-cost sampling strategy: trace all errors and slow requests, and sample a small percentage of normal traffic. Add indexed annotations only for fields you will query frequently to keep costs and cardinality manageable.
Links and references

Watch Video