Skip to content

Commit

Permalink
fix(exporter-logs-otlp-*): set User-Agent header (#4398)
Browse files Browse the repository at this point in the history
* fix(exporter-logs-otlp-grpc): set User-Agent header

* fix(exporter-logs-otlp-http): set User-Agent header

* fix(exporter-logs-otlp-proto): set User-Agent header

* chore(changelog): update changelog

---------

Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
Vunovati and pichlermarc authored Jan 25, 2024
1 parent 8648313 commit df63272
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
4 changes: 4 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(exporter-logs-otlp-grpc): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
* fix(exporter-logs-otlp-http): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
* fix(exporter-logs-otlp-proto): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import {
createExportLogsServiceRequest,
IExportLogsServiceRequest,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from './version';

const USER_AGENT = {
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
};

/**
* OTLP Logs Exporter for Node
Expand All @@ -38,9 +43,12 @@ export class OTLPLogExporter
{
constructor(config: OTLPGRPCExporterConfigNode = {}) {
super(config);
const headers = baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
);
const headers = {
...USER_AGENT,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
),
};
this.metadata ||= new Metadata();
for (const [k, v] of Object.entries(headers)) {
this.metadata.set(k, v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
IExportLogsServiceRequest,
IResourceLogs,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from '../src/version';

const logsServiceProtoPath =
'opentelemetry/proto/collector/logs/v1/logs_service.proto';
Expand Down Expand Up @@ -333,6 +334,12 @@ describe('when configuring via environment', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
envSource.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = '';
});
it('should include user-agent header by default', () => {
const collectorExporter = new OTLPLogExporter();
assert.deepStrictEqual(collectorExporter.metadata?.get('User-Agent'), [
`OTel-OTLP-Exporter-JavaScript/${VERSION}`,
]);
});
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const collectorExporter = new OTLPLogExporter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import { OTLPExporterNodeBase } from '@opentelemetry/otlp-exporter-base';
import { createExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';

import { getDefaultUrl } from '../config';
import { VERSION } from '../../version';

const USER_AGENT = {
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
};

/**
* Collector Logs Exporter for Node
Expand All @@ -39,13 +44,14 @@ export class OTLPLogExporter
timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
...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(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import { PassThrough, Stream } from 'stream';
import { IExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';
import { ExportResultCode } from '@opentelemetry/core';
import { VERSION } from '../../src/version';

let fakeRequest: PassThrough;

Expand Down Expand Up @@ -79,6 +80,14 @@ describe('OTLPLogExporter', () => {
assert.ok(exporter instanceof OTLPLogExporter);
});

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 exporter = new OTLPLogExporter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit df63272

Please sign in to comment.