javascript 如何设置Opentelemetry - NodeJS -自动 Jmeter 服务名称?

7vux5j2d  于 2023-02-15  发布在  Java
关注(0)|答案(2)|浏览(130)

我在tracing.js中设置了以下配置(文档中的1:1),但我不知道如何设置服务名。现在在Datadog中,我看到的只是“unknown_service”。

const opentelemetry = require("@opentelemetry/sdk-node");
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-http");
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');

//How can I set custom service name?

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

const sdk = new opentelemetry.NodeSDK({
  traceExporter: new OTLPTraceExporter({
    url: process.env.OPENTELEMETRY_URL, 
    headers: {},
  }),
  instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();
yqkkidmi

yqkkidmi1#

有几种方法可以做到这一点:
首先将其添加到代码中:

const opentelemetry = require("@opentelemetry/sdk-node");
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-http");
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');

//How can I set custom service name?

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

const sdk = new opentelemetry.NodeSDK({
  traceExporter: new OTLPTraceExporter({
    url: process.env.OPENTELEMETRY_URL, 
    headers: {},
  }),
  instrumentations: [getNodeAutoInstrumentations()],
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: '<service-name>',
  }),
});

sdk.start();

另一种方法是使用@Jan Garaj推荐的环境变量OTEL_SERVICE_NAME

OTEL_SERVICE_NAME=<service-name>

最后,您还可以使用环境变量OTEL_RESOURCE_ATTRIBUTES

OTEL_RESOURCE_ATTRIBUTES=service.name=<service-name>

这些是OpenTelemetry命名服务的官方方式,如果它们与您正在使用的后端一起工作,则命名为IDK。
它可以与Jager和Zipkin等OSS工具一起使用。

相关问题