Skip to content

Commit 5d8a094

Browse files
committed
feat!: use serializeres in protobuf and json exporters
1 parent bb60e9f commit 5d8a094

File tree

17 files changed

+169
-302
lines changed

17 files changed

+169
-302
lines changed

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ import type {
1919
LogRecordExporter,
2020
} from '@opentelemetry/sdk-logs';
2121
import type { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
22-
import type { IExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';
22+
import type {
23+
IExportLogsServiceRequest,
24+
IExportLogsServiceResponse,
25+
} from '@opentelemetry/otlp-transformer';
2326
import { getEnv, baggageUtils } from '@opentelemetry/core';
2427
import { OTLPExporterNodeBase } from '@opentelemetry/otlp-exporter-base';
25-
import { createExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';
28+
import { JsonLogsSerializer } from '@opentelemetry/otlp-transformer';
2629

2730
import { getDefaultUrl } from '../config';
2831
import { VERSION } from '../../version';
@@ -35,15 +38,23 @@ const USER_AGENT = {
3538
* Collector Logs Exporter for Node
3639
*/
3740
export class OTLPLogExporter
38-
extends OTLPExporterNodeBase<ReadableLogRecord, IExportLogsServiceRequest>
41+
extends OTLPExporterNodeBase<
42+
ReadableLogRecord,
43+
IExportLogsServiceRequest,
44+
IExportLogsServiceResponse
45+
>
3946
implements LogRecordExporter
4047
{
4148
constructor(config: OTLPExporterNodeConfigBase = {}) {
4249
// load OTEL_EXPORTER_OTLP_LOGS_TIMEOUT env
43-
super({
44-
timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
45-
...config,
46-
});
50+
super(
51+
{
52+
timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
53+
...config,
54+
},
55+
JsonLogsSerializer,
56+
'application/json'
57+
);
4758
this.headers = {
4859
...this.headers,
4960
...USER_AGENT,
@@ -54,13 +65,6 @@ export class OTLPLogExporter
5465
};
5566
}
5667

57-
convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
58-
return createExportLogsServiceRequest(logRecords, {
59-
useHex: true,
60-
useLongBits: false,
61-
});
62-
}
63-
6468
getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
6569
return getDefaultUrl(config);
6670
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ import {
1919
OTLPExporterConfigBase,
2020
appendResourcePathToUrl,
2121
appendRootPathToUrlIfNeeded,
22+
OTLPExporterNodeBase,
2223
} from '@opentelemetry/otlp-exporter-base';
24+
import { ServiceClientType } from '@opentelemetry/otlp-proto-exporter-base';
2325
import {
24-
OTLPProtoExporterNodeBase,
25-
ServiceClientType,
26-
} from '@opentelemetry/otlp-proto-exporter-base';
27-
import {
28-
createExportLogsServiceRequest,
2926
IExportLogsServiceRequest,
27+
IExportLogsServiceResponse,
28+
ProtobufLogsSerializer,
3029
} from '@opentelemetry/otlp-transformer';
3130

3231
import { ReadableLogRecord, LogRecordExporter } from '@opentelemetry/sdk-logs';
@@ -43,14 +42,15 @@ const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURC
4342
* Collector Trace Exporter for Node
4443
*/
4544
export class OTLPLogExporter
46-
extends OTLPProtoExporterNodeBase<
45+
extends OTLPExporterNodeBase<
4746
ReadableLogRecord,
48-
IExportLogsServiceRequest
47+
IExportLogsServiceRequest,
48+
IExportLogsServiceResponse
4949
>
5050
implements LogRecordExporter
5151
{
5252
constructor(config: OTLPExporterConfigBase = {}) {
53-
super(config);
53+
super(config, ProtobufLogsSerializer, 'application/x-protobuf');
5454
this.headers = {
5555
...this.headers,
5656
...USER_AGENT,
@@ -60,9 +60,6 @@ export class OTLPLogExporter
6060
...config.headers,
6161
};
6262
}
63-
convert(logs: ReadableLogRecord[]): IExportLogsServiceRequest {
64-
return createExportLogsServiceRequest(logs);
65-
}
6663

6764
getDefaultUrl(config: OTLPExporterConfigBase): string {
6865
return typeof config.url === 'string'

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ describe('OTLPLogExporter - node with proto over http', () => {
193193
});
194194

195195
it('should open the connection', done => {
196-
collectorExporter.export(logs, () => {});
197-
198196
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
199197
assert.strictEqual(options.hostname, 'foo.bar.com');
200198
assert.strictEqual(options.method, 'POST');
@@ -206,11 +204,10 @@ describe('OTLPLogExporter - node with proto over http', () => {
206204
done();
207205
return fakeRequest as any;
208206
});
207+
collectorExporter.export(logs, () => {});
209208
});
210209

211210
it('should set custom headers', done => {
212-
collectorExporter.export(logs, () => {});
213-
214211
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
215212
assert.strictEqual(options.headers['foo'], 'bar');
216213

@@ -220,11 +217,10 @@ describe('OTLPLogExporter - node with proto over http', () => {
220217
done();
221218
return fakeRequest as any;
222219
});
220+
collectorExporter.export(logs, () => {});
223221
});
224222

225223
it('should have keep alive and keepAliveMsecs option set', done => {
226-
collectorExporter.export(logs, () => {});
227-
228224
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
229225
assert.strictEqual(options.agent.keepAlive, true);
230226
assert.strictEqual(options.agent.options.keepAliveMsecs, 2000);
@@ -235,6 +231,7 @@ describe('OTLPLogExporter - node with proto over http', () => {
235231
done();
236232
return fakeRequest as any;
237233
});
234+
collectorExporter.export(logs, () => {});
238235
});
239236

240237
it('should successfully send the logs', done => {
@@ -271,35 +268,35 @@ describe('OTLPLogExporter - node with proto over http', () => {
271268
// Need to stub/spy on the underlying logger as the "diag" instance is global
272269
const spyLoggerError = sinon.stub(diag, 'error');
273270

274-
collectorExporter.export(logs, result => {
275-
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
276-
assert.strictEqual(spyLoggerError.args.length, 0);
277-
done();
278-
});
279-
280271
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
281272
const mockRes = new MockedResponse(200);
282273
cb(mockRes);
283274
mockRes.send('success');
284275
return fakeRequest as any;
285276
});
286-
});
287277

288-
it('should log the error message', done => {
289278
collectorExporter.export(logs, result => {
290-
assert.strictEqual(result.code, ExportResultCode.FAILED);
291-
// @ts-expect-error verify error code
292-
assert.strictEqual(result.error.code, 400);
279+
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
280+
assert.strictEqual(spyLoggerError.args.length, 0);
293281
done();
294282
});
283+
});
295284

285+
it('should log the error message', done => {
296286
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
297287
const mockResError = new MockedResponse(400);
298288
cb(mockResError);
299289
mockResError.send('failed');
300290

301291
return fakeRequest as any;
302292
});
293+
294+
collectorExporter.export(logs, result => {
295+
assert.strictEqual(result.code, ExportResultCode.FAILED);
296+
// @ts-expect-error verify error code
297+
assert.strictEqual(result.error.code, 400);
298+
done();
299+
});
303300
});
304301
});
305302
describe('export - with compression', () => {

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import {
2323
appendRootPathToUrlIfNeeded,
2424
} from '@opentelemetry/otlp-exporter-base';
2525
import {
26-
createExportTraceServiceRequest,
2726
IExportTraceServiceRequest,
27+
IExportTraceServiceResponse,
2828
} from '@opentelemetry/otlp-transformer';
2929
import { VERSION } from '../../version';
30+
import { JsonTraceSerializer } from '@opentelemetry/otlp-transformer';
3031

3132
const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/traces';
3233
const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
@@ -38,11 +39,15 @@ const USER_AGENT = {
3839
* Collector Trace Exporter for Node
3940
*/
4041
export class OTLPTraceExporter
41-
extends OTLPExporterNodeBase<ReadableSpan, IExportTraceServiceRequest>
42+
extends OTLPExporterNodeBase<
43+
ReadableSpan,
44+
IExportTraceServiceRequest,
45+
IExportTraceServiceResponse
46+
>
4247
implements SpanExporter
4348
{
4449
constructor(config: OTLPExporterNodeConfigBase = {}) {
45-
super(config);
50+
super(config, JsonTraceSerializer, 'application/json');
4651
this.headers = {
4752
...this.headers,
4853
...USER_AGENT,
@@ -53,13 +58,6 @@ export class OTLPTraceExporter
5358
};
5459
}
5560

56-
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
57-
return createExportTraceServiceRequest(spans, {
58-
useHex: true,
59-
useLongBits: false,
60-
});
61-
}
62-
6361
getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
6462
return typeof config.url === 'string'
6563
? config.url

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import {
2020
OTLPExporterNodeConfigBase,
2121
appendResourcePathToUrl,
2222
appendRootPathToUrlIfNeeded,
23+
OTLPExporterNodeBase,
2324
} from '@opentelemetry/otlp-exporter-base';
25+
import { ServiceClientType } from '@opentelemetry/otlp-proto-exporter-base';
2426
import {
25-
OTLPProtoExporterNodeBase,
26-
ServiceClientType,
27-
} from '@opentelemetry/otlp-proto-exporter-base';
28-
import {
29-
createExportTraceServiceRequest,
3027
IExportTraceServiceRequest,
28+
IExportTraceServiceResponse,
29+
ProtobufTraceSerializer,
3130
} from '@opentelemetry/otlp-transformer';
3231
import { VERSION } from '../../version';
3332

@@ -41,11 +40,15 @@ const USER_AGENT = {
4140
* Collector Trace Exporter for Node with protobuf
4241
*/
4342
export class OTLPTraceExporter
44-
extends OTLPProtoExporterNodeBase<ReadableSpan, IExportTraceServiceRequest>
43+
extends OTLPExporterNodeBase<
44+
ReadableSpan,
45+
IExportTraceServiceRequest,
46+
IExportTraceServiceResponse
47+
>
4548
implements SpanExporter
4649
{
4750
constructor(config: OTLPExporterNodeConfigBase = {}) {
48-
super(config);
51+
super(config, ProtobufTraceSerializer, 'application/x-protobuf');
4952
this.headers = {
5053
...this.headers,
5154
...USER_AGENT,
@@ -56,10 +59,6 @@ export class OTLPTraceExporter
5659
};
5760
}
5861

59-
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
60-
return createExportTraceServiceRequest(spans);
61-
}
62-
6362
getDefaultUrl(config: OTLPExporterNodeConfigBase) {
6463
return typeof config.url === 'string'
6564
? config.url

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ describe('OTLPTraceExporter - node with proto over http', () => {
197197
});
198198

199199
it('should open the connection', done => {
200-
collectorExporter.export(spans, () => {});
201-
202200
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
203201
assert.strictEqual(options.hostname, 'foo.bar.com');
204202
assert.strictEqual(options.method, 'POST');
@@ -210,11 +208,11 @@ describe('OTLPTraceExporter - node with proto over http', () => {
210208
done();
211209
return fakeRequest as any;
212210
});
213-
});
214211

215-
it('should set custom headers', done => {
216212
collectorExporter.export(spans, () => {});
213+
});
217214

215+
it('should set custom headers', done => {
218216
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
219217
assert.strictEqual(options.headers['foo'], 'bar');
220218

@@ -224,11 +222,11 @@ describe('OTLPTraceExporter - node with proto over http', () => {
224222
done();
225223
return fakeRequest as any;
226224
});
227-
});
228225

229-
it('should have keep alive and keepAliveMsecs option set', done => {
230226
collectorExporter.export(spans, () => {});
227+
});
231228

229+
it('should have keep alive and keepAliveMsecs option set', done => {
232230
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
233231
assert.strictEqual(options.agent.keepAlive, true);
234232
assert.strictEqual(options.agent.options.keepAliveMsecs, 2000);
@@ -239,6 +237,8 @@ describe('OTLPTraceExporter - node with proto over http', () => {
239237
done();
240238
return fakeRequest as any;
241239
});
240+
241+
collectorExporter.export(spans, () => {});
242242
});
243243

244244
it('should successfully send the spans', done => {
@@ -275,35 +275,35 @@ describe('OTLPTraceExporter - node with proto over http', () => {
275275
// Need to stub/spy on the underlying logger as the "diag" instance is global
276276
const spyLoggerError = sinon.stub(diag, 'error');
277277

278-
collectorExporter.export(spans, result => {
279-
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
280-
assert.strictEqual(spyLoggerError.args.length, 0);
281-
done();
282-
});
283-
284278
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
285279
const mockRes = new MockedResponse(200);
286280
cb(mockRes);
287281
mockRes.send('success');
288282
return fakeRequest as any;
289283
});
290-
});
291284

292-
it('should log the error message', done => {
293285
collectorExporter.export(spans, result => {
294-
assert.strictEqual(result.code, ExportResultCode.FAILED);
295-
// @ts-expect-error verify error code
296-
assert.strictEqual(result.error.code, 400);
286+
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
287+
assert.strictEqual(spyLoggerError.args.length, 0);
297288
done();
298289
});
290+
});
299291

292+
it('should log the error message', done => {
300293
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
301294
const mockResError = new MockedResponse(400);
302295
cb(mockResError);
303296
mockResError.send('failed');
304297

305298
return fakeRequest as any;
306299
});
300+
301+
collectorExporter.export(spans, result => {
302+
assert.strictEqual(result.code, ExportResultCode.FAILED);
303+
// @ts-expect-error verify error code
304+
assert.strictEqual(result.error.code, 400);
305+
done();
306+
});
307307
});
308308
});
309309
describe('export - with compression', () => {

0 commit comments

Comments
 (0)