From a49fc1c0e03facae1fc7e96108c643de733a024c Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Fri, 3 Jun 2022 16:03:55 +0200 Subject: [PATCH 1/3] fix(grpc-exporter): keep URL protocol so that gRPC channel security can be determined. --- .../src/OTLPTraceExporter.ts | 20 ++++++++++++------- .../src/OTLPMetricExporter.ts | 20 ++++++++++++------- .../src/OTLPGRPCExporterNodeBase.ts | 1 + .../otlp-grpc-exporter-base/src/util.ts | 8 ++++---- .../test/OTLPGRPCExporterNodeBase.test.ts | 4 ++++ 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts index baafc4773a..f2971b4957 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts @@ -48,13 +48,7 @@ export class OTLPTraceExporter } getDefaultUrl(config: OTLPGRPCExporterConfigNode) { - return typeof config.url === 'string' - ? validateAndNormalizeUrl(config.url) - : getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0 - ? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_ENDPOINT) - : validateAndNormalizeUrl(DEFAULT_COLLECTOR_URL); + return validateAndNormalizeUrl(this.getUrlFromConfig(config)); } getServiceClientType() { @@ -64,4 +58,16 @@ export class OTLPTraceExporter getServiceProtoPath(): string { return 'opentelemetry/proto/collector/trace/v1/trace_service.proto'; } + + getUrlFromConfig(config: OTLPGRPCExporterConfigNode): string { + if (typeof config.url === 'string') { + return config.url; + } else if (getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) { + return getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; + } else if (getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return getEnv().OTEL_EXPORTER_OTLP_ENDPOINT; + } else { + return DEFAULT_COLLECTOR_URL; + } + } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts index 71e6e9b105..e7275f46ee 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts @@ -51,18 +51,24 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase 0 - ? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_ENDPOINT) - : validateAndNormalizeUrl(DEFAULT_COLLECTOR_URL); + return validateAndNormalizeUrl(this.getUrlFromConfig(config)); } convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest { return createExportMetricsServiceRequest(metrics); } + + getUrlFromConfig(config: OTLPGRPCExporterConfigNode): string { + if (typeof config.url === 'string') { + return config.url; + } else if (getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0) { + return getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT; + } else if (getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return getEnv().OTEL_EXPORTER_OTLP_ENDPOINT; + } else { + return DEFAULT_COLLECTOR_URL; + } + } } /** diff --git a/experimental/packages/otlp-grpc-exporter-base/src/OTLPGRPCExporterNodeBase.ts b/experimental/packages/otlp-grpc-exporter-base/src/OTLPGRPCExporterNodeBase.ts index 9cc349fe4e..87d7e01787 100644 --- a/experimental/packages/otlp-grpc-exporter-base/src/OTLPGRPCExporterNodeBase.ts +++ b/experimental/packages/otlp-grpc-exporter-base/src/OTLPGRPCExporterNodeBase.ts @@ -116,4 +116,5 @@ export abstract class OTLPGRPCExporterNodeBase< abstract getServiceProtoPath(): string; abstract getServiceClientType(): ServiceClientType; + abstract getUrlFromConfig(config: OTLPGRPCExporterConfigNode): string; } diff --git a/experimental/packages/otlp-grpc-exporter-base/src/util.ts b/experimental/packages/otlp-grpc-exporter-base/src/util.ts index 313618fa72..1d3791bd38 100644 --- a/experimental/packages/otlp-grpc-exporter-base/src/util.ts +++ b/experimental/packages/otlp-grpc-exporter-base/src/util.ts @@ -33,7 +33,7 @@ export function onInit( ): void { collector.grpcQueue = []; - const credentials: grpc.ChannelCredentials = configureSecurity(config.credentials, collector.url); + const credentials: grpc.ChannelCredentials = configureSecurity(config.credentials, collector.getUrlFromConfig(config)); const includeDirs = [path.resolve(__dirname, '..', 'protos')]; @@ -92,7 +92,7 @@ export function send( collector.serviceClient.export( serviceRequest, collector.metadata || new grpc.Metadata(), - {deadline: deadline}, + { deadline: deadline }, (err: ExportServiceError) => { if (err) { diag.error('Service request', serviceRequest); @@ -225,7 +225,7 @@ function retrieveCertChain(): Buffer | undefined { } function toGrpcCompression(compression: CompressionAlgorithm): GrpcCompressionAlgorithm { - if(compression === CompressionAlgorithm.NONE) + if (compression === CompressionAlgorithm.NONE) return GrpcCompressionAlgorithm.NONE; else if (compression === CompressionAlgorithm.GZIP) return GrpcCompressionAlgorithm.GZIP; @@ -246,6 +246,6 @@ export function configureCompression(compression: CompressionAlgorithm | undefin } else { const definedCompression = getEnv().OTEL_EXPORTER_OTLP_TRACES_COMPRESSION || getEnv().OTEL_EXPORTER_OTLP_COMPRESSION; - return definedCompression === 'gzip' ? GrpcCompressionAlgorithm.GZIP: GrpcCompressionAlgorithm.NONE; + return definedCompression === 'gzip' ? GrpcCompressionAlgorithm.GZIP : GrpcCompressionAlgorithm.NONE; } } diff --git a/experimental/packages/otlp-grpc-exporter-base/test/OTLPGRPCExporterNodeBase.test.ts b/experimental/packages/otlp-grpc-exporter-base/test/OTLPGRPCExporterNodeBase.test.ts index aca3540763..ec28b53011 100644 --- a/experimental/packages/otlp-grpc-exporter-base/test/OTLPGRPCExporterNodeBase.test.ts +++ b/experimental/packages/otlp-grpc-exporter-base/test/OTLPGRPCExporterNodeBase.test.ts @@ -52,6 +52,10 @@ class MockCollectorExporter extends OTLPGRPCExporterNodeBase< getServiceProtoPath(): string { return 'opentelemetry/proto/collector/trace/v1/trace_service.proto'; } + + getUrlFromConfig(config: OTLPGRPCExporterConfigNode): string { + return ''; + } } // Mocked _send which just saves the callbacks for later From 0efe2f8d6c308894bcebcfb4169e12f27205565d Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Fri, 3 Jun 2022 16:36:07 +0200 Subject: [PATCH 2/3] fix(changelog): add changelog entry. --- experimental/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 43686973f4..2662d605ad 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -10,6 +10,8 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) +* fix(grpc-exporter): use non-normalized URL to determine channel security #3019 @pichlermarc + ### :books: (Refine Doc) ### :house: (Internal) From 743aa7c27119e216507473a9a7748a2e3afd5163 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 8 Jun 2022 14:53:47 +0200 Subject: [PATCH 3/3] fix(grpc-exporter): use simplified syntax to determine url --- .../exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts | 10 ++++------ .../src/OTLPMetricExporter.ts | 10 ++++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts index f2971b4957..f4b0554715 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts @@ -62,12 +62,10 @@ export class OTLPTraceExporter getUrlFromConfig(config: OTLPGRPCExporterConfigNode): string { if (typeof config.url === 'string') { return config.url; - } else if (getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) { - return getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; - } else if (getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { - return getEnv().OTEL_EXPORTER_OTLP_ENDPOINT; - } else { - return DEFAULT_COLLECTOR_URL; } + + return getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT || + getEnv().OTEL_EXPORTER_OTLP_ENDPOINT || + DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts index e7275f46ee..90c6ceb92b 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts @@ -61,13 +61,11 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase 0) { - return getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT; - } else if (getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { - return getEnv().OTEL_EXPORTER_OTLP_ENDPOINT; - } else { - return DEFAULT_COLLECTOR_URL; } + + return getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT || + getEnv().OTEL_EXPORTER_OTLP_ENDPOINT || + DEFAULT_COLLECTOR_URL; } }