Lambda- successive invocations reusing first invocation's facade segment
- Dominant language
- JavaScript
- Stars
- 280
- Forks
- 157
- PR merge metrics
- No merged PRs in 30d
Description
Successive invocations of a lambda are adding to the trace of the initial invocation. I presume I've got this set up incorrectly but I'm struggling to identify what it is.
The first invocation trace looks like:

A later one looks like this:

Simplified code:
```
const AWSXRay = require("aws-xray-sdk");
const AWS = AWSXRay.captureAWS(require("aws-sdk"));
const AWS_EXECUTION_ENV = process.env.AWS_EXECUTION_ENV;
async function processLocation(event) {
AWSXRay.setContextMissingStrategy(() => {});
xrayEnabled = !!AWS_EXECUTION_ENV;
if (xrayEnabled) {
let facadeSegment = AWSXRay.getSegment();
console.log("Starting segment is called: " + facadeSegment.name);
let segment = facadeSegment.addNewSubsegment("Root");
AWSXRay.setSegment(segment);
segment.addMetadata("event data", JSON.stringify(event));
}
```
The log statement outputs:

Any pointers?
Later in the lambda I call `AWSXRay.setSegment(subSegment);` before making parallel HTTP calls otherwise I found the auto-instrumented segments using `captureHTTPsGlobal` wouldn't attach to the correct parent segments. It's around here that it smells bad to me.
Contributor guide
Assessment
This issue has not been assessed yet.