Skip to content

Commit ae0a3c5

Browse files
fix(exporter-logs-otlp-proto): programatic headers take precedence ov… (#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 <[email protected]>
1 parent b36ab12 commit ae0a3c5

File tree

9 files changed

+86
-5
lines changed

9 files changed

+86
-5
lines changed

experimental/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ All notable changes to experimental packages in this project will be documented
66

77
### :boom: Breaking Change
88

9+
* fix(exporter-logs-otlp-http): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati
10+
* fix(exporter-logs-otlp-proto): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati
11+
* fix(exporter-trace-otlp-http): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati
12+
* fix(exporter-trace-otlp-proto): programatic headers take precedence over environment variables [#2370](https://github.com/open-telemetry/opentelemetry-js/pull/4351) @Vunovati
13+
914
### :rocket: (Enhancement)
1015

1116
### :bug: (Bug Fix)

experimental/packages/exporter-logs-otlp-http/src/platform/node/OTLPLogExporter.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ export class OTLPLogExporter
3939
timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
4040
...config,
4141
});
42-
this.headers = {
43-
...this.headers,
44-
...baggageUtils.parseKeyPairsIntoRecord(
42+
this.headers = Object.assign(
43+
this.headers,
44+
baggageUtils.parseKeyPairsIntoRecord(
4545
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
4646
),
47-
};
47+
config.headers
48+
);
4849
}
4950

5051
convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {

experimental/packages/exporter-logs-otlp-http/test/node/OTLPLogExporter.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ describe('OTLPLogExporter', () => {
9494
delete envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS;
9595
delete envSource.OTEL_EXPORTER_OTLP_LOGS_TIMEOUT;
9696
});
97+
98+
it('should override headers defined via env with headers defined in constructor', () => {
99+
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
100+
const collectorExporter = new OTLPLogExporter({
101+
headers: {
102+
foo: 'constructor',
103+
},
104+
});
105+
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
106+
assert.strictEqual(collectorExporter.headers.bar, 'foo');
107+
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
108+
});
97109
});
98110

99111
describe('getDefaultUrl', () => {

experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class OTLPLogExporter
5050
this.headers,
5151
baggageUtils.parseKeyPairsIntoRecord(
5252
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
53-
)
53+
),
54+
config.headers
5455
);
5556
}
5657
convert(logs: ReadableLogRecord[]): IExportLogsServiceRequest {

experimental/packages/exporter-logs-otlp-proto/test/node/OTLPLogExporter.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ describe('OTLPLogExporter - node with proto over http', () => {
9292
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
9393
envSource.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = '';
9494
});
95+
it('should override url defined in env with url defined in constructor', () => {
96+
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/';
97+
const constructorDefinedEndpoint = 'http://constructor/v1/logs';
98+
const collectorExporter = new OTLPLogExporter({
99+
url: constructorDefinedEndpoint,
100+
});
101+
assert.strictEqual(collectorExporter.url, constructorDefinedEndpoint);
102+
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
103+
});
95104
it('should add root path when signal url defined in env contains no path and no root path', () => {
96105
envSource.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = 'http://foo.bar';
97106
const collectorExporter = new OTLPLogExporter();
@@ -143,6 +152,17 @@ describe('OTLPLogExporter - node with proto over http', () => {
143152
envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS = '';
144153
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
145154
});
155+
it('should override headers defined via env with headers defined in constructor', () => {
156+
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
157+
const collectorExporter = new OTLPLogExporter({
158+
headers: {
159+
foo: 'constructor',
160+
},
161+
});
162+
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
163+
assert.strictEqual(collectorExporter.headers.bar, 'foo');
164+
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
165+
});
146166
});
147167

148168
describe('export', () => {

experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class OTLPTraceExporter
4949
...baggageUtils.parseKeyPairsIntoRecord(
5050
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
5151
),
52+
...config.headers,
5253
};
5354
}
5455

experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ describe('OTLPTraceExporter - node with json over http', () => {
118118
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
119119
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = '';
120120
});
121+
it('should override url defined in env with url defined in constructor', () => {
122+
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://foo.bar';
123+
const constructorDefinedEndpoint = 'http://constructor/v1/traces';
124+
const collectorExporter = new OTLPTraceExporter({
125+
url: constructorDefinedEndpoint,
126+
});
127+
assert.strictEqual(collectorExporter.url, constructorDefinedEndpoint);
128+
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = '';
129+
});
121130
it('should add root path when signal url defined in env contains no path and no root path', () => {
122131
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://foo.bar';
123132
const collectorExporter = new OTLPTraceExporter();
@@ -177,6 +186,17 @@ describe('OTLPTraceExporter - node with json over http', () => {
177186
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = '';
178187
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
179188
});
189+
it('should override headers defined via env with headers defined in constructor', () => {
190+
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
191+
const collectorExporter = new OTLPTraceExporter({
192+
headers: {
193+
foo: 'constructor',
194+
},
195+
});
196+
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
197+
assert.strictEqual(collectorExporter.headers.bar, 'foo');
198+
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
199+
});
180200
it('should use compression defined via env', () => {
181201
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip';
182202
const collectorExporter = new OTLPTraceExporter();

experimental/packages/exporter-trace-otlp-proto/src/platform/node/OTLPTraceExporter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class OTLPTraceExporter
5252
...baggageUtils.parseKeyPairsIntoRecord(
5353
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
5454
),
55+
...config.headers,
5556
};
5657
}
5758

experimental/packages/exporter-trace-otlp-proto/test/node/OTLPTraceExporter.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ describe('OTLPTraceExporter - node with proto over http', () => {
103103
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
104104
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = '';
105105
});
106+
it('should override url defined in env with url defined in constructor', () => {
107+
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/';
108+
const constructorDefinedEndpoint = 'http://constructor/v1/traces';
109+
const collectorExporter = new OTLPTraceExporter({
110+
url: constructorDefinedEndpoint,
111+
});
112+
assert.strictEqual(collectorExporter.url, constructorDefinedEndpoint);
113+
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
114+
});
106115
it('should add root path when signal url defined in env contains no path and no root path', () => {
107116
envSource.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://foo.bar';
108117
const collectorExporter = new OTLPTraceExporter();
@@ -155,6 +164,17 @@ describe('OTLPTraceExporter - node with proto over http', () => {
155164
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = '';
156165
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
157166
});
167+
it('should override headers defined via env with headers defined in constructor', () => {
168+
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
169+
const collectorExporter = new OTLPTraceExporter({
170+
headers: {
171+
foo: 'constructor',
172+
},
173+
});
174+
assert.strictEqual(collectorExporter.headers.foo, 'constructor');
175+
assert.strictEqual(collectorExporter.headers.bar, 'foo');
176+
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
177+
});
158178
});
159179

160180
describe('export', () => {

0 commit comments

Comments
 (0)