From adbac6f187cba0c9d6d0de981ea50a3f5bbbc9f6 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Mon, 15 May 2023 18:03:43 -0400 Subject: [PATCH 1/5] add user agent to grpc metric exporter --- .../src/OTLPMetricExporter.ts | 15 ++++++++++++--- .../test/OTLPMetricExporter.test.ts | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) 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 20f917e543..0ada75d437 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts @@ -32,6 +32,11 @@ import { createExportMetricsServiceRequest, IExportMetricsServiceRequest, } from '@opentelemetry/otlp-transformer'; +import { VERSION } from './version'; + +const USER_AGENT = { + 'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`, +}; class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase< ResourceMetrics, @@ -39,9 +44,13 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase< > { constructor(config?: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions) { super(config); - const headers = baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS - ); + const headers = { + ...USER_AGENT, + ...baggageUtils.parseKeyPairsIntoRecord( + getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS + ), + }; + this.metadata ||= new Metadata(); for (const [k, v] of Object.entries(headers)) { this.metadata.set(k, v); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts index 4b8d326545..9e4e27ecb0 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts @@ -43,6 +43,7 @@ import { IExportMetricsServiceRequest, IResourceMetrics, } from '@opentelemetry/otlp-transformer'; +import { VERSION } from '../src/version'; const metricsServiceProtoPath = 'opentelemetry/proto/collector/metrics/v1/metrics_service.proto'; @@ -314,6 +315,13 @@ describe('when configuring via environment', () => { ); envSource.OTEL_EXPORTER_OTLP_HEADERS = ''; }); + it('should include user agent in header', () => { + const collectorExporter = new OTLPMetricExporter(); + assert.deepStrictEqual( + collectorExporter._otlpExporter.metadata?.get('User-Agent'), + [`OTel-OTLP-Exporter-JavaScript/${VERSION}`] + ); + }); it('should override global headers config with signal headers defined via env', () => { const metadata = new grpc.Metadata(); metadata.set('foo', 'bar'); From 1dde1d83e2fcb4300897a3db7a393a155a3a6673 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Mon, 15 May 2023 18:04:06 -0400 Subject: [PATCH 2/5] add user agent to httpjson metric exporter --- .../src/platform/node/OTLPMetricExporter.ts | 15 ++++++++++----- .../test/node/CollectorMetricExporter.test.ts | 8 ++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts index 980931a6cb..f83e414e70 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts @@ -28,9 +28,13 @@ import { createExportMetricsServiceRequest, IExportMetricsServiceRequest, } from '@opentelemetry/otlp-transformer'; +import { VERSION } from '../../version'; const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/metrics'; const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`; +const USER_AGENT = { + 'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`, +}; class OTLPExporterNodeProxy extends OTLPExporterNodeBase< ResourceMetrics, @@ -38,12 +42,13 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase< > { constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) { super(config); - this.headers = Object.assign( - this.headers, - baggageUtils.parseKeyPairsIntoRecord( + this.headers = { + ...this.headers, + ...USER_AGENT, + ...baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS - ) - ); + ), + }; } convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts index d23ca628f7..042e5ebb4b 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts @@ -50,6 +50,7 @@ import { OTLPExporterNodeConfigBase, } from '@opentelemetry/otlp-exporter-base'; import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; +import { VERSION } from '../../src/version'; let fakeRequest: PassThrough; @@ -188,6 +189,13 @@ describe('OTLPMetricExporter - node with json over http', () => { assert.strictEqual(collectorExporter._otlpExporter.headers.foo, 'bar'); envSource.OTEL_EXPORTER_OTLP_HEADERS = ''; }); + it('should include user agent in header', () => { + const collectorExporter = new OTLPMetricExporter(); + assert.strictEqual( + collectorExporter._otlpExporter.headers['User-Agent'], + `OTel-OTLP-Exporter-JavaScript/${VERSION}` + ); + }); it('should override global headers config with signal headers defined via env', () => { envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo'; envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = 'foo=boo'; From 7815bb6b4126598520bfcb0e1b55c04da5955675 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Mon, 15 May 2023 18:04:25 -0400 Subject: [PATCH 3/5] add user agent to httpproto metric exporter --- .../src/OTLPMetricExporter.ts | 15 ++++++++++----- .../test/OTLPMetricExporter.test.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts index c29ae0085c..8d1fb114b3 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts @@ -31,9 +31,13 @@ import { createExportMetricsServiceRequest, IExportMetricsServiceRequest, } from '@opentelemetry/otlp-transformer'; +import { VERSION } from './version'; const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/metrics'; const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`; +const USER_AGENT = { + 'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`, +}; class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase< ResourceMetrics, @@ -41,12 +45,13 @@ class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase< > { constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) { super(config); - this.headers = Object.assign( - this.headers, - baggageUtils.parseKeyPairsIntoRecord( + this.headers = { + ...this.headers, + ...USER_AGENT, + ...baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS - ) - ); + ), + }; } convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts index 05ca183dcc..d3270b6a9f 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts @@ -46,6 +46,7 @@ import { OTLPMetricExporterOptions } from '@opentelemetry/exporter-metrics-otlp- import { Stream, PassThrough } from 'stream'; import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base'; import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; +import { VERSION } from '../src/version'; let fakeRequest: PassThrough; @@ -60,6 +61,16 @@ describe('OTLPMetricExporter - node with proto over http', () => { sinon.restore(); }); + describe('default behavior for headers', () => { + const collectorExporter = new OTLPMetricExporter(); + it('should include user agent in header', () => { + assert.strictEqual( + collectorExporter._otlpExporter.headers['User-Agent'], + `OTel-OTLP-Exporter-JavaScript/${VERSION}` + ); + }); + }); + describe('when configuring via environment', () => { const envSource = process.env; it('should use url defined in env that ends with root path and append version and signal path', () => { From 3f74bca7cbde4095225707db510d5884dca4859c Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Mon, 15 May 2023 18:05:35 -0400 Subject: [PATCH 4/5] update changelog --- experimental/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 3ee06e9fc8..d4c3903cb6 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * feat(otlp-trace-exporters): Add User-Agent header to OTLP trace exporters. [#3790](https://github.com/open-telemetry/opentelemetry-js/pull/3790) @JamieDanielson +* feat(otlp-metric-exporters): Add User-Agent header to OTLP metric exporters. @JamieDanielson ### :boom: Breaking Change From fa4faace3a668ffb404b1fcb0dc8541339b6b689 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Mon, 15 May 2023 18:18:15 -0400 Subject: [PATCH 5/5] update changelog --- experimental/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index d4c3903cb6..19a69846ed 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * feat(otlp-trace-exporters): Add User-Agent header to OTLP trace exporters. [#3790](https://github.com/open-telemetry/opentelemetry-js/pull/3790) @JamieDanielson -* feat(otlp-metric-exporters): Add User-Agent header to OTLP metric exporters. @JamieDanielson +* feat(otlp-metric-exporters): Add User-Agent header to OTLP metric exporters. [#3806](https://github.com/open-telemetry/opentelemetry-js/pull/3806) @JamieDanielson ### :boom: Breaking Change