Skip to content

Commit

Permalink
fix(exporter-logs-otlp-proto): programatic headers take precedence ov… (
Browse files Browse the repository at this point in the history
#4351)

* fix(exporter-logs-otlp-proto): programatic headers take precedence over environment variables

* chore: update PR url in changelog

* chore: fix deletion of env var

* fix(exporter-logs-otlp-http): programatic headers take precedence over environment variables

* fix(exporter-trace-otlp-http): programatic headers take precedence over environment variables

* fix(exporter-trace-otlp-proto): programatic headers take precedence over environment variable

* chore: update CHANGELOG

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
Vunovati and pichlermarc committed Jan 3, 2024
1 parent b36ab12 commit ae0a3c5
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 5 deletions.
5 changes: 5 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ All notable changes to experimental packages in this project will be documented

### :boom: Breaking Change

* fix(exporter-logs-otlp-http): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati
* fix(exporter-logs-otlp-proto): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati
* fix(exporter-trace-otlp-http): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati
* fix(exporter-trace-otlp-proto): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati

### :rocket: (Enhancement)

### :bug: (Bug Fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ export class OTLPLogExporter
timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
...config,
});
this.headers = {
...this.headers,
...baggageUtils.parseKeyPairsIntoRecord(
this.headers = Object.assign(
this.headers,
baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
),
};
config.headers
);
}

convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ describe('OTLPLogExporter', () => {
delete envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS;
delete envSource.OTEL_EXPORTER_OTLP_LOGS_TIMEOUT;
});

it('should override headers defined via env with headers defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
const collectorExporter = new OTLPLogExporter({
headers: {
foo: 'constructor',
},
});
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
assert.strictEqual(collectorExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});

describe('getDefaultUrl', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class OTLPLogExporter
this.headers,
baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
)
),
config.headers
);
}
convert(logs: ReadableLogRecord[]): IExportLogsServiceRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ describe('OTLPLogExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
envSource.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = '';
});
it('should override url defined in env with url defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/';
const constructorDefinedEndpoint = 'http://constructor/v1/logs';
const collectorExporter = new OTLPLogExporter({
url: constructorDefinedEndpoint,
});
assert.strictEqual(collectorExporter.url, constructorDefinedEndpoint);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
});
it('should add root path when signal url defined in env contains no path and no root path', () => {
envSource.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = 'http://foo.bar';
const collectorExporter = new OTLPLogExporter();
Expand Down Expand Up @@ -143,6 +152,17 @@ describe('OTLPLogExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should override headers defined via env with headers defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
const collectorExporter = new OTLPLogExporter({
headers: {
foo: 'constructor',
},
});
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
assert.strictEqual(collectorExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});

describe('export', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class OTLPTraceExporter
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
),
...config.headers,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ describe('OTLPTraceExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = '';
});
it('should override url defined in env with url defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://foo.bar';
const constructorDefinedEndpoint = 'http://constructor/v1/traces';
const collectorExporter = new OTLPTraceExporter({
url: constructorDefinedEndpoint,
});
assert.strictEqual(collectorExporter.url, constructorDefinedEndpoint);
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = '';
});
it('should add root path when signal url defined in env contains no path and no root path', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://foo.bar';
const collectorExporter = new OTLPTraceExporter();
Expand Down Expand Up @@ -177,6 +186,17 @@ describe('OTLPTraceExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should override headers defined via env with headers defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
const collectorExporter = new OTLPTraceExporter({
headers: {
foo: 'constructor',
},
});
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
assert.strictEqual(collectorExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should use compression defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip';
const collectorExporter = new OTLPTraceExporter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class OTLPTraceExporter
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
),
...config.headers,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ describe('OTLPTraceExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = '';
});
it('should override url defined in env with url defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/';
const constructorDefinedEndpoint = 'http://constructor/v1/traces';
const collectorExporter = new OTLPTraceExporter({
url: constructorDefinedEndpoint,
});
assert.strictEqual(collectorExporter.url, constructorDefinedEndpoint);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
});
it('should add root path when signal url defined in env contains no path and no root path', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://foo.bar';
const collectorExporter = new OTLPTraceExporter();
Expand Down Expand Up @@ -155,6 +164,17 @@ describe('OTLPTraceExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should override headers defined via env with headers defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
const collectorExporter = new OTLPTraceExporter({
headers: {
foo: 'constructor',
},
});
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
assert.strictEqual(collectorExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});

describe('export', () => {
Expand Down

0 comments on commit ae0a3c5

Please sign in to comment.