Inconsistent behaviour of .captureAsyncFunc()
- 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
Assessment
This issue has not been assessed yet.