Following from the General Availability (GA) release of the AWS X-Ray distributed tracing service in April, Amazon has added AWS Lambda support for AWS X-Ray, enabling function invocations and associated metadata to be recorded, displayed graphically via the AWS Console, and analysed for debugging or fault resolution purposes.
X-Ray can be enabled for existing Lambda functions via the AWS Console by navigating to the function’s configuration tab, scrolling to the ‘Advanced settings’, and selecting the ‘Enable active tracing’ box; or via the AWS Command Line Interface (CLI) by updating the function’s tracing-config:
$ aws lambda update-function-configuration --tracing-config '{"Mode": "Active"}' --function-name 'my-function-name-or-ARN'
The AWS Blog contains an article by Randall Hunt, an AWS Developer Evangelist, which states that when tracing mode is active, Lambda will attempt to trace the function unless this is explicitly disabled by an upstream service. As Lambda functions are triggered, traces will begin to be generated and captured, and a visual representation of the application resources and the connections (edges) between them can be viewed via the AWS Console:
Visual Representation of an X-Ray Trace in the AWS Console (Image via Randall Hunt)
The data collected at each service or function is called a segment (analogous to a Span within the OpenTracing API specification), and is stored as a chunk of JSON data. A sample of these segments are sent to the AWS X-Ray service via the X-Ray daemon that must be running alongside the Lambda function. Hunt notes that the X-Ray daemon does consume a small amount of the associated Lambda function resources, and accordingly if the function gets close to the specified memory limit Lambda will try to kill the X-Ray daemon to avoid throwing an out-of-memory error.
A segment represents a unit of work, and includes request and response timing, along with optional sub-segments that represent smaller work units. Sub-segments can be started and stopped, and additional metadata added, via a language-specific SDK for X-Ray. Currently there is official support in the X-Ray SDK for the Java 8 runtime and X-Ray SDK for Node.js 4.3 and newer runtimes, and unofficial support for other languages from the open source community e.g. Python support is provided by the ‘Fleece’ project, hosted on GitHub.
The path of a request through your application is tracked with a ‘trace’. A trace collects all the segments generated by a single request. That means you can easily trace events throughout a request lifecycle from any supported service, for example an API Gateway invocation, processing via application/service functions and all the way to an X-Ray enabled datastore like AWS DynamoDB, in order to understand where errors and latencies are located.
Additional details on the AWS services that are currently supported by AWS X-Ray can be found on the ‘Integrating AWS X-Ray with Other AWS Services’ documentation, and further information on AWS X-Ray and the associated tracing terminology can be found in an earlier InfoQ news item published after the 2016 AWS re:invent conference.
The X-Ray service is generally available in the US East (Northern Virginia), US West (Northern California), US East (Ohio), US West (Oregon), EU (Ireland), EU (Frankfurt), South America (São Paulo), Asia Pacific (Tokyo), Asia Pacific (Seoul), Asia Pacific (Sydney), Asia Pacific (Sydney), and Asia Pacific (Mumbai) Regions. Additional information can be found on the AWS Lambda webpage and the ‘Troubleshooting Lambda-Based Applications’ documentation.