elastic / elastic/apm-agent-nodejs

get working out of the box with AWS Lambda using esbuild: Agent singleton, esbuild format config, etc.

Open
#3,244 2 comments 0 reactions 0 assignees View on GitHub
agent-nodejs
Dominant language
JavaScript
Stars
594
Forks
244
Avg merge
1d 8h
Merged PRs (30d)
16

Description

This AWS guide pitches using esbuild, sam, and typescript for Lambdas:
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build-typescript.html

[This discuss issue](https://discuss.elastic.co/t/how-to-send-error-message-to-elastic-apm-and-show-the-error-in-kibana-tab/329183) pointed out that attempting to import and use the APM agent in the Lambda handler code does not work.

1. `apm.isStarted()` returns `undefined`, indicating that the in-code import is *not* getting the wanted shared `Agent` singleton from the APM agent started via `NODE_OPTIONS`. A fix for this might be (or at least partially) using `globalThis` for the singleton sharing. The current use of `require.cache` doesn't work with bundled modules.
2. Another workaround might be to specify that the `elastic-apm-node` module be in the esbuild `externals` -- as documented at https://www.elastic.co/guide/en/apm/agent/nodejs/current/starting-the-agent.html#start-esbuild. Can this config be passed through via the SAM configuration? If so, we should document this example.
3. Another possible workaround might be to *not* have `elastic-apm-node` listed in dependencies, but *still* import it **via CommonJS `require`**. That might result in getting the `elastic-apm-node` module provided by the Elastic Lambda layer. This may require configuring a linter and/or esbuild to ignore the attempt to import a module that is not listed in "dependencies".
4. Another separate issue with general instrumentation of modules used in the bundle will likely be (a) if the module is bundled then our instrumentation won't be able to instrument them (this is also discussed at https://www.elastic.co/guide/en/apm/agent/nodejs/current/starting-the-agent.html#start-esbuild) and (b) even if marked external, the default output [format](https://esbuild.github.io/api/#format) of the esbuild may be using ESM which isn't currently supported by the APM agent. The solution to this is to use `Format: cjs` in the SAM config.

I don't know how prevalent this is, but it has come up at least once: https://discuss.elastic.co/t/how-to-send-error-message-to-elastic-apm-and-show-the-error-in-kibana-tab/329183/5
As well, the "esbuild-bundled APM agent not sharing the same singleton Agent instance" has also come up before.

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.