DVA-C02Chapter 36 of 101Objective 4.1

X-Ray Segments, Annotations, and Sampling

This chapter dives deep into AWS X-Ray segments, annotations, and sampling—core concepts that appear in roughly 15% of DVA-C02 exam questions under Domain 4: Troubleshooting. You will learn how X-Ray models distributed application requests as segments and subsegments, how to enrich traces with annotations for powerful filtering, and how sampling controls cost and performance. Mastery of these topics is essential for diagnosing performance bottlenecks and errors in serverless and microservice architectures. The exam tests not just definitions but the ability to choose the right tool (annotations vs. metadata vs. subsegments) and configure sampling rules correctly.

25 min read
Intermediate
Updated May 31, 2026

X-Ray: Detective's Case File System

Imagine a detective agency investigating a complex crime that spans multiple locations. Each detective (microservice) sends back reports about their part of the investigation. The agency's case file system (X-Ray) collects these reports into a single case file (trace). Each report includes a timestamp, location, and what was found (segment). To make reports more useful, detectives can attach sticky notes (annotations) with key facts like suspect height or vehicle color, and they can add detailed notes (metadata) with full descriptions. However, to avoid being overwhelmed, the agency decides to only file reports from every 1 in 10 cases (sampling). For high-priority cases (like a murder), they file every report (reservoir sampling). The agency also has a rule: if a case involves a VIP (like a celebrity), they always file full reports (sampling rules). This way, they get enough data to solve crimes without drowning in paperwork. In AWS X-Ray, segments represent work done by a service, annotations are key-value pairs used for filtering traces, metadata are arbitrary structured data, and sampling determines which requests are traced. The detective agency's case number is the trace ID, and the team lead's report is the root segment. Just as the agency can search case files by suspect height (annotation), X-Ray can filter traces by annotation values.

How It Actually Works

What Are Segments and Subsegments?

AWS X-Ray models a single request through a distributed application as a trace, which is composed of segments and subsegments. A segment represents the work done by a single service (e.g., an API Gateway endpoint, a Lambda function, or an EC2 instance) as it processes a request. Each segment has a start time, end time, duration, and optional error/fault/throttle flags. The first segment created for a trace is the root segment, which typically represents the entry point (e.g., API Gateway or an instrumented web server).

Subsegments break down the work within a segment into finer-grained units, such as downstream calls (e.g., DynamoDB queries, HTTP calls to another microservice), or internal logic (e.g., database query, file processing). Subsegments are nested inside a segment and can themselves have subsegments. They are critical for pinpointing where time is spent within a single service.

How X-Ray Tracks Segments

When a request enters your application, the X-Ray SDK creates a trace ID (a unique identifier, e.g., 1-5e3b3b3b-abcdef1234567890abcdef12) and a segment ID. The SDK sends the segment data to the X-Ray daemon, which buffers and forwards it to the X-Ray API. If the application makes downstream calls, the SDK propagates the trace header (containing the trace ID, parent segment ID, and sampling decision) via HTTP headers (e.g., X-Amzn-Trace-Id) or other protocols. The receiving service creates a new segment as a child of the calling segment, forming a trace tree.

Annotations vs. Metadata

Annotations are simple key-value pairs with string, number, or boolean values. They are indexed by X-Ray for filtering and searching traces. For example, you can annotate a segment with "customer_tier": "premium" and later search for all traces where customer_tier = premium. Annotations are limited to 50 per segment, and keys can be up to 512 characters, values up to 1024 characters (strings). They are ideal for high-cardinality attributes you need to query on.

Metadata are arbitrary key-value pairs that can hold any data type (including objects and arrays). They are not indexed and cannot be used for filtering. Metadata is stored alongside the segment but only appears in the raw segment details when you view a trace. Use metadata for debugging information that you don't need to search on, such as full request/response payloads, stack traces, or internal state.

Sampling: Controlling Trace Volume

X-Ray sampling determines which requests are traced. By default, X-Ray uses centralized sampling where the X-Ray service manages sampling rules. You can also define local sampling rules in the SDK configuration. The default sampling rate is 1 request per second (reservoir) and 5% of additional requests (rate). This means the first request each second is always traced, and then 5% of the remaining requests are traced. This is called reservoir and rate sampling.

