Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(otlp-metric-exporters): Add User-Agent header to OTLP metric exporters #3806

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* feat(otlp-trace-exporters): Add User-Agent header to OTLP trace exporters. [#3790](https://github.com/open-telemetry/opentelemetry-js/pull/3790) @JamieDanielson
* feat(otlp-metric-exporters): Add User-Agent header to OTLP metric exporters. [#3806](https://github.com/open-telemetry/opentelemetry-js/pull/3806) @JamieDanielson

### :boom: Breaking Change

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@ import {
createExportMetricsServiceRequest,
IExportMetricsServiceRequest,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from './version';

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

class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase<
ResourceMetrics,
IExportMetricsServiceRequest
> {
constructor(config?: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions) {
super(config);
const headers = baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
);
const headers = {
...USER_AGENT,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_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 @@ -43,6 +43,7 @@ import {
IExportMetricsServiceRequest,
IResourceMetrics,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from '../src/version';

const metricsServiceProtoPath =
'opentelemetry/proto/collector/metrics/v1/metrics_service.proto';
Expand Down Expand Up @@ -314,6 +315,13 @@ describe('when configuring via environment', () => {
);
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should include user agent in header', () => {
const collectorExporter = new OTLPMetricExporter();
assert.deepStrictEqual(
collectorExporter._otlpExporter.metadata?.get('User-Agent'),
[`OTel-OTLP-Exporter-JavaScript/${VERSION}`]
);
});
it('should override global headers config with signal headers defined via env', () => {
const metadata = new grpc.Metadata();
metadata.set('foo', 'bar');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@ import {
createExportMetricsServiceRequest,
IExportMetricsServiceRequest,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from '../../version';

const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/metrics';
const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
const USER_AGENT = {
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
};

class OTLPExporterNodeProxy extends OTLPExporterNodeBase<
ResourceMetrics,
IExportMetricsServiceRequest
> {
constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) {
super(config);
this.headers = Object.assign(
this.headers,
baggageUtils.parseKeyPairsIntoRecord(
this.headers = {
...this.headers,
...USER_AGENT,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
)
);
),
};
}

convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
OTLPExporterNodeConfigBase,
} from '@opentelemetry/otlp-exporter-base';
import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer';
import { VERSION } from '../../src/version';

let fakeRequest: PassThrough;

Expand Down Expand Up @@ -188,6 +189,13 @@ describe('OTLPMetricExporter - node with json over http', () => {
assert.strictEqual(collectorExporter._otlpExporter.headers.foo, 'bar');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should include user agent in header', () => {
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter._otlpExporter.headers['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});
it('should override global headers config with signal headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = 'foo=boo';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,27 @@ import {
createExportMetricsServiceRequest,
IExportMetricsServiceRequest,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from './version';

const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/metrics';
const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
const USER_AGENT = {
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
};

class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase<
ResourceMetrics,
IExportMetricsServiceRequest
> {
constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) {
super(config);
this.headers = Object.assign(
this.headers,
baggageUtils.parseKeyPairsIntoRecord(
this.headers = {
...this.headers,
...USER_AGENT,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
)
);
),
};
}

convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { OTLPMetricExporterOptions } from '@opentelemetry/exporter-metrics-otlp-
import { Stream, PassThrough } from 'stream';
import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer';
import { VERSION } from '../src/version';

let fakeRequest: PassThrough;

Expand All @@ -60,6 +61,16 @@ describe('OTLPMetricExporter - node with proto over http', () => {
sinon.restore();
});

describe('default behavior for headers', () => {
const collectorExporter = new OTLPMetricExporter();
it('should include user agent in header', () => {
assert.strictEqual(
collectorExporter._otlpExporter.headers['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});
});

describe('when configuring via environment', () => {
const envSource = process.env;
it('should use url defined in env that ends with root path and append version and signal path', () => {
Expand Down