Get startedGet started for free

Lesson 4.2 Video

1. Implementing X-Ray tracing

Last lesson covered X-Ray concepts. Now we put them into practice.

2. X-Ray SDK overview

The AWS SDK supports six languages, including Python, Java, and .NET. Automatic instrumentation patches common libraries so AWS calls become subsegments with almost no code changes. Manual instrumentation gives fine-grained control over specific functions. Either way, the SDK sends to the local daemon, never directly to the X-Ray API.

3. Python SDK: Flask setup

Install the AWS X-Ray SDK, then configure the recorder with four settings: service name, sampling, context_missing set to LOG_ERROR so the app doesn't crash when tracing context is missing, and the daemon address. Attach the middleware, and every request is traced.

4. Automatic instrumentation: Python

Calling patch_all is like fitting a black box flight recorder: you don't change how the plane flies, it silently captures everything. Every boto3 call, HTTP request, and database query becomes a subsegment. Use the selective patch call to control which libraries are instrumented.

5. Manual instrumentation: Python

The decorator wraps an entire function as a named subsegment. The context manager times specific blocks within a function, useful when one function makes multiple calls and you want each step's timing separately. Both appear in your trace timeline.

6. Annotations and metadata in code

Annotations and metadata are single-line calls. Think of annotations as sticky tabs in a reference book: they stick out so you flip straight to the right page. Annotations take simple typed values and are indexed for filtering; metadata is the content on the page, any JSON structure, but not indexed. WHERE clause: make it an annotation. SELECT clause: make it metadata.

7. Error handling in traces

Exceptions inside a traced function are captured automatically and attached to the segment. Add an annotation for the error type to filter for specific failures, and metadata for the full error details. Always re-raise so your application's normal error handling continues.

8. Node.js SDK: Express setup

Node.js follows the same pattern. Wrap your routes between openSegment and closeSegment middleware. For automatic instrumentation, wrap the AWS SDK with captureAWS and HTTP modules with captureHTTPs. Downstream calls are then traced as subsegments.

9. Lambda implementation

With Active Tracing enabled, Lambda sends basic traces automatically, no SDK required. Install the SDK to add subsegments, annotations, or instrument downstream calls like DynamoDB and S3. Enable it in the console or your template, and every invocation is traced.

10. ECS and Fargate: sidecar pattern

For ECS and Fargate, run the daemon as a sidecar container, like a support vehicle alongside a racing car: the car goes fast, the support vehicle handles logistics. Define two containers in the task definition: the daemon on UDP port 2000, and your app pointing to it via environment variable. It scales with your tasks, and failures stay isolated.

11. Installing the X-Ray daemon

The daemon is like a post box outside your front door: your app drops letters in, no waiting. It collects, bundles, and sends to X-Ray in batches. Install varies by platform: RPM, deb, Windows installer, or Docker. Then start the service.

12. Daemon configuration

You configure the daemon through a cfg.yaml file. A few keys matter most. TotalBufferSizeMB sets the buffer memory, so raise it for high traffic. Concurrency sets the parallel connections to X-Ray. Region pins it to your traces' region. The Socket block sets the UDP and TCP listen addresses; for containers, set the UDP address to 0.0.0.0 so others can reach it. Logging sets the level and log path, and LocalMode runs locally for development without AWS credentials.

13. IAM permissions

The daemon needs five IAM actions: PutTraceSegments and PutTelemetryRecords for sending data, plus three sampling permissions. Simplest is the managed policy AWSXRayDaemonWriteAccess; attach it to your EC2 instance, ECS task, or Lambda execution role. Without it, the daemon runs but silently drops all segments.

14. Daemon deployment patterns

Three deployment patterns. Per-instance on EC2 puts app and daemon on one host, simple and reliable. Sidecar on ECS or Fargate runs the daemon in a second container per task, recommended for containers. Shared service uses one central daemon for many tasks, fewer resources but a single point of failure.

15. Lesson summary

To recap: the SDK gives automatic instrumentation with patch_all and manual control with decorators and context managers. Annotations for filtering, metadata for context. Lambda traces automatically; add the SDK for subsegments. ECS uses the sidecar pattern. The daemon is configured via cfg.yaml and needs the managed IAM write-access policy.

16. Let's practice!

Let's take a closer look at implementing X-Ray tracing.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.