From cd0d8806e0ed4c9dc68be9773770349c712e5e76 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 24 Apr 2023 10:55:57 +0200 Subject: [PATCH] fix(sdk-node): lazy require @opentelemetry/exporter-jaeger (#3739) Co-authored-by: Daniel Dyla --- CHANGELOG.md | 2 ++ .../src/TracerProviderWithEnvExporter.ts | 18 ++++++++++++++++-- .../opentelemetry-exporter-jaeger/README.md | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55f17b5742..9c4b2f297a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) +* fix(sdk-node): fix initialization in bundled environments by not loading @opentelemetry/exporter-jaeger [#3739](https://github.com/open-telemetry/opentelemetry-js/pull/3739) @pichlermarc + ### :books: (Refine Doc) ### :house: (Internal) diff --git a/experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts b/experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts index add81c7c91..a4b9ee1286 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts @@ -32,7 +32,6 @@ import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/expo import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; import { OTLPTraceExporter as OTLPGrpcTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'; import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; -import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; export class TracerProviderWithEnvExporters extends NodeTracerProvider { private _configuredExporters: SpanExporter[] = []; @@ -68,13 +67,28 @@ export class TracerProviderWithEnvExporters extends NodeTracerProvider { ); } + private static configureJaeger() { + // The JaegerExporter does not support being required in bundled + // environments. By delaying the require statement to here, we only crash when + // the exporter is actually used in such an environment. + try { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); + return new JaegerExporter(); + } catch (e) { + throw new Error( + `Could not instantiate JaegerExporter. This could be due to the JaegerExporter's lack of support for bundling. If possible, use @opentelemetry/exporter-trace-otlp-proto instead. Original Error: ${e}` + ); + } + } + protected static override _registeredExporters = new Map< string, () => SpanExporter >([ ['otlp', () => this.configureOtlp()], ['zipkin', () => new ZipkinExporter()], - ['jaeger', () => new JaegerExporter()], + ['jaeger', () => this.configureJaeger()], ['console', () => new ConsoleSpanExporter()], ]); diff --git a/packages/opentelemetry-exporter-jaeger/README.md b/packages/opentelemetry-exporter-jaeger/README.md index 4adceb9b74..7e662a345e 100644 --- a/packages/opentelemetry-exporter-jaeger/README.md +++ b/packages/opentelemetry-exporter-jaeger/README.md @@ -9,6 +9,8 @@ - `@opentelemetry/exporter-trace-otlp-grpc` - `@opentelemetry/exporter-trace-otlp-http` +**NOTE: Bundling (with e.g. `webpack`, `rollup`, `esbuild`, ...) is not supported by this package. Please use `@opentelemetry/exporter-trace-otlp-proto` instead.** + OpenTelemetry Jaeger Trace Exporter allows the user to send collected traces to Jaeger. [Jaeger](https://jaeger.readthedocs.io/en/latest/), inspired by [Dapper](https://research.google.com/pubs/pub36356.html) and [OpenZipkin](http://zipkin.io/), is a distributed tracing system released as open source by [Uber Technologies](http://uber.github.io/). It is used for monitoring and troubleshooting microservices-based distributed systems, including: