AWS X-Ray
Core concept: X-Ray provides distributed tracing โ see how requests flow through your entire application (Lambda โ API Gateway โ DynamoDB โ external services).
Key Conceptsโ
| Concept | Description |
|---|---|
| Trace | End-to-end path of a request through all services |
| Segment | One service's portion of a trace (e.g., Lambda execution) |
| Subsegment | Work within a segment (e.g., a DynamoDB call) |
| Annotation | Key-value pair indexed for filtering/searching |
| Metadata | Key-value pair not indexed โ for debugging detail |
| Service Map | Visual graph of all services and their connections |
| Sampling | % of requests to trace (controls cost) |
Annotations vs Metadataโ
| Annotations | Metadata | |
|---|---|---|
| Indexed | โ Yes โ searchable | โ No |
| Use for | Filtering traces (userId, orderId) | Debugging data (full request/response) |
| Type | String, number, boolean | Any (JSON) |
Subsegment subsegment = AWSXRay.beginSubsegment("ProcessOrder");
try {
// Annotations are searchable
subsegment.putAnnotation("orderId", orderId);
subsegment.putAnnotation("customerId", customerId);
// Metadata for rich debugging (not searchable)
subsegment.putMetadata("orderDetails", orderMap);
processOrder(orderId);
} catch (Exception e) {
subsegment.addException(e);
throw e;
} finally {
AWSXRay.endSubsegment();
}
Sampling Rulesโ
Default sampling: 5% of requests + 1 req/second reservoir
Custom rules (configured in console or via API):
{
"RuleName": "HighValueOrders",
"Priority": 1,
"ReservoirSize": 10,
"FixedRate": 0.5,
"URLPath": "/orders/*",
"ServiceName": "order-service",
"HTTPMethod": "POST"
}
- ReservoirSize: requests/second to always trace
- FixedRate: % of remaining requests to trace
- Lower Priority number = higher priority
X-Ray Daemonโ
The X-Ray SDK sends trace data to the X-Ray daemon, which buffers and sends to the X-Ray API:
Your App โ X-Ray SDK โ UDP port 2000 โ X-Ray Daemon โ X-Ray API
| Environment | Daemon Location |
|---|---|
| Lambda | Built-in (enable Active Tracing) |
| ECS | Sidecar container |
| EC2 | Install manually or via User Data |
| Elastic Beanstalk | Built-in (enable in config) |
# Lambda โ enable X-Ray in SAM
MyFunction:
Type: AWS::Serverless::Function
Properties:
Tracing: Active # PassThrough = disabled
Java SDK Integrationโ
Maven Dependencyโ
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-core</artifactId>
<version>2.14.0</version>
</dependency>
<!-- AWS SDK instrumentation -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
<version>2.14.0</version>
</dependency>
Spring Boot Integrationโ
@Configuration
public class XRayConfig {
@Bean
public Filter tracingFilter() {
return new AWSXRayServletFilter("my-spring-service");
}
}
// All AWS SDK calls (DynamoDB, S3, SQS...) are auto-instrumented
// when aws-xray-recorder-sdk-aws-sdk-v2-instrumentor is on classpath
Manual Subsegmentsโ
@Service
public class PaymentService {
public void processPayment(String orderId) {
// Creates a subsegment in the current trace
AWSXRay.createSubsegment("PaymentGateway", (subsegment) -> {
subsegment.putAnnotation("orderId", orderId);
// Call external payment gateway
paymentGateway.charge(orderId);
return null;
});
}
}
What X-Ray Tracesโ
With the SDK and instrumentation:
- Incoming HTTP requests (via filter)
- AWS SDK calls (DynamoDB, S3, SQS, SNS, Lambda...)
- SQL queries (JDBC instrumentation)
- HTTP client calls (Apache HttpClient, OkHttp)
- Custom business logic (manual subsegments)
๐งช Practice Questionsโ
Q1. A developer needs to search X-Ray traces for all requests where userId = "user-123". What should they use to make this possible?
A) X-Ray Metadata with key userId
B) X-Ray Annotation with key userId
C) CloudWatch Log filter
D) X-Ray Groups
โ Answer & Explanation
B โ Annotations are indexed and searchable. Metadata is stored with the trace but not indexed and cannot be used in filter expressions. Use putAnnotation("userId", userId).
Q2. A Lambda function has X-Ray Active Tracing enabled. However, DynamoDB calls are not appearing as subsegments. What is missing?
A) X-Ray Daemon is not installed
B) The IAM role lacks xray:PutTraceSegments
C) The X-Ray SDK AWS SDK instrumentor is not on the classpath
D) X-Ray sampling rate is too low
โ Answer & Explanation
C โ To auto-instrument AWS SDK v2 calls, you need aws-xray-recorder-sdk-aws-sdk-v2-instrumentor on the classpath. Without it, DynamoDB/S3/SQS calls won't appear as subsegments.
Q3. By default, what percentage of requests does X-Ray trace?
A) 100%
B) 10%
C) 5% + 1 request/second reservoir
D) 1%
โ Answer & Explanation
C โ The default sampling rule traces the first request each second (reservoir) and 5% of all additional requests. This balances visibility with cost.