AWS X-Ray là distributed tracing service cho phép trace request path qua toàn bộ microservices architecture, identify performance bottleneck và debug errors trong distributed system.
- Concepts: Trace — toàn bộ journey của 1 request (end-to-end); Segment — công việc của 1 service trong trace; Subsegment — breakdown chi tiết trong segment (downstream call, DB query, external HTTP); Annotation — key-value indexed data để filter traces; Metadata — arbitrary data không indexed; Sampling — chỉ trace một tỷ lệ % requests để kiểm soát cost (mặc định 5% + 1 request/s đầu tiên).
- Instrument Node.js:
javascript
npm install aws-xray-sdk-core
// Prefer AWS SDK v3 with captureAWSv3Client:
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const AWSXRay = require('aws-xray-sdk-core');
const ddb = AWSXRay.captureAWSv3Client(new DynamoDBClient({}));
// Capture HTTP outbound:
AWSXRay.captureHTTPsGlobal(require('https'));
// Express middleware:
app.use(AWSXRay.express.openSegment('MyApp'));Lambda tự động tích hợp nếu enable Active Tracing trong function config (Lambda tạo segment, bạn thêm subsegment).
- X-Ray Service Map visualize toàn bộ dependencies, response time, error rate từng node.
- X-Ray Analytics cho filter, compare, build group của traces.
- Tích hợp với CloudWatch ServiceLens để combine metrics, logs và traces trong 1 view.
- OpenTelemetry compatible qua AWS Distro for OpenTelemetry (ADOT) — cho phép switch provider sau.
AWS X-Ray is a distributed tracing service that lets you trace request paths across an entire microservices architecture, identify performance bottlenecks, and debug errors in distributed systems.
- Key concepts: Trace — the full end-to-end journey of a single request; Segment — work done by one service within a trace; Subsegment — detailed breakdown within a segment (downstream calls, DB queries, external HTTP); Annotation — indexed key-value data for filtering traces; Metadata — arbitrary non-indexed data; Sampling — traces only a percentage of requests to control cost (default: 5% + the first request per second).
- Instrumenting Node.js:
javascript
npm install aws-xray-sdk-core
// Prefer AWS SDK v3 with captureAWSv3Client:
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const AWSXRay = require('aws-xray-sdk-core');
const ddb = AWSXRay.captureAWSv3Client(new DynamoDBClient({}));
// Capture HTTP outbound:
AWSXRay.captureHTTPsGlobal(require('https'));
// Express middleware:
app.use(AWSXRay.express.openSegment('MyApp'));Lambda integrates automatically when Active Tracing is enabled in function config (Lambda creates the segment; you add subsegments).
- The X-Ray Service Map visualizes all dependencies, response times, and error rates per node.
- X-Ray Analytics allows filtering, comparing, and grouping traces.
- Integrates with CloudWatch ServiceLens to combine metrics, logs, and traces in a single view.
- Compatible with OpenTelemetry via AWS Distro for OpenTelemetry (ADOT) — allowing you to switch providers later.