From e834533eff4fc371dde98990cc05cb43e6edadd1 Mon Sep 17 00:00:00 2001 From: Vladimir Adamic Date: Wed, 3 Jan 2024 23:22:20 +0100 Subject: [PATCH] fix(exporter-logs-otlp-proto): set User-Agent header --- .../src/platform/node/OTLPLogExporter.ts | 16 +++++++++++----- .../test/node/OTLPLogExporter.test.ts | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts b/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts index a8cdfc4c969..caec7352fff 100644 --- a/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts +++ b/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts @@ -30,6 +30,11 @@ import { } from '@opentelemetry/otlp-transformer'; import { ReadableLogRecord, LogRecordExporter } from '@opentelemetry/sdk-logs'; +import { VERSION } from '../../version'; + +const USER_AGENT = { + 'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`, +}; const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/logs'; const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`; @@ -46,13 +51,14 @@ export class OTLPLogExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config); - this.headers = Object.assign( - this.headers, - baggageUtils.parseKeyPairsIntoRecord( + this.headers = { + ...this.headers, + ...USER_AGENT, + ...baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS ), - config.headers - ); + ...config.headers, + }; } convert(logs: ReadableLogRecord[]): IExportLogsServiceRequest { return createExportLogsServiceRequest(logs); diff --git a/experimental/packages/exporter-logs-otlp-proto/test/node/OTLPLogExporter.test.ts b/experimental/packages/exporter-logs-otlp-proto/test/node/OTLPLogExporter.test.ts index ab918c992b6..9778c950464 100644 --- a/experimental/packages/exporter-logs-otlp-proto/test/node/OTLPLogExporter.test.ts +++ b/experimental/packages/exporter-logs-otlp-proto/test/node/OTLPLogExporter.test.ts @@ -39,6 +39,7 @@ import { } from '@opentelemetry/otlp-proto-exporter-base'; import { IExportLogsServiceRequest } from '@opentelemetry/otlp-transformer'; import { ReadableLogRecord } from '@opentelemetry/sdk-logs'; +import { VERSION } from '../../src/version'; let fakeRequest: PassThrough; @@ -137,6 +138,13 @@ describe('OTLPLogExporter - node with proto over http', () => { ); envSource.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = ''; }); + it('should include user-agent header by default', () => { + const exporter = new OTLPLogExporter(); + assert.strictEqual( + exporter.headers['User-Agent'], + `OTel-OTLP-Exporter-JavaScript/${VERSION}` + ); + }); it('should use headers defined via env', () => { envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS = 'foo=bar'; const collectorExporter = new OTLPLogExporter();