我正在尝试为我的 Nestjs 应用程序使用 otel 指标。
出现错误:
MetricReader can not be bound to a MeterProvider again.
at PeriodicExportingMetricReader.setMetricProducer (/Users//metrics-otel/node_modules/@opentelemetry/sdk-metrics/src/export/MetricReader.ts:93:13)
at MeterProvider.addMetricReader (/Users/metrics-otel/node_modules/@opentelemetry/sdk-metrics/src/MeterProvider.ts:97:18)
at new MeterProvider (/Users/metrics-otel/node_modules/@opentelemetry/sdk-metrics/src/MeterProvider.ts:61:14)
at NodeSDK.start (/Users/metrics-otel/node_modules/@opentelemetry/sdk-node/src/sdk.ts:279:29)
at bootstrap (/Users/metrics-otel/src/main.ts:6:17)
at Object.<anonymous> (/Users/metrics-otel/src/main.ts:10:1)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Function.Module._load (node:internal/modules/cjs/loader:960:12)
重现步骤:
/*instrumentation.ts*/
import { NodeSDK } from '@opentelemetry/sdk-node';
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import {
PeriodicExportingMetricReader,
ConsoleMetricExporter,
} from '@opentelemetry/sdk-metrics';
export const otelSDK = new NodeSDK({
traceExporter: new ConsoleSpanExporter(),
metricReader: new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter(),
}),
instrumentations: [getNodeAutoInstrumentations()],
});
otelSDK.start();
main.ts
import { otelSDK } from './metrics';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
await otelSDK.start();
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
我的 package.json 看起来像这样
“依赖关系”:{ "@opentelemetry/api": "^1.8.0", "@opentelemetry/auto-instrumentations-node": "^0.46.1", "@opentelemetry/sdk-metrics": "^1.24.1", "@opentelemetry/sdk-node": "^0.51.1", "@opentelemetry/sdk-trace-node": "^1.24.1", },
我关注的文档 -> https://opentelemetry.io/docs/languages/js/getting-started/nodejs/
您两次启动了SDK。
第一次在您的
otelSDK
中导入
main.ts
时
import { otelSDK } from './metrics';
第二次在 main.ts 中调用
bootstrap();
时。
从您的
otelSDK.start();
文件中删除 metrics.ts
。
删除
await otelSDK.start();
函数中 main.ts
中的 bootstrap();
。