Invoke AWS Lambda with AWS X-Ray locally
Asked Answered
S

1

10

Is there a way to invoke lambda with X-Ray by using sam invoke local?

According to the idea which PaulMaddox mentioned, I have tried the step below, and I don't know whether I misunderstood :

  1. Run a X-Ray Daemon locally (0.0.0.0:2000) by following the document
  2. In my lambda's template.yaml set the ENV AWS_XRAY_DAEMON_ADDRESS: 0.0.0.0:2000
  3. Invoke the function, but still got error Missing AWS Lambda trace data for X-Ray. Expected _X_AMZN_TRACE_ID to be set

Here is part of template.yaml setting, I used the environment variable to set AWS_XRAY_DAEMON_ADDRESS

enter image description here

It would be nice if you could provide more information.

Servility answered 15/6, 2018 at 2:53 Comment(0)
J
6

I'm not too familiar with SAM, but...

You need to set the _X_AMZN_TRACE_ID environment variable. Currently, the X-Ray Node SDK works by cross communicating between the Lambda runtime start-up code and the user code.

Lambda starts the segment in their start-up code, records information such as timing and exceptions, and sends the segment to the X-Ray service. Then, it forwards the trace ID/parent ID/sampling decision to the user code via setting the _X_AMZN_TRACE_ID environment variable. This allows the SDK to create a separate subsegment, inferring a connection to the original segment, which gets "weaved" into the original on the service end without actually being directly related. Both are sent out of band, asynchronously from each other.

The _X_AMZN_TRACE_ID variable fits the same format as the tracing header here as discussed here: https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader

If you want to send traces through the Daemon to the X-Ray service, you'll need to figure out how to get SAM to construct this Lambda segment initially and set the _X_AMZN_TRACE_ID prior to importing the SDK.

Since the SDK auto-detects the presence of Lambda (which as I understand, SAM mimics), you'll have to set the _X_AMZN_TRACE_ID variable before importing in the SDK. Which, is kind of a catch-22, because you need to import the SDK (in the non-Lambda mode) to construct the Lambda segment before you can populate the _X_AMZN_TRACE_ID.

The problem lies here: https://github.com/aws/aws-xray-sdk-node/blob/master/packages/core/lib/aws-xray.js#L361

If you flip the SDK into LOG_ERROR mode (ignoring the Lambda errors), create and send the Lambda segment (just manually create a segment, load the generated ID/Parent ID/Sampling into _X_AMZN_TRACE_ID then close the segment) and then clear cache/re-import the SDK after, then that should work.

Otherwise, I suspect there may be some work on the SAM end to have this built in. But, hopefully this work around works.

Jarvisjary answered 15/6, 2018 at 22:55 Comment(2)
Thank you for the clear explanation ! I will try the idea you gave, it's really a great help.Servility
Example : github.com/aws/aws-xray-sdk-node/issues/…Cutlery

© 2022 - 2024 — McMap. All rights reserved.