如何使用 lightstep/otel-launcher-node 作为 OpenTelemetry 导出器?

How to use lightstep/otel-launcher-node as an OpenTelemetry exporter?

在我使用 lightstep/opentelemetry-exporter-js 之前,我可以同时使用自己的导出器和 Lightstep 导出器。

import { CollectorTraceExporter } from '@opentelemetry/exporter-collector';
import { NodeTracerProvider } from '@opentelemetry/node';
import { BatchSpanProcessor, ConsoleSpanExporter } from '@opentelemetry/tracing';
import { LightstepExporter } from 'lightstep-opentelemetry-exporter';

const initTracer = () => {
  const serviceName = 'server-trace-service';
  const tracerProvider = new NodeTracerProvider({
    plugins: {
      http: {
        enabled: true,
        path: '@opentelemetry/plugin-http',
      },
    },
  });

  tracerProvider.addSpanProcessor(new BatchSpanProcessor(new ConsoleSpanExporter()));
  tracerProvider.addSpanProcessor(
    new BatchSpanProcessor(
      new CollectorTraceExporter({
        serviceName,
      })
    )
  );
  tracerProvider.addSpanProcessor(
    new BatchSpanProcessor(
      new LightstepExporter({
        serviceName,
        token: 'myToken',
      })
    )
  );

  tracerProvider.register();
};

然而,刚刚看到 lightstep/opentelemetry-exporter-js 已被弃用并被 lightstep/otel-launcher-node 取代。

我检查了它的源代码和演示,它看起来像是一个基于 OpenTelemetry 的“框架”。

const {
  lightstep,
  opentelemetry,
} = require('lightstep-opentelemetry-launcher-node');

const sdk = lightstep.configureOpenTelemetry({
  accessToken: 'YOUR ACCESS TOKEN',
  serviceName: 'locl-ex',
});

sdk.start().then(() => {
  const tracer = opentelemetry.trace.getTracer('otel-node-example');
  const span = tracer.startSpan('test-span');
  span.end();

  opentelemetry.trace.getTracerProvider().getActiveSpanProcessor().shutdown();
});

是否可以简单地将其用作 OpenTelemetry 导出器之一?

lightstep-opentelemetry-launcher-node 基本上为您捆绑了所需的东西以便于配置,因此这不是导出器。如果您只是简单地将代码中的“LightstepExporter”替换为“OpenTelemetry Collector Exporter”,您只需这样做

  import { CollectorTraceExporter } from '@opentelemetry/exporter-collector';

  tracerProvider.addSpanProcessor(
    new BatchSpanProcessor(
      new CollectorTraceExporter({
        url: 'YOUR_DIGEST_URL',
        headers: {
          'Lightstep-Access-Token': 'YOUR_TOKEN'
        }
      })
    )
  );

lightstep/otel-launcher-node 中的默认 YOUR_DIGETS_URLhttps://ingest.lightstep.com:443/api/v2/otel/trace