Skip to content

Commit 3b8210b

Browse files
Merge 5d6fcc4 into 422a36a
2 parents 422a36a + 5d6fcc4 commit 3b8210b

File tree

7 files changed

+61
-13
lines changed

7 files changed

+61
-13
lines changed

experimental/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to experimental packages in this project will be documented
44

55
## Unreleased
66

7+
### :rocket: (Enhancement)
8+
9+
* feat(otlp-trace-exporters): Add User-Agent header to OTLP trace exporters. [#3790](https://github.com/open-telemetry/opentelemetry-js/pull/3790) @JamieDanielson
10+
711
### :boom: Breaking Change
812

913
### :rocket: (Enhancement)

experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import {
2828
createExportTraceServiceRequest,
2929
IExportTraceServiceRequest,
3030
} from '@opentelemetry/otlp-transformer';
31+
import { VERSION } from './version';
32+
33+
const USER_AGENT = {
34+
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
35+
};
3136

3237
/**
3338
* OTLP Trace Exporter for Node
@@ -38,9 +43,12 @@ export class OTLPTraceExporter
3843
{
3944
constructor(config: OTLPGRPCExporterConfigNode = {}) {
4045
super(config);
41-
const headers = baggageUtils.parseKeyPairsIntoRecord(
42-
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
43-
);
46+
const headers = {
47+
...USER_AGENT,
48+
...baggageUtils.parseKeyPairsIntoRecord(
49+
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
50+
),
51+
};
4452
this.metadata ||= new Metadata();
4553
for (const [k, v] of Object.entries(headers)) {
4654
this.metadata.set(k, v);

experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as grpc from '@grpc/grpc-js';
2727
import * as path from 'path';
2828
import * as sinon from 'sinon';
2929
import { OTLPTraceExporter } from '../src';
30+
import { VERSION } from '../src/version';
3031

3132
import {
3233
ensureExportedSpanIsCorrect,
@@ -336,6 +337,12 @@ describe('when configuring via environment', () => {
336337
assert.deepStrictEqual(collectorExporter.metadata?.get('foo'), ['bar']);
337338
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
338339
});
340+
it('should include user agent in header', () => {
341+
const collectorExporter = new OTLPTraceExporter();
342+
assert.deepStrictEqual(collectorExporter.metadata?.get('User-Agent'), [
343+
`OTel-OTLP-Exporter-JavaScript/${VERSION}`,
344+
]);
345+
});
339346
it('should override global headers config with signal headers defined via env', () => {
340347
const metadata = new grpc.Metadata();
341348
metadata.set('foo', 'bar');

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ import {
2626
createExportTraceServiceRequest,
2727
IExportTraceServiceRequest,
2828
} from '@opentelemetry/otlp-transformer';
29+
import { VERSION } from '../../version';
2930

3031
const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/traces';
3132
const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
33+
const USER_AGENT = {
34+
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
35+
};
3236

3337
/**
3438
* Collector Trace Exporter for Node
@@ -39,12 +43,13 @@ export class OTLPTraceExporter
3943
{
4044
constructor(config: OTLPExporterNodeConfigBase = {}) {
4145
super(config);
42-
this.headers = Object.assign(
43-
this.headers,
44-
baggageUtils.parseKeyPairsIntoRecord(
46+
this.headers = {
47+
...this.headers,
48+
...USER_AGENT,
49+
...baggageUtils.parseKeyPairsIntoRecord(
4550
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
46-
)
47-
);
51+
),
52+
};
4853
}
4954

5055
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
import { nextTick } from 'process';
3737
import { MockedResponse } from './nodeHelpers';
3838
import { IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer';
39+
import { VERSION } from '../../src/version';
3940

4041
let fakeRequest: PassThrough;
4142

@@ -160,6 +161,13 @@ describe('OTLPTraceExporter - node with json over http', () => {
160161
assert.strictEqual(collectorExporter.headers.foo, 'bar');
161162
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
162163
});
164+
it('should include user agent in header', () => {
165+
const collectorExporter = new OTLPTraceExporter();
166+
assert.strictEqual(
167+
collectorExporter.headers['User-Agent'],
168+
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
169+
);
170+
});
163171
it('should override global headers config with signal headers defined via env', () => {
164172
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
165173
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = 'foo=boo';

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ import {
2929
createExportTraceServiceRequest,
3030
IExportTraceServiceRequest,
3131
} from '@opentelemetry/otlp-transformer';
32+
import { VERSION } from '../../version';
3233

3334
const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/traces';
3435
const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
36+
const USER_AGENT = {
37+
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
38+
};
3539

3640
/**
3741
* Collector Trace Exporter for Node with protobuf
@@ -42,12 +46,13 @@ export class OTLPTraceExporter
4246
{
4347
constructor(config: OTLPExporterNodeConfigBase = {}) {
4448
super(config);
45-
this.headers = Object.assign(
46-
this.headers,
47-
baggageUtils.parseKeyPairsIntoRecord(
49+
this.headers = {
50+
...this.headers,
51+
...USER_AGENT,
52+
...baggageUtils.parseKeyPairsIntoRecord(
4853
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
49-
)
50-
);
54+
),
55+
};
5156
}
5257

5358
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
ServiceClientType,
4040
} from '@opentelemetry/otlp-proto-exporter-base';
4141
import { IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer';
42+
import { VERSION } from '../../src/version';
4243

4344
let fakeRequest: PassThrough;
4445

@@ -52,6 +53,16 @@ describe('OTLPTraceExporter - node with proto over http', () => {
5253
sinon.restore();
5354
});
5455

56+
describe('default behavior for headers', () => {
57+
const collectorExporter = new OTLPTraceExporter();
58+
it('should include user agent in header', () => {
59+
assert.strictEqual(
60+
collectorExporter.headers['User-Agent'],
61+
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
62+
);
63+
});
64+
});
65+
5566
describe('when configuring via environment', () => {
5667
const envSource = process.env;
5768
it('should use url defined in env that ends with root path and append version and signal path', () => {

0 commit comments

Comments
 (0)