Study Guide1,150 words

AWS Certified Developer - Associate: Implementing Tracing & Observability

Implement tracing by using AWS services and tools

Implementing Tracing with AWS X-Ray and Tools

This guide focuses on the implementation of distributed tracing within AWS environments, a core requirement for the DVA-C02 exam under Task 4.2: Instrument code for observability.

Learning Objectives

After studying this guide, you should be able to:

  • Differentiate between Segments and Subsegments in AWS X-Ray.
  • Implement X-Ray instrumentation in AWS Lambda, EC2, and ECS.
  • Contrast Annotations and Metadata for effective filtering.
  • Configure Sampling Rules to balance cost and visibility.
  • Use AWS Distro for OpenTelemetry (ADOT) as an alternative tracing mechanism.

Key Terms & Glossary

  • Trace: A collection of segments that represent a single request moving through an entire distributed system.
  • Segment: A JSON object containing information about the work performed by a single service (e.g., an HTTP request handled by a Lambda function).
  • Subsegment: Granular timing data for downstream calls (e.g., a DynamoDB PutItem call) or specific code blocks within a segment.
  • Sampling: The process of selecting which requests are traced to minimize overhead and cost.
  • Annotation: Key-value pairs indexed by X-Ray for use with filter expressions.
  • Metadata: Key-value pairs (any data type) not indexed, used for additional context in the trace.

The "Big Idea"

In a monolithic architecture, a single log file might tell the whole story. In a Microservices or Serverless architecture, a single user request can trigger dozens of internal calls. Tracing is the "thread" that sews these disparate events together. AWS X-Ray allows developers to see the "path of travel," identify where bottlenecks occur, and find the exact service causing a 5XX error in a complex chain.

Formula / Concept Box

FeatureAnnotationsMetadata
Searchable/IndexedYesNo
Data TypesString, Number, BooleanAny (including Objects/Arrays)
Use CaseFiltering for specific UserIDs or Error codesStoring large response payloads for debugging
Limit50 per segmentNo strict count (size limit applies)

Hierarchical Outline

  1. X-Ray Core Concepts
    • Trace Header: X-Amzn-Trace-Id used to propagate tracing across services.
    • Service Graph: Visual representation of the relationship between services.
  2. Instrumentation Strategies
    • AWS Lambda: Enable "Active Tracing" in the console/template. No daemon installation required.
    • EC2 / On-Premise: Requires installation and execution of the X-Ray Daemon (UDP port 2000).
    • ECS / Fargate: Run the X-Ray Daemon as a sidecar container.
  3. SDK Integration
    • Wrapping the AWS SDK (e.g., AWSXRay.captureAWS(require('aws-sdk'))).
    • Wrapping HTTP clients and SQL queries.
  4. Sampling Rules
    • Reservoir: Minimum number of traces per second (guaranteed visibility).
    • Rate: Percentage of traces above the reservoir.

Visual Anchors

Request Lifecycle in X-Ray

Loading Diagram...

Trace Structure (TikZ)

\begin{tikzpicture}[node distance=1cm, every node/.style={draw, rectangle, rounded corners, inner sep=5pt}] \node (trace) [fill=blue!10] {\textbf{Trace ID: 1-5759...}}; \node (seg1) [below of=trace, xshift=-2cm, fill=green!10] {Segment: Frontend}; \node (seg2) [below of=trace, xshift=2cm, fill=orange!10] {Segment: Backend}; \node (sub1) [below of=seg2, fill=yellow!10] {Subsegment: RDS Query};

\draw[->] (trace) -- (seg1); \draw[->] (trace) -- (seg2); \draw[->] (seg2) -- (sub1); \end{tikzpicture}

Definition-Example Pairs

  • Active Tracing: Enabling X-Ray at the service level (like Lambda or API Gateway) without modifying code.
    • Example: Checking the "Enable active tracing" box in the Lambda configuration tab to automatically capture function execution time.
  • Downstream Call Instrumentation: Modifying the AWS SDK client to record calls to other AWS services.
    • Example: Wrapping the boto3 client in Python so that every s3.put_object call shows up as a subsegment in the X-Ray console.
  • X-Ray Daemon: A listener application that buffers segments and uploads them to the X-Ray API.
    • Example: Running the aws-xray-daemon as a background process on an Amazon Linux 2 EC2 instance.

Worked Examples

Example 1: Instrumenting a Node.js Lambda Function

Goal: Capture traces for a Lambda function that writes to DynamoDB.

  1. Permission: Ensure the Lambda execution role has xray:PutTraceSegments and xray:PutTelemetryRecords (via the AWSXRayDaemonWriteAccess policy).
  2. Configuration: Enable "Active Tracing" in the Lambda settings.
  3. Code:
javascript
const AWSXRay = require('aws-xray-sdk-core'); const AWS = AWSXRay.captureAWS(require('aws-sdk')); // Wrap the entire SDK const ddb = new AWS.DynamoDB.DocumentClient(); exports.handler = async (event) => { const segment = AWSXRay.getSegment(); // Get automatically created segment segment.addAnnotation('UserID', '12345'); // Indexed for search await ddb.put({ TableName: 'Users', Item: { id: '12345' } }).promise(); return { statusCode: 200 }; };

Example 2: Configuring Custom Sampling Rules

Goal: Ensure we always get 1 trace per second, but only 5% of traffic thereafter for a high-volume API.

Create a sampling-rules.json file:

json
{ "SamplingRule": { "RuleName": "ProductionAPI", "Priority": 10, "FixedRate": 0.05, "ReservoirSize": 1, "ServiceName": "*", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "/v1/*", "Version": 1 } }

Apply via CLI: aws xray create-sampling-rule --cli-input-json file://sampling-rules.json.

Checkpoint Questions

  1. Which X-Ray component is responsible for gathering data and sending it to the X-Ray API on an EC2 instance?
  2. What is the difference between a Segment and a Subsegment?
  3. You need to search for traces associated with a specific Order_ID. Should you use an Annotation or Metadata?
  4. How does a Lambda function send trace data if you haven't installed the X-Ray daemon?
  5. What port and protocol does the X-Ray daemon use by default?
Click to see answers
  1. The X-Ray Daemon.
  2. A Segment represents the work of a single service; a Subsegment represents granular work (like downstream calls or logic) within that service.
  3. Annotation, because annotations are indexed and searchable.
  4. Lambda runs a managed version of the X-Ray daemon in the execution environment automatically when Active Tracing is enabled.
  5. Port 2000 using UDP.

Ready to study AWS Certified Developer - Associate (DVA-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free