XRay Tracing

XRay Tracing

LocalStack Pro allows to instrument your applications using XRay tracing. This helps in optimizing the interactions between service calls, and facilitates debugging of performance bottlenecks.

For example, a Python Lambda function can be instrumented as follows (based on the example here):

  1. import boto3
  2. from aws_xray_sdk.core import xray_recorder
  3. from aws_xray_sdk.core import patch
  4. patch(['boto3'])
  5. s3_client = boto3.client('s3')
  6. def lambda_handler(event, context):
  7. s3_client.create_bucket(Bucket='mybucket')
  8. xray_recorder.begin_subsegment('my_code')
  9. # your function code goes here...
  10. xray_recorder.end_subsegment()

Running this code in Lambda on LocalStack will result in two trace segments being created in XRay - one from the instrumented boto3 client when running create_bucket(..), and one for the custom subsegment denoted 'my_code'. You can use the regular XRay API calls (e.g., GetTraceSummaries, BatchGetTraces) to retrieve the details (timestamps, IDs, etc) of these segments.

You can also checkout another of our examples with Xray and Lambda, deployed via the Serverless framework, here

Note: To use XRay in Lambdas, please note that you’ll need to configure LAMBDA_XRAY_INIT=1 - this will ensure that the XRay daemon process is fully initialized when spawning Lambda containers (may slightly increase startup times).

Last modified June 29, 2022: add pro-sample reference for xray with lambda (d8affaae)