GoogleCloudPlatform/opentelemetry-operations-js

Basic setup is throwing an error

gajus opened this issue · 3 comments

gajus commented
import { TraceExporter } from '@google-cloud/opentelemetry-cloud-trace-exporter';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';

const nodeTracerProvider = new NodeTracerProvider();

nodeTracerProvider.register();

const exporter = new TraceExporter();

nodeTracerProvider.addSpanProcessor(new BatchSpanProcessor(exporter));

registerInstrumentations({
  instrumentations: [new HttpInstrumentation()],
});

Error:

/Users/gajus/Documents/dev/contra/contra-api/node_modules/@opentelemetry/instrumentation/build/src/instrumentation.js:41
        this._meter = api_1.metrics.getMeter(instrumentationName, instrumentationVersion);
                                    ^


TypeError: Cannot read properties of undefined (reading 'getMeter')
    at InstrumentationAbstract (/Users/gajus/Documents/dev/contra/contra-api/node_modules/@opentelemetry/instrumentation/build/src/instrumentation.js:41:37)
    at InstrumentationBase (/Users/gajus/Documents/dev/contra/contra-api/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:29:9)
    at HttpInstrumentation (/Users/gajus/Documents/dev/contra/contra-api/node_modules/@opentelemetry/instrumentation-http/build/src/http.js:32:9)
    at <anonymous> (/Users/gajus/Documents/dev/contra/contra-api/src/bin/tracing.ts:19:5)
    at Object.<anonymous> (/Users/gajus/Documents/dev/contra/contra-api/src/bin/tracing.ts:22:2)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Object.v (/Users/gajus/Documents/dev/contra/contra-api/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:901)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)

Based on the provided documentation, I would expect this to work. Not clear why it does not.

@gajus I just tested this out an it worked fine, it's probably an issue with your local node_modules folder or package.json. The stacktrace also doesn't mention any of the packages in this repo, just upstream OTel packages.

If that doesn't fix things, I'd recommend opening an issue in https://github.com/open-telemetry/opentelemetry-js with a repro. Closing as "can't repro"

gajus commented

For anyone else reading this, adding trace.setGlobalTracerProvider(nodeTracerProvider); fixes the problem.