Skip to content

Commit

Permalink
fix: programmatic url and headers take precedence in metric exporters… (
Browse files Browse the repository at this point in the history
#4334)

* fix: programmatic url and headers take precedence in metric exporters (#2370)

* chore: adjust grpc exporter metrics test

* chore(changelog): update changelog
  • Loading branch information
Vunovati authored Dec 6, 2023
1 parent 104a5e8 commit 4daa264
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to experimental packages in this project will be documented

### :boom: Breaking Change

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

### :rocket: (Enhancement)

* feat(sdk-logs): add droppedAttributesCount field to ReadableLogRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase<
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
),
...config?.headers,
};

this.metadata ||= new Metadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ describe('when configuring via environment', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = '';
});
it('should use override url defined in env with url defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/v1/metrics';
const constructorDefinedEndpoint = 'http://constructor/v1/metrics';
const collectorExporter = new OTLPMetricExporter({
url: constructorDefinedEndpoint,
});
assert.strictEqual(collectorExporter._otlpExporter.url, 'constructor');
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
});
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const collectorExporter = new OTLPMetricExporter();
Expand Down Expand Up @@ -363,6 +372,23 @@ describe('when configuring via environment', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_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 OTLPMetricExporter({
headers: {
foo: 'constructor',
},
});
assert.deepStrictEqual(
collectorExporter._otlpExporter.metadata?.get('foo'),
['constructor']
);
assert.deepStrictEqual(
collectorExporter._otlpExporter.metadata?.get('bar'),
['foo']
);
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});

testOTLPMetricExporter({ useTLS: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase<
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
),
...config?.headers,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ describe('OTLPMetricExporter - node with json over http', () => {
);
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = '';
});
it('should use override url defined in env with url defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/v1/metrics';
const constructorDefinedEndpoint = 'http://constructor/v1/metrics';
const collectorExporter = new OTLPMetricExporter({
url: constructorDefinedEndpoint,
});
assert.strictEqual(
collectorExporter._otlpExporter.url,
constructorDefinedEndpoint
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
});
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const collectorExporter = new OTLPMetricExporter();
Expand All @@ -318,6 +330,20 @@ describe('OTLPMetricExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_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 OTLPMetricExporter({
headers: {
foo: 'constructor',
},
});
assert.strictEqual(
collectorExporter._otlpExporter.headers.foo,
'constructor'
);
assert.strictEqual(collectorExporter._otlpExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should use delta temporality defined via env', () => {
for (const envValue of ['delta', 'DELTA', 'DeLTa', 'delta ']) {
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = envValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase<
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
),
...config?.headers,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ describe('OTLPMetricExporter - node with proto over http', () => {
);
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = '';
});
it('should use override url defined in env with url defined in constructor', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/v1/metrics';
const constructorDefinedEndpoint = 'http://constructor/v1/metrics';
const collectorExporter = new OTLPMetricExporter({
url: constructorDefinedEndpoint,
});
assert.strictEqual(
collectorExporter._otlpExporter.url,
constructorDefinedEndpoint
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
});
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const collectorExporter = new OTLPMetricExporter();
Expand All @@ -163,6 +175,20 @@ describe('OTLPMetricExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_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 OTLPMetricExporter({
headers: {
foo: 'constructor',
},
});
assert.strictEqual(
collectorExporter._otlpExporter.headers.foo,
'constructor'
);
assert.strictEqual(collectorExporter._otlpExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});

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

0 comments on commit 4daa264

Please sign in to comment.