Muhammad Ahmad
Muhammad Ahmad

Reputation: 1

Load ESM module using import-in-the-middle

I have a nodejs project with type="module". now i want to use opentelemetry instrumentation for distributed tracing. the issue i am facing is opentelmetry doesn't support ESM. in order to do this we need to use custom loader. i tried using custom loader provided in their exampleusing

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

But the issue is that the path name of the file on my computer contains 'an'escape character, and due to this, I got this error: --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url";register("import-in-the-middle/hook.mjs", pathToFileURL("./"));' (Usenode --trace-warnings ...to show where the warning was created) file:///C:/Users/Ahmadd's/Desktop/rec/demo-tracing/index.js?iitm=true:2 import { register } from 'file:///C:/Users/Ahmadd's/Desktop/rec/demo-tracing/node_modules/import-in-the-middle/lib/register.js'

> file:///C:/Users/Ahmadd's/Desktop
>                        ^
>             issue =>   |

I also tried import-in-the-middle using this code:

tracer.cjs

    import { register } from "node:module";
    import { pathToFileURL } from "node:url";

    register("import-in-the-middle/hook.mjs", pathToFileURL("./"));

    import { trace } from "@opentelemetry/api";
    import { registerInstrumentations } from "@opentelemetry/instrumentation";
    import { GrpcInstrumentation } from "@opentelemetry/instrumentation-grpc";
    import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
    import { Resource } from "@opentelemetry/resources";
    import { SimpleSpanProcessor, ConsoleSpanExporter } from "@opentelemetry/sdk-trace-base";
    import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
    import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";

    const CONNECTION_STRING = process.env.APPLICATION_INSIGHTS_CONNECTION_STRING ?? "";

    // Optionally register automatic instrumentation libraries
    registerInstrumentations({
        instrumentations: [new GrpcInstrumentation(), new HttpInstrumentation()],
    });

    const resource = Resource.default().merge(
        new Resource({
            [SEMRESATTRS_SERVICE_NAME]: "user-service",
            [SEMRESATTRS_SERVICE_VERSION]: "1.0",
        }),
    );

    const provider = new NodeTracerProvider({
        resource,
    });


    // All tracings are exported and stored in Azure SDK
    const processor = new SimpleSpanProcessor(new ConsoleSpanExporter());
    provider.addSpanProcessor(processor);
    provider.register();
    const tracer = trace.getTracer("sample-grpc-tracer", "1.0");
    export default tracer;
`

I tried to achieve distributed tracing functionality using opentelemetry but due to no support for ESM, i am facing issues

Upvotes: 0

Views: 488

Answers (0)

Related Questions