Reservoir: A fixed number of traces per second (default 1). Once the reservoir is consumed for that second, no more reservoir traces are taken until the next second.

Rate: A percentage of requests beyond the reservoir that are traced (default 5%).

You can create custom sampling rules to override defaults for specific services, HTTP methods, URLs, or hostnames. Rules are evaluated in order of priority (lowest number = highest priority). The first matching rule is applied. If no rule matches, the default rule is used.

Sampling Decision Propagation

The sampling decision (trace or not) is made at the first service that receives the request (e.g., API Gateway or load balancer). That decision is propagated downstream via the trace header. Downstream services must honor the decision; they cannot override it to start tracing a request that was not sampled. However, they can generate subsegments even if the parent segment was not sampled? No—if the root segment is not sampled, no segments are sent. But the SDK will still propagate the decision. If a downstream service receives a request with a sampling decision of "not sampled," it should not create a segment. However, the SDK allows a service to generate a segment for its own work if it has a local sampling rule that overrides? Actually, the SDK follows the propagated decision; local rules only apply when the service is the origin of the trace (i.e., the first service). For incoming requests with a trace header, the SDK uses the decision in the header.

Subsegments and Async Work

Subsegments are created for synchronous calls automatically by the SDK (e.g., DynamoDB, S3). For asynchronous work (e.g., SQS, Lambda invocation), you must manually create subsegments using AWSXRay.beginSubsegment() and AWSXRay.endSubsegment(). The exam tests that you know subsegments can be created manually for custom instrumentation.

Lambda and X-Ray Integration

When you enable X-Ray tracing on a Lambda function, Lambda automatically sends trace data for the Lambda service segment (invocation, cold start, duration). Your function code can use the X-Ray SDK to add custom subsegments and annotations. Lambda also propagates the _X_AMZN_TRACE_ID environment variable containing the trace ID, parent segment ID, and sampling decision. The X-Ray daemon is automatically run inside the Lambda execution environment (since the runtime supports it).

API Gateway and X-Ray

API Gateway can be configured to pass trace headers and can also generate its own segment. When X-Ray tracing is enabled on an API Gateway stage, API Gateway sends a segment for each request. The segment includes the request path, method, and response status. API Gateway can also propagate the trace header to the backend integration.

Configuration Commands

To enable X-Ray on Lambda (using the AWS CLI):

aws lambda update-function-configuration --function-name my-function --tracing-config Mode=Active

To create a sampling rule:

aws xray create-sampling-rule --sampling-rule file://sampling-rule.json

Example sampling-rule.json:

{
  "RuleName": "MyRule",
  "ResourceARN": "*",
  "ServiceName": "*",
  "ServiceType": "*",
  "Host": "*",
  "HTTPMethod": "GET",
  "URLPath": "/api/*",
  "Priority": 10,
  "FixedRate": 0.1,
  "ReservoirSize": 5
}

Interacting with Other AWS Services

X-Ray integrates with many AWS services: Elastic Beanstalk, EC2, ECS, Lambda, API Gateway, SNS, SQS, DynamoDB, and more. These services can automatically send segments and propagate trace headers. For example, SQS can propagate the trace header from producer to consumer, allowing end-to-end tracing across async boundaries.

Common Pitfalls

Forgetting to enable X-Ray on all services in the path; missing segments break the trace tree.

Overusing metadata instead of annotations for filtering; metadata cannot be searched.

Not understanding that sampling decisions are made at the entry point and propagated; downstream services cannot override.

Exceeding the 50 annotations per segment limit; use metadata for extra data.

Using incorrect segment naming; segment names should reflect the service name for clarity.

Exam-Relevant Details

Default sampling: 1 reservoir per second, 5% rate.

Annotations are indexed; metadata is not.

Subsegments can be created manually for custom instrumentation.

The trace ID format: 1-{8 hex digits}-{24 hex digits}.

X-Ray daemon listens on UDP port 2000 by default.

Maximum segment document size: 64 KB.

Annotations: up to 50 per segment, key max 512 chars, value max 1024 chars (strings).

Metadata: no size limit per key? Actually, total segment size limit applies.

This depth of understanding is exactly what the DVA-C02 exam requires.

Walk-Through

1

Enable X-Ray on Lambda Function

Navigate to the Lambda function in the AWS Console, under Configuration > Monitoring and operations tools, set X-Ray tracing to Active. Alternatively, use the AWS CLI: `aws lambda update-function-configuration --function-name my-function --tracing-config Mode=Active`. This instructs Lambda to record segments for each invocation and to run the X-Ray daemon in the execution environment. The environment variable `_X_AMZN_TRACE_ID` will be set automatically, containing the trace ID, parent segment ID, and sampling decision. Without this, Lambda will not send any trace data, and downstream calls will not be correlated.

2

Instrument Code with X-Ray SDK

Add the X-Ray SDK to your function code (e.g., using `aws-xray-sdk` for Node.js, Python, Java, etc.). The SDK automatically captures subsegments for AWS SDK calls (DynamoDB, S3, etc.) and HTTP requests. For custom logic, wrap code with `AWSXRay.captureAsyncFunc()` or manually create subsegments using `AWSXRay.beginSubsegment()` and `AWSXRay.endSubsegment()`. Example in Node.js: `const AWSXRay = require('aws-xray-sdk-core'); const segment = AWSXRay.getSegment(); const subsegment = segment.addNewSubsegment('MyCustomWork'); // ... do work ... subsegment.close();`

3

Add Annotations and Metadata

Within your code, use `segment.addAnnotation('key', 'value')` to add searchable key-value pairs. For example, `segment.addAnnotation('customerId', '12345')` allows you to later find all traces for that customer. Use `segment.addMetadata('key', data, 'namespace')` to attach arbitrary data (e.g., full request payload) that is not searchable. You can add up to 50 annotations per segment. Annotations support string, number, and boolean values. Metadata can be any JSON-serializable object. The namespace is optional and defaults to 'default'.

4

Configure Sampling Rules

Define sampling rules in the X-Ray console or via AWS CLI to control which requests are traced. Rules are evaluated by priority (lowest number first). Each rule specifies a fixed rate (0.0 to 1.0) and a reservoir size (number of traces per second guaranteed). For example, to trace 10% of requests with a reservoir of 5 per second, set FixedRate=0.1 and ReservoirSize=5. Rules can filter by service name, HTTP method, URL path, and host. If no rule matches, the default rule (1 reservoir, 5% rate) applies. The sampling decision is made at the first service and propagated downstream.

5

View and Analyze Traces in X-Ray Console

After deploying, send test requests. In the X-Ray console, navigate to Service Map to see a visual graph of services and connections. Click on a node to see traces. Use the search bar to filter by annotation (e.g., `annotation.customerId = "12345"`). Click on a trace to view the timeline of segments and subsegments, including durations, errors, and HTTP status codes. The raw segment document shows annotations and metadata. You can also use the GetTraceSummaries and BatchGetTraces APIs programmatically.

What This Looks Like on the Job

Scenario 1: E-Commerce Checkout Microservice

A large e-commerce platform uses a microservice architecture for checkout: API Gateway → Lambda (order processing) → DynamoDB (write order) → SNS (notify shipping). They enable X-Ray on all services. Initially, they only see segments from API Gateway and Lambda, but the DynamoDB call appears as a generic subsegment. They add annotations like orderId, customerTier, and paymentMethod. This allows the operations team to search for all failed checkouts for premium customers. They also add metadata containing the full order payload for debugging. They configure a custom sampling rule to trace 100% of requests for customerTier = "premium" by setting a high priority rule with FixedRate=1.0 and ReservoirSize=1000. For other customers, they use the default sampling. This saves costs while ensuring premium customers are fully traced.

Scenario 2: High-Volume Image Processing Pipeline

A video processing pipeline uses S3 → Lambda (thumbnail generation) → SQS → EC2 (video encoding). They enable X-Ray on Lambda and EC2. However, the EC2 instances are not instrumented with the X-Ray SDK, so traces end at Lambda. They install the X-Ray daemon on EC2 and instrument the encoding code. They add annotations for videoId and resolution. They face a problem: the default sampling (1 reservoir, 5% rate) is too low for the high volume (10,000 requests/sec). They increase the reservoir to 100 and rate to 10% but still miss important traces. They then create a rule to trace 100% of requests for a specific video that is being tested. They also realize that the SQS async call breaks the trace; they manually propagate the trace header by reading it from the SQS message attributes and passing it to the EC2 worker. This restores the end-to-end trace.

Scenario 3: Serverless API with Custom Authentication

A company builds a serverless API using API Gateway with a Lambda authorizer. They enable X-Ray on both the authorizer and the backend Lambda. They notice that the authorizer segment appears as a separate trace because the authorizer runs before the backend. They add annotations to both segments: userId from the JWT token. They create a sampling rule that traces 100% of requests for users in the "admin" group. They also add metadata containing the decoded JWT payload for debugging authorization failures. They discover that the authorizer sometimes throws errors; by examining the subsegments, they pinpoint a DynamoDB call that times out. They increase the DynamoDB timeout and add retry logic. X-Ray helped them reduce mean time to resolution from hours to minutes.

How DVA-C02 Actually Tests This

What the DVA-C02 Tests

The exam focuses on the practical application of X-Ray segments, annotations, and sampling. Key objectives under Domain 4.1 include: understanding the difference between segments and subsegments, knowing when to use annotations vs. metadata, configuring sampling rules, and interpreting trace data. You will be asked to choose the correct approach for a given scenario.

Top Wrong Answers and Why Candidates Choose Them

1.

Confusing annotations with metadata: Candidates often select metadata when the question asks for searchable filtering. They think metadata is more flexible, but the exam tests that annotations are indexed for search.

2.

Thinking sampling decisions can be overridden downstream: A common misconception. The exam presents a scenario where a downstream service wants to trace a request that was not sampled upstream. The correct answer is that the downstream service must honor the decision; it cannot start a new trace for the same request.

3.

Assuming all AWS services automatically propagate trace headers: While many do, some like SQS require manual propagation. The exam may ask how to maintain trace context across async boundaries.

4.

Selecting the wrong default sampling values: The default reservoir is 1 per second and rate is 5%. Candidates sometimes remember 10% or 0.1.

Specific Numbers and Terms That Appear Verbatim

Default sampling: 1 reservoir, 5% rate.

Annotations limit: 50 per segment.

Annotation key max: 512 characters; value max: 1024 characters.

X-Ray daemon UDP port: 2000.

Trace ID format: 1-{8 hex digits}-{24 hex digits}.

Maximum segment document size: 64 KB.

_X_AMZN_TRACE_ID environment variable in Lambda.

Edge Cases and Exceptions

Lambda with X-Ray disabled: If tracing is not enabled, no segments are sent, even if the SDK is used. The environment variable _X_AMZN_TRACE_ID is not set.

API Gateway with X-Ray enabled but Lambda with X-Ray disabled: API Gateway sends a segment, but the Lambda segment is missing. The trace will be incomplete.

Sampling rules with overlapping conditions: The rule with the lowest priority number wins. If two rules have the same priority, behavior is undefined (but AWS documentation says the first match wins).

Subsegments for async calls: Must be created manually; the SDK does not auto-capture async operations.

How to Eliminate Wrong Answers

If a question asks for filtering traces, eliminate any option mentioning metadata. If it asks for reducing trace volume, think sampling rules, not disabling X-Ray entirely. If it asks about propagating trace context across SQS, look for manual header manipulation. Always remember the default sampling values—they are frequently tested.

Key Takeaways

Segments represent work by a service; subsegments break down work within a segment.

Annotations are indexed key-value pairs for filtering; metadata is not indexed.

Default sampling: 1 reservoir per second and 5% rate.

The sampling decision is made at the entry point and propagated downstream; cannot be overridden.

Lambda requires X-Ray tracing enabled and SDK instrumentation for custom subsegments.

X-Ray daemon listens on UDP port 2000.

Trace ID format: 1-{8 hex digits}-{24 hex digits}.

Maximum segment document size is 64 KB.

Subsegments for async calls must be created manually.

Sampling rules are evaluated by priority (lowest number first).

Easy to Mix Up

These come up on the exam all the time. Here's how to tell them apart.

Annotations

Indexed for search and filtering

Limited to 50 per segment

Supports string, number, boolean values

Key max 512 chars, value max 1024 chars

Used for high-cardinality attributes like customerId

Metadata

Not indexed; cannot be used for filtering

No specific count limit (but segment size limit applies)

Can store any JSON-serializable data (objects, arrays)

No key/value length limits (but segment size limit applies)

Used for debugging data like full request payloads

Watch Out for These

Mistake

Annotations and metadata are interchangeable for filtering traces.

Correct

Annotations are indexed and searchable; metadata is not. Only annotations can be used in the X-Ray console search bar or via the GetTraceSummaries API filter expressions.

Mistake

You can change the sampling decision at any downstream service.

Correct

The sampling decision is made at the entry point and propagated via trace headers. Downstream services must honor that decision; they cannot override it to start tracing a request that was not sampled.

Mistake

The X-Ray SDK automatically captures all subsegments, including asynchronous calls.

Correct

The SDK auto-captures subsegments for synchronous AWS SDK calls and HTTP requests. Asynchronous operations (e.g., SQS send, Lambda invoke) require manual subsegment creation using `beginSubsegment()` and `endSubsegment()`.

Mistake

Enabling X-Ray on Lambda automatically traces all downstream calls without any code changes.

Correct

Enabling X-Ray on Lambda only sends the Lambda service segment. To trace downstream calls and add custom annotations, you must instrument your function code with the X-Ray SDK.

Mistake

The default sampling rate traces 10% of requests.

Correct

The default sampling is 1 reservoir per second and 5% of additional requests. The effective rate varies with volume but is not a flat 10%.

Do You Actually Know This?

Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.

Frequently Asked Questions

What is the difference between a segment and a subsegment in AWS X-Ray?

A segment represents the work done by a single service (e.g., a Lambda function, an EC2 instance) for a request. It is the top-level unit of work. A subsegment is a nested unit within a segment that represents a downstream call (e.g., DynamoDB query) or a piece of internal logic. Subsegments help you drill down into the performance of specific operations within a service. For example, a Lambda segment might contain subsegments for an HTTP call and a database query.

When should I use annotations vs. metadata in X-Ray?

Use annotations when you need to filter or search traces based on that attribute. Annotations are indexed and can be used in the X-Ray console search bar or via the GetTraceSummaries API filter expressions (e.g., `annotation.customerId = "123"`). Use metadata for arbitrary debugging data that you don't need to search on, such as full request/response payloads or stack traces. Metadata is not indexed and only appears in the raw segment details.

How does X-Ray sampling work and what are the default values?

X-Ray sampling determines which requests are traced to control cost and volume. The default sampling rule uses a reservoir of 1 trace per second (guaranteed to be traced) and a rate of 5% of additional requests. So the first request each second is always traced, and then 5% of the remaining requests are traced. You can create custom rules with higher priority to override the default for specific services, paths, or methods.

Can I change the sampling decision for a request after it has been made?

No. The sampling decision is made at the first service that receives the request (e.g., API Gateway, load balancer, or instrumented web server). That decision is propagated to all downstream services via the trace header. Downstream services must honor the decision; they cannot start tracing a request that was not sampled. However, they can still create subsegments if the parent segment is sampled.

How do I propagate the X-Ray trace context across asynchronous calls like SQS?

For asynchronous calls, you must manually propagate the trace header. For SQS, you can read the trace header from the `_X_AMZN_TRACE_ID` environment variable (in Lambda) or from the current segment, and then include it as a message attribute when sending the message. The consumer service then reads the attribute and sets the trace header before processing. The X-Ray SDK provides utilities for this, such as `AWSXRay.utils.processTraceData()`.

What is the maximum size of a segment document in X-Ray?

The maximum segment document size is 64 KB. If your segment (including annotations and metadata) exceeds this, X-Ray will reject it. You should avoid storing large payloads in metadata; instead, store them in a separate service like S3 and reference the key in an annotation.

How do I enable X-Ray tracing on a Lambda function?

You can enable X-Ray tracing on a Lambda function in the AWS Console under Configuration > Monitoring and operations tools, set X-Ray tracing to Active. Alternatively, use the AWS CLI: `aws lambda update-function-configuration --function-name my-function --tracing-config Mode=Active`. You also need to instrument your function code with the X-Ray SDK to capture custom subsegments and annotations.

Terms Worth Knowing

Ready to put this to the test?

You've just covered X-Ray Segments, Annotations, and Sampling — now see how well it sticks with free DVA-C02 practice questions. Full explanations included, no account needed.

Done with this chapter?