# APM - new transaction created when lambda requests via aws application load balancer

**URL:** <https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [September 14, 2022, 10:20am UTC](https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397 "2022-09-14T10:20:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcmikeyy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mcmikeyy/32/110865_2.png) [@mcmikeyy](https://discuss.elastic.co/u/mcmikeyy)\
**Post date:** [September 14, 2022, 10:20am UTC](https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397/1 "2022-09-14T10:20:46Z")

</div>

A lambda nodejs app has

- apm package "elastic-apm-node": "3.38.0"
- layer extension: layer:elastic-apm-extension-ver-1-1-0-x86\_64:1
- using elastic/kibana: 8.2.3

When a nodejs lambda is a target for a load balancer the traceparent/elastic-apm-headers are propagated but a new transaction is created even when the traceparent/elastic-apm-traceparent/tracestate headers are available on the request.

Looking at the code in elastic/apm-agent-nodejs this will only happen for requests done via api gateway (definition: event.requestContext && event.requestContext.requestId).

Is there any other way of getting the trace to propagate?

---

<div class="post-metadata">

**Author:** ![alanstorm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alanstorm/32/77134_2.png) [@alanstorm](https://discuss.elastic.co/u/alanstorm)\
**Post date:** [September 14, 2022, 2:23pm UTC](https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397/2 "2022-09-14T14:23:38Z")

</div>

Thanks for reporting this @mcmikeyy -- it sounds like you're saying that an API Gateway Request made to a Node.js instrumented lambda function will always start a new _distributed trace_ instead of continuing an existing trace. (that is -- a new transaction is always created for every new HTTP request, but transactions are linked together via a trace)

If that's the case this sounds like a bug -- we've opened [an issue for this](https://github.com/elastic/apm-agent-nodejs/issues/2925).

If you have a moment, we have a few additional questions. If you could answer as many of these as possible it will help us track down the root cause of this issue.

1. Do you happen to have a set of steps that can reproduce this behavior?

2. If not, do you have an example of the headers that are sent to the API Gateway?

3. Also, do you know if these initial trace headers generated by the Node.js Agent, an OpenTelemetry Agent, or some other agent/system?

---

<div class="post-metadata">

**Author:** ![mcmikeyy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mcmikeyy/32/110865_2.png) [@mcmikeyy](https://discuss.elastic.co/u/mcmikeyy)\
**Post date:** [September 14, 2022, 3:38pm UTC](https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397/3 "2022-09-14T15:38:48Z")

</div>

> [@alanstorm](#):
>
> If not, do you have an example of the headers that are sent to the API Gateway

I think my post was not clear enough. Sorry for the confusion I am not using an api gateway but application load balancer.

So fargate (nodejs) --\> application load balancer --\> lambda.

The fargate nodejs initiates the tracing, it makes a call to endpoint which resolves to an alb target which is a lambda. In the request to the lambda i can see the correct traceparent headers. In the Lambda the incoming json object is ALBEvent.

The ALBEvent contains the headers which have the traceparent within it and the requestContext which does not have a requestId unlike an api gateway which will have.

If i take this ALBEvent json and run it in the lambda test function and add a event.requestContext.requestId the service map will show an association between the two apps as the nodejs agent code assumes the request came from an api gateway request.

Initial trace headers are generated by the elastic-apm-node library package included in the fargate nodejs app.

"elastic-apm-traceparent"  
"traceparent"  
"tracestate"

At the moment i have deployed apps in aws where i can recreate this.

The lambda uses Starting node with `-r elastic-apm-node/start`

- node14
- apm package "elastic-apm-node": "3.38.0"
- layer extension: layer:elastic-apm-extension-ver-1-1-0-x86\_64:1

---

<div class="post-metadata">

**Author:** ![alanstorm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alanstorm/32/77134_2.png) [@alanstorm](https://discuss.elastic.co/u/alanstorm)\
**Post date:** [September 15, 2022, 12:13am UTC](https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397/4 "2022-09-15T00:13:25Z")

</div>

Ah, got it, that makes sense. And the misunderstanding was a team effort 🙂 I didn't realize you meant Amazon's Load Balance service -- I thought you were using the term generically.

First -- this is actually on our radar to get working -- the specific Node.js issue to watch is here: [[META 679] Add support for ELB and Lambda URL metadata in lambda integrations · Issue #2901 · elastic/apm-agent-nodejs · GitHub](https://github.com/elastic/apm-agent-nodejs/issues/2901), which will link to the meta issue

Second -- as a work around until the above lands -- I haven't tried it but my first instinct would be [to start and end a new transaction yourself in the Lambda function using the API](https://www.elastic.co/guide/en/apm/agent/nodejs/current/agent-api.html#apm-start-transaction) and use the extracted headers. You can use the documented `childOf` option as the `traceparent`, and while it's not yet a formalized part of the public api, an option of `tracestate` should be sufficient to pass on the `tracestate`.

If you give option 2 a try let us know how it goes.

---

<div class="post-metadata">

**Author:** ![mcmikeyy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mcmikeyy/32/110865_2.png) [@mcmikeyy](https://discuss.elastic.co/u/mcmikeyy)\
**Post date:** [September 23, 2022, 9:07am UTC](https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397/5 "2022-09-23T09:07:46Z")

</div>

Hi,

Thanks for the update. Tried option 2 and it didnt seem to generate any meaningful data. I am looking at open telemetry to see if that supports elb.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 14, 2022, 5:08am UTC](https://discuss.elastic.co/t/apm-new-transaction-created-when-lambda-requests-via-aws-application-load-balancer/314397/6 "2022-10-14T05:08:24Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
