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

@opentelemetry/instrumentation/hook.mjs crashes when loading a module containing export * from syntax #4547

Closed
jaydenseric opened this issue Mar 14, 2024 · 3 comments · Fixed by #4745
Labels
bug Something isn't working pkg:instrumentation priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc

Comments

@jaydenseric
Copy link

What happened?

Steps to Reproduce

Create a Node.js project with a package.json with the dependency @opentelemetry/instrumentation v0.49.1 properly installed.

Then, in a.mjs:

export const a = 1;
export const b = 2;

In b.mjs:

export * from "./a.mjs";

Then, with the the current version of the Node.js CLI (i.e. Node.js v21.7.1) run:

node --experimental-loader=@opentelemetry/instrumentation/hook.mjs b.mjs

Expected Result

Node.js script runs and exits without an error.

Actual Result

The Node.js script exits with an error, and this console output:

(node:33595) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("%40opentelemetry/instrumentation/hook.mjs", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
file://[redacted]/b.mjs?iitm=true:6
let $* = namespace.*
     ^

SyntaxError: Unexpected token '*'
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:168:18)
    at callTranslator (node:internal/modules/esm/loader:279:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:285:30)
    at async link (node:internal/modules/esm/module_job:78:21)

Node.js v21.7.1

Additional Details

This is a really critical bug that's blocking us from instrumenting a Node.js project at work; it makes it impossible to instrument ESM dependencies like koa via @opentelemetry/instrumentation-koa in an ESM project.

OpenTelemetry Setup Code

// instrumentation.mts

import { AzureMonitorTraceExporter } from "@azure/monitor-opentelemetry-exporter";
import { createAzureSdkInstrumentation } from "@azure/opentelemetry-instrumentation-azure-sdk";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { GraphQLInstrumentation } from "@opentelemetry/instrumentation-graphql";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { KoaInstrumentation } from "@opentelemetry/instrumentation-koa";
import { Resource } from "@opentelemetry/resources";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions";

import getOpenTelemetryServiceName from "../env/getOpenTelemetryServiceName.mjs";

const OTEL_SERVICE_NAME = getOpenTelemetryServiceName();

const tracerProvider = new NodeTracerProvider({
  resource: new Resource({
    // The OpenTelemetry “service name” becomes the Azure Application Insights
    // “cloud role name”.
    // https://learn.microsoft.com/en-us/javascript/api/overview/azure/monitor-opentelemetry-readme?view=azure-node-latest#set-the-cloud-role-name-and-the-cloud-role-instance
    [SEMRESATTRS_SERVICE_NAME]: OTEL_SERVICE_NAME,
  }),
});

tracerProvider.addSpanProcessor(
  new SimpleSpanProcessor(new AzureMonitorTraceExporter()),
);

tracerProvider.register();

registerInstrumentations({
  instrumentations: [
    new HttpInstrumentation(),
    new KoaInstrumentation(),
    new GraphQLInstrumentation(),
    createAzureSdkInstrumentation(),
  ],
  tracerProvider,
});

package.json

No response

Relevant log output

No response

@jaydenseric jaydenseric added bug Something isn't working triage labels Mar 14, 2024
@pichlermarc
Copy link
Member

@jaydenseric thanks for reaching out.

Unfortunately we currently do not support instrumenting ESM in these Node.js versions as support for these versions has only been added in [email protected] and [email protected]. However, we currently cannot update as both of these new import-in-the-middle versions break our ESM instrumentations (see nodejs/import-in-the-middle#57)

I've opened a series of issues (one in this repo #4553) just now to better explain how a solution to the problem may come about. We're happy to accept contributions on the matter.

Further context:

@pichlermarc pichlermarc added pkg:instrumentation priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc and removed triage labels Mar 18, 2024
@bmxpiku
Copy link

bmxpiku commented Mar 25, 2024

@pichlermarc till which version it should work for ESM project?

@pichlermarc
Copy link
Member

@pichlermarc till which version it should work for ESM project?

AFAIK only Node.js v18.18.2 is working at this point in time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:instrumentation priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants