# How do I setup log correlation when using extension layers?

**URL:** <https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309>\
**Category:** APM\
**Tags:** nodejs, aws-lambda\
**Created:** [December 2, 2022, 12:51am UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309 "2022-12-02T00:51:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![wjensen-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wjensen-godaddy/32/114131_2.png) [@wjensen-godaddy](https://discuss.elastic.co/u/wjensen-godaddy)\
**Post date:** [December 2, 2022, 12:51am UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/1 "2022-12-02T00:51:00Z")

</div>

Hi! I'm using the following two extension layers:

arn:aws:lambda:us-west-2:267093732750:layer:elastic-apm-extension-ver-1-0-2-x86\_64:1, arn:aws:lambda:us-west-2:267093732750:layer:elastic-apm-node-ver-3-38-0:1

The APM is working. Now I'm interested in setting up log correlation. But I'm not sure to get the trace and transaction IDs so I can include them in my logs. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Emily\_Stolfo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emily_stolfo/32/55017_2.png) [@Emily\_Stolfo](https://discuss.elastic.co/u/Emily_Stolfo)\
**Post date:** [December 2, 2022, 11:43am UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/2 "2022-12-02T11:43:21Z")

</div>

Hi @wjensen-godaddy what logging library are you using? Our documentation on how to set up log correlation is [here](https://www.elastic.co/guide/en/apm/agent/nodejs/current/log-correlation.html). You can inject the IDs into the logs using the `apm.currentTraceIds` method. Let me know if that helps or if you need more info.

---

<div class="post-metadata">

**Author:** ![wjensen-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wjensen-godaddy/32/114131_2.png) [@wjensen-godaddy](https://discuss.elastic.co/u/wjensen-godaddy)\
**Post date:** [December 2, 2022, 6:10pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/3 "2022-12-02T18:10:14Z")

</div>

This is what I've tried in my handler:

```auto
import apm from 'elastic-apm-node';

console.log({ currentTraceIds: apm.currentTraceIds });

```

And this is what I see in my logs:

```auto
INFO	{ currentTraceIds: Ids {} }

```

It doesn't look like it can access the trace Ids from the extension layer?

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 2, 2022, 7:44pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/4 "2022-12-02T19:44:18Z")

</div>

@wjensen-godaddy Hi. Is your `console.log(...)` inside the handler function? It will need to be for there to be an active transaction. For example, here is a complete handler file of mine:

```javascript
const apm = require('elastic-apm-node')

exports.handler = async function myPlayHandler (event, context) {
  console.log('currentTraceIds', apm.currentTraceIds)
  return {
    statusCode: 200,
    body: 'hi from myPlayHandler'
  }
}

```

Invoking that Lambda function results in this line in the CloudWatch log output:

```auto
2022-12-02T19:40:53.405000+00:00 2022/12/02/[$LATEST]b42db6975b494355841ad3a4d7ba8ffc 2022-12-02T19:40:53.405Z	55622ea1-6a89-44f1-bac1-8301a2969dc9	INFO	currentTraceIds TransactionIds {
  'trace.id': 'f9ea0cef0435c7baa76c9eddac7ce9d1',
  'transaction.id': '291a35665e5c34bc'
}

```

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 2, 2022, 7:46pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/5 "2022-12-02T19:46:55Z")

</div>

@wjensen-godaddy Also, if you are using pino or winston, we have formatter modules that you can use with them to (a) get ecs-logging formatted output and (b) get automatic log correlation: [Introduction | ECS Logging Node.js Reference | Elastic](https://www.elastic.co/guide/en/ecs-logging/nodejs/master/intro.html)

---

<div class="post-metadata">

**Author:** ![wjensen-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wjensen-godaddy/32/114131_2.png) [@wjensen-godaddy](https://discuss.elastic.co/u/wjensen-godaddy)\
**Post date:** [December 2, 2022, 7:59pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/6 "2022-12-02T19:59:47Z")

</div>

I see. So this _should_ be working. I don't know why it's not working for me. We both seem to be doing the same thing.

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 2, 2022, 8:01pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/7 "2022-12-02T20:01:42Z")

</div>

@wjensen-godaddy  
Could it have to do with your usage of `import ...`? Is this TypeScript code that is being transpiled to JS code that uses commonjs (i.e. `require`) usage?

Or if this is using `import` (ES Modules), then there are various limitations there for the APM agent. I don't know how that would affect this case, however. Can you show a more complete example of your handler code?

---

<div class="post-metadata">

**Author:** ![wjensen-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wjensen-godaddy/32/114131_2.png) [@wjensen-godaddy](https://discuss.elastic.co/u/wjensen-godaddy)\
**Post date:** [December 2, 2022, 8:08pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/8 "2022-12-02T20:08:19Z")

</div>

I'm afraid our handler code is too big to share.

Ya, we're transpiling using commonjs modules.

---

<div class="post-metadata">

**Author:** ![wjensen-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wjensen-godaddy/32/114131_2.png) [@wjensen-godaddy](https://discuss.elastic.co/u/wjensen-godaddy)\
**Post date:** [December 2, 2022, 8:13pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/9 "2022-12-02T20:13:24Z")

</div>

I suspect esbuild might be including 'elastic-apm-node' in the bundle. I have it as a dev dependency but I forgot to mark it as external. Testing that now...

---

<div class="post-metadata">

**Author:** ![wjensen-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wjensen-godaddy/32/114131_2.png) [@wjensen-godaddy](https://discuss.elastic.co/u/wjensen-godaddy)\
**Post date:** [December 2, 2022, 8:32pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/10 "2022-12-02T20:32:37Z")

</div>

Ugh, that was it. 😑

Thanks for the help. It might be nice if the [documentation](https://www.elastic.co/guide/en/apm/agent/nodejs/current/lambda.html) had a section on this. But maybe the solution is obvious to other people. Haha.

---

<div class="post-metadata">

**Author:** ![wjensen-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wjensen-godaddy/32/114131_2.png) [@wjensen-godaddy](https://discuss.elastic.co/u/wjensen-godaddy)\
**Post date:** [December 2, 2022, 8:37pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/11 "2022-12-02T20:37:11Z")

</div>

By "solution", I just mean that it's possible to do log correlation by importing the APM from the extension layer, and calling `apm.currentTraceIds`.

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 2, 2022, 8:38pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/12 "2022-12-02T20:38:10Z")

</div>

Ah, yes, esbuild (or the same impact for any bundler). We have some docs on this starting here: [Starting the agent | APM Node.js Agent Reference [3.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/starting-the-agent.html#start-bundlers)  
But that wouldn't have been obvious for you to find at all.

I really should get a pointer to this info in the troubleshooting guide: [Troubleshooting | APM Node.js Agent Reference [3.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/troubleshooting.html)  
I've opened [add section about bundlers gotchas to the Troubleshooting docs · Issue #3055 · elastic/apm-agent-nodejs · GitHub](https://github.com/elastic/apm-agent-nodejs/issues/3055) to do that eventually.

---

<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:** [December 23, 2022, 4:38pm UTC](https://discuss.elastic.co/t/how-do-i-setup-log-correlation-when-using-extension-layers/320309/13 "2022-12-23T16:38:42Z")

</div>

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