aws / aws/aws-xray-sdk-node

Inconsistent behaviour of .captureAsyncFunc()

Open
#270 1 comment 0 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, we are having some problems with the manual subsegment creation.

One detail is that we are using experimental in order to work with promises.

Let me share 2 different classes using manual segmentation.

```
async query(sql: QueryConfig | string, values?: Array): Promise {
const client = await this.pool.connect();
return new Promise((resolve, reject) => {
AWSXRay.captureAsyncFunc('query', async (subsegment: any) => {
try {
const response = await client.query(sql, values);
resolve(response);
} catch (error) {
reject(error);
} finally {
client.release();
subsegment!.close();
}
})
});
}
```

That one is from our Postgres service and is working very well. Somehow, the same way of capturing is not working in our Controller class:

```
public async getItems(groupedOfflineIds: { [type in OfflineType]: string[] }): Promise {
return new Promise((resolve, reject)=>{
AWSXRay.captureAsyncFunc('Offline getItems (ES)', async (subsegment: any) => {
try{
const [a, b, c] = await Promise.all([
this.asyncFunc1(),
this.asyncFunc2(),
this.asyncFunc2(),
]);

subsegment!.addAnnotation('a lenght', a.length);
subsegment!.addAnnotation('b lenght', b.length);
subsegment!.addAnnotation('c lenght', c.length);

return resolve({a, b, c})
}
finally {
subsegment!.close();
}
})
});
}
```

It is worth mentioning that this subsegments are inside a segment that is being handled by express (as middleware).
```
app.use(AWSXRay.express.openSegment('api'));
// ...
app.use(AWSXRay.express.closeSegment());
```

The entire trace fails for the second snippet, not only the subsegment that is not showing, the whole `API` segment fails for the endpoint containing the code. While the endpoints calling the first code snippet are fine.

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.