https://github.com/blimmer/xray-async-invocation
https://github.com/blimmer/xray-async-invocation
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/blimmer/xray-async-invocation
- Owner: blimmer
- Created: 2021-11-30T21:51:20.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-08T02:13:05.000Z (over 3 years ago)
- Last Synced: 2025-02-26T20:43:00.958Z (over 1 year ago)
- Language: TypeScript
- Size: 273 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Node X-Ray Async Invocation Issue
This is some simple sample code that illustrates an issue where X-Ray Segments are lost when asynchronously invoking
another Node lambda from an instrumented Node lambda.
I opened an issue on the aws-xray-sdk-node GitHub repository: https://github.com/aws/aws-xray-sdk-node/issues/477 to track this issue.
## Code Layout
There are two lambda functions defined in this repository:
- [triggering lambda](https://github.com/blimmer/xray-async-invocation/blob/main/lib/xray-async-invocation-stack.triggering.ts)
- lists all s3 buckets (to generate a segment)
- invokes the triggered lambda
- [triggered lambda](https://github.com/blimmer/xray-async-invocation/blob/main/lib/xray-async-invocation-stack.triggered.ts)
- lists all s3 buckets (to generate a segment)
In both lambdas, the `captureAWS` method from `aws-xray-sdk` is used as recommended by
[this documentation](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-tracing.html).
## To Deploy
This is an [aws-cdk](https://aws.amazon.com/cdk/) application.
1. Run `npm install`
1. Run `npx cdk deploy 'TestXrayAsyncInvocationStack'`
This will create a CloudFormation stack in your AWS account called `TestXrayAsyncInvocationStack`.
## Reproduction Steps
1. Visit the Lambda console for the `test-xray-triggering`. For instance, if you deploy to `us-east-1` the URL will be https://console.aws.amazon.com/lambda/home?region=us-east-1#/functions/test-xray-triggering?tab=code.
1. Click the "Test" tab and then the "Test" button to trigger the lambda. The contents of the event do not matter.
### Observed Behavior
The last segment is a `202` HTTP status code, meaning that the async invocation of the test lambda
succeeded.

However, the triggered lambda produced no traces at all:

Even though it was definitely invoked:

### Expected Behavior
Because these two lambdas are triggered by the same "logical request", I expected the X-Ray
tracing segment generated by the triggering lambda to be passed as metadata to the async
invocation of the triggered lambda.
At the very least, even if the traces were not linked, I expected a trace to be produced by
the triggered lambda.
## Python Example
I'm pretty confident that this is an issue with the Node version of the `aws-xray-sdk` package because everything
works as expected when using the Python version of `aws-xray-sdk`.
You can check out the [`use-python` branch of this repo](https://github.com/blimmer/xray-async-invocation/tree/use-python)
to see this working.
### Deploying the Python Example
Prerequisites: You must have `docker` installed and running on your machine.
1. `git checkout use-python`
1. Run `npm install`
1. Run `npx cdk deploy 'TestXrayAsyncInvocationStack'`
### Running the Python Example
Follow the same steps as [above](#reproduction-steps)
You'll see that the X-Ray trace includes the asynchronous invocation, which is the expected behavior for Node, as well.

## Tearing Down the Infrastructure
After you're done testing, you can tear down the infrastructure by running:
```
npx cdk destroy 'TestXrayAsyncInvocationStack'
```