aws / aws/aws-xray-sdk-node

XRAY does not work when an app initialises AWS clients that are already initialised in imported libraries

Open
#315 13 comments 7 reactions 1 assignee Claimed by @willarmiros View on GitHub
bug
Dominant language
JavaScript
Stars
280
Forks
157
PR merge metrics
No merged PRs in 30d

Description

Hello,

I'm seeing a strange issue with XRAY. Apologies if the title isn't clear. I'll describe the issue below:

### Setup

- I have a couple of libraries which AWS and XRAY to perform some tasks (like Dynamo lookup, SNS publish, etc). These tasks are exposed by functions. All of these libraries use SSM like this:

```
import AWSXRay from 'aws-xray-sdk-core';
import AWSNoXray from 'aws-sdk';

const AWS = AWSXRay.captureAWS(AWSNoXray);
const ssmClient = new AWS.SSM();
//do ssm lookup using client.
```

- I have a framework which imports some of the functions from the above libraries. This framework exposes a couple of lambda handler functions.

**_The libraries and framework are bundled using webpack_**

- I have an app which has multiple lambda functions (a couple of the handler functions are in this app and a couple are imported from the framework). It also imports some of the functions from the above libraries and within the app itself, there are additional functions that require AWS and XRAY.
The first point of entry of the app is a lambda function called `firstFunction`. All of the processing for this function is in the framework. It sends an SNS which is then handled by another lambda function called `secondFunction`. `secondFunction` is partially handled in the framework; it performs some tasks then emits events (I use the `NodeJS.EventEmitter`). The event handling happens in the app whereas all tasks until and including event emission happen int he framework.

### Problem
I see XRAY traces lambda `firstFunction` but not for `secondFunction`.

### Workaround that works
The current workaround is not ideal but here it is:
1. Expose all exported functions from all libraries in the framework. The app then imports these functions from the framework.
So in the app, I go from:
```
import {someFn} from 'my-library'
```
...to:
```
import {someFn} from 'my-framework'
```
_**Basically, any fn in these libraries that uses AWS clients should be imported via the framework and not directly from the library.**_

2. Any AWS client that the app requires should also be imported from the framework. So I had to create another library called `my-aws-clients` which basically has this:
```
import AWSXRay from 'aws-xray-sdk-core';
import AWSNoXray from 'aws-sdk';
const AWS = AWSXRay.captureAWS(AWSNoXray);
export const dynamoClient = new AWS.DynamoDB.DocumentClient({
service: new AWS.DynamoDB()
});
export const snsClient = new AWS.SNS();
export const smsClient = new AWS.SSM();
```
Then I exposed these in the framework so my app can import these AWS clients from the framework.

3. All of the AWS clients in the libraries were updated to use the clients from `my-aws-clients`.

### Why is the workaround not ideal
I have libraries that do tasks which shouldn't be imported/exposed via the framework; basically it's not their place. For one particular library, I had to copy the entire code into the app so that the AWS interactions of this library happen via the AWS clients that are coming from the framework. This particular library is meant to be used by others apps/teams that have no relation to the framework so I cannot have the dependency on the framework.

### What I tried that did not work
1. I tried to get the libs, framework and app to import from `my-aws-clients`. But it looks like if I do that, XRAY is re-initialised and the rootId becomes null.
2. I created a lambda handler function for `secondFunction` in the app to replace the existing one from the framework. In this function, I did a simple Dynamo query (so no event emitters, etc) to see if there is any problem with event emitters. But this also did not work.

Pls let me know if you require more info.

Thank you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.