Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not use ESM support in lambda environment #4842

Open
nikhileshcr-splunk opened this issue Jul 2, 2024 · 2 comments
Open

Can not use ESM support in lambda environment #4842

nikhileshcr-splunk opened this issue Jul 2, 2024 · 2 comments
Labels
bug Something isn't working priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect triage

Comments

@nikhileshcr-splunk
Copy link

nikhileshcr-splunk commented Jul 2, 2024

What happened?

I am trying to instrument ESM lambdas using the ESM support feature added in #3698 and the feature is not working in the lambda environment.

Steps to Reproduce

  1. Deploy an ESM node lambda using serverless.
  2. I am using Splunk's opentelemetry layer to instrument the lambda which internally uses opentelemetry-js for the instrumentation.
  3. Made sure to pre-load @opentelemetry/instrumentation/hook.mjs in the lambda wrapper.

Expected Result

The lambda should be instrumented and should send traces to the collector.

Actual Result

We are not seeing any traces being sent to the collector.

Additional Details

I reckon there's an issue with import-in-the-middle which internally uses module-details-from-path's parse method to get the baseDir value. It requires modules to be in a node_modules directory to work.
We are getting undefiend for the baseDire value in the hookfn here.
And since the baseDir is undefined, the _onRequire method unexpectedly returns from here.

OpenTelemetry Setup Code

AWS_LAMBDA_EXEC_WRAPPER: nodejs-otel-handler

#!/bin/bash
source /opt/splunk-default-config

export NODE_OPTIONS="${NODE_OPTIONS} --require /opt/wrapper.js --experimental-loader=/opt/nodejs/node_modules/@opentelemetry/instrumentation/hook.mjs"

if [[ $OTEL_RESOURCE_ATTRIBUTES != *"service.name="* ]]; then
  export OTEL_RESOURCE_ATTRIBUTES="service.name=${AWS_LAMBDA_FUNCTION_NAME},${OTEL_RESOURCE_ATTRIBUTES}"
fi

exec "$@"

Splunk's Otel wrapper, https://github.com/signalfx/splunk-otel-lambda/blob/main/nodejs/src/wrapper.ts

package.json

No response

Relevant log output

No response

@nikhileshcr-splunk nikhileshcr-splunk added bug Something isn't working triage labels Jul 2, 2024
@JamieDanielson
Copy link
Member

@nikhileshcr-splunk The links in the Additional Details section look to be linked to a private repo; can you double check and replace with links to code we can access to help out? Thanks!

@JamieDanielson JamieDanielson added the priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect label Jul 10, 2024
@nikhileshcr-splunk
Copy link
Author

Hi @JamieDanielson, my bad. Thanks for the reply and looking into the issue.
I have fixed the links. Please look into it when you have a moment. Appreciate your help.

jj22ee pushed a commit to aws-observability/aws-otel-js-instrumentation that referenced this issue Oct 11, 2024
*Description of changes:*
1. Add `IS_ESM` check in `otel_instrument` wrapper before calling lambda
handler. If it is EMS format, add ESM node-options
[supported](https://github.com/open-telemetry/opentelemetry-js/blob/966ac176af249d86de6cb10feac2306062846768/doc/esm-support.md#esm-options-for-different-versions-of-nodejs)
by OTel community. Added a new wrapper script `otel_instrument_esm` if
the esm auto detection logic is failed, customer can opt-in to tell tell
layer go with ESM instrumentation path.
2. Set a new env var `HANDLER_IS_ESM` for lambda function when ESM is
detected
3. Patch aws-lambda instrumentation, when `IS_ESM` env var is set, apply
ESM supported `InstrumentationNodeModuleDefinition` to patch function
handler, otherwise keep using the existing handler patcher.

Note: this change add a new branch for supporting ESM, the existing
CommonJS path should not be impacted.

1. open-telemetry/opentelemetry-js#4842
2.
open-telemetry/opentelemetry-js-contrib#1942

*Test*
```

2024-10-09T20:38:13.411-07:00 | Instrumenting lambda handler {
-- | --
  | 2024-10-09T20:38:13.411-07:00 | taskRoot: '/var/task',
  | 2024-10-09T20:38:13.411-07:00 | handlerDef: 'index.handler',
  | 2024-10-09T20:38:13.411-07:00 | handler: 'index.handler',
  | 2024-10-09T20:38:13.411-07:00 | moduleRoot: '',
  | 2024-10-09T20:38:13.411-07:00 | module: 'index',
  | 2024-10-09T20:38:13.411-07:00 | filename: '/var/task/index.mjs',
  | 2024-10-09T20:38:13.411-07:00 | functionName: 'handler'
  | 2024-10-09T20:38:13.411-07:00 | }


  | 2024-10-09T20:38:15.386-07:00 | 'cloud.account.id': '252610625673',
-- | -- | --
  | 2024-10-09T20:38:15.386-07:00 | 'aws.is.local.root': true,
  | 2024-10-09T20:38:15.386-07:00 | 'aws.local.service': 'TestESM',
  | 2024-10-09T20:38:15.386-07:00 | 'aws.local.operation': 'TestESM/Handler',
  | 2024-10-09T20:38:15.386-07:00 | 'aws.span.kind': 'LOCAL_ROOT'


```
By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect triage
Projects
None yet
Development

No branches or pull requests

2 participants