Skip to content

Commit

Permalink
feat!: use serializeres in protobuf and json exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Mar 13, 2024
1 parent bb60e9f commit 5d8a094
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import type {
LogRecordExporter,
} from '@opentelemetry/sdk-logs';
import type { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
import type { IExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';
import type {
IExportLogsServiceRequest,
IExportLogsServiceResponse,
} from '@opentelemetry/otlp-transformer';
import { getEnv, baggageUtils } from '@opentelemetry/core';
import { OTLPExporterNodeBase } from '@opentelemetry/otlp-exporter-base';
import { createExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';
import { JsonLogsSerializer } from '@opentelemetry/otlp-transformer';

import { getDefaultUrl } from '../config';
import { VERSION } from '../../version';
Expand All @@ -35,15 +38,23 @@ const USER_AGENT = {
* Collector Logs Exporter for Node
*/
export class OTLPLogExporter
extends OTLPExporterNodeBase<ReadableLogRecord, IExportLogsServiceRequest>
extends OTLPExporterNodeBase<
ReadableLogRecord,
IExportLogsServiceRequest,
IExportLogsServiceResponse
>
implements LogRecordExporter
{
constructor(config: OTLPExporterNodeConfigBase = {}) {
// load OTEL_EXPORTER_OTLP_LOGS_TIMEOUT env
super({
timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
...config,
});
super(
{
timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
...config,
},
JsonLogsSerializer,
'application/json'
);
this.headers = {
...this.headers,
...USER_AGENT,
Expand All @@ -54,13 +65,6 @@ export class OTLPLogExporter
};
}

convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
return createExportLogsServiceRequest(logRecords, {
useHex: true,
useLongBits: false,
});
}

getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
return getDefaultUrl(config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import {
OTLPExporterConfigBase,
appendResourcePathToUrl,
appendRootPathToUrlIfNeeded,
OTLPExporterNodeBase,
} from '@opentelemetry/otlp-exporter-base';
import { ServiceClientType } from '@opentelemetry/otlp-proto-exporter-base';
import {
OTLPProtoExporterNodeBase,
ServiceClientType,
} from '@opentelemetry/otlp-proto-exporter-base';
import {
createExportLogsServiceRequest,
IExportLogsServiceRequest,
IExportLogsServiceResponse,
ProtobufLogsSerializer,
} from '@opentelemetry/otlp-transformer';

import { ReadableLogRecord, LogRecordExporter } from '@opentelemetry/sdk-logs';
Expand All @@ -43,14 +42,15 @@ const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURC
* Collector Trace Exporter for Node
*/
export class OTLPLogExporter
extends OTLPProtoExporterNodeBase<
extends OTLPExporterNodeBase<
ReadableLogRecord,
IExportLogsServiceRequest
IExportLogsServiceRequest,
IExportLogsServiceResponse
>
implements LogRecordExporter
{
constructor(config: OTLPExporterConfigBase = {}) {
super(config);
super(config, ProtobufLogsSerializer, 'application/x-protobuf');
this.headers = {
...this.headers,
...USER_AGENT,
Expand All @@ -60,9 +60,6 @@ export class OTLPLogExporter
...config.headers,
};
}
convert(logs: ReadableLogRecord[]): IExportLogsServiceRequest {
return createExportLogsServiceRequest(logs);
}

getDefaultUrl(config: OTLPExporterConfigBase): string {
return typeof config.url === 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ describe('OTLPLogExporter - node with proto over http', () => {
});

it('should open the connection', done => {
collectorExporter.export(logs, () => {});

sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
assert.strictEqual(options.hostname, 'foo.bar.com');
assert.strictEqual(options.method, 'POST');
Expand All @@ -206,11 +204,10 @@ describe('OTLPLogExporter - node with proto over http', () => {
done();
return fakeRequest as any;
});
collectorExporter.export(logs, () => {});
});

it('should set custom headers', done => {
collectorExporter.export(logs, () => {});

sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
assert.strictEqual(options.headers['foo'], 'bar');

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

it('should have keep alive and keepAliveMsecs option set', done => {
collectorExporter.export(logs, () => {});

sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
assert.strictEqual(options.agent.keepAlive, true);
assert.strictEqual(options.agent.options.keepAliveMsecs, 2000);
Expand All @@ -235,6 +231,7 @@ describe('OTLPLogExporter - node with proto over http', () => {
done();
return fakeRequest as any;
});
collectorExporter.export(logs, () => {});
});

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

collectorExporter.export(logs, result => {
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
assert.strictEqual(spyLoggerError.args.length, 0);
done();
});

sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
return fakeRequest as any;
});
});

it('should log the error message', done => {
collectorExporter.export(logs, result => {
assert.strictEqual(result.code, ExportResultCode.FAILED);
// @ts-expect-error verify error code
assert.strictEqual(result.error.code, 400);
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
assert.strictEqual(spyLoggerError.args.length, 0);
done();
});
});

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

return fakeRequest as any;
});

collectorExporter.export(logs, result => {
assert.strictEqual(result.code, ExportResultCode.FAILED);
// @ts-expect-error verify error code
assert.strictEqual(result.error.code, 400);
done();
});
});
});
describe('export - with compression', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import {
appendRootPathToUrlIfNeeded,
} from '@opentelemetry/otlp-exporter-base';
import {
createExportTraceServiceRequest,
IExportTraceServiceRequest,
IExportTraceServiceResponse,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from '../../version';
import { JsonTraceSerializer } from '@opentelemetry/otlp-transformer';

const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/traces';
const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
Expand All @@ -38,11 +39,15 @@ const USER_AGENT = {
* Collector Trace Exporter for Node
*/
export class OTLPTraceExporter
extends OTLPExporterNodeBase<ReadableSpan, IExportTraceServiceRequest>
extends OTLPExporterNodeBase<
ReadableSpan,
IExportTraceServiceRequest,
IExportTraceServiceResponse
>
implements SpanExporter
{
constructor(config: OTLPExporterNodeConfigBase = {}) {
super(config);
super(config, JsonTraceSerializer, 'application/json');
this.headers = {
...this.headers,
...USER_AGENT,
Expand All @@ -53,13 +58,6 @@ export class OTLPTraceExporter
};
}

convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
return createExportTraceServiceRequest(spans, {
useHex: true,
useLongBits: false,
});
}

getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
return typeof config.url === 'string'
? config.url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import {
OTLPExporterNodeConfigBase,
appendResourcePathToUrl,
appendRootPathToUrlIfNeeded,
OTLPExporterNodeBase,
} from '@opentelemetry/otlp-exporter-base';
import { ServiceClientType } from '@opentelemetry/otlp-proto-exporter-base';
import {
OTLPProtoExporterNodeBase,
ServiceClientType,
} from '@opentelemetry/otlp-proto-exporter-base';
import {
createExportTraceServiceRequest,
IExportTraceServiceRequest,
IExportTraceServiceResponse,
ProtobufTraceSerializer,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from '../../version';

Expand All @@ -41,11 +40,15 @@ const USER_AGENT = {
* Collector Trace Exporter for Node with protobuf
*/
export class OTLPTraceExporter
extends OTLPProtoExporterNodeBase<ReadableSpan, IExportTraceServiceRequest>
extends OTLPExporterNodeBase<
ReadableSpan,
IExportTraceServiceRequest,
IExportTraceServiceResponse
>
implements SpanExporter
{
constructor(config: OTLPExporterNodeConfigBase = {}) {
super(config);
super(config, ProtobufTraceSerializer, 'application/x-protobuf');
this.headers = {
...this.headers,
...USER_AGENT,
Expand All @@ -56,10 +59,6 @@ export class OTLPTraceExporter
};
}

convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
return createExportTraceServiceRequest(spans);
}

getDefaultUrl(config: OTLPExporterNodeConfigBase) {
return typeof config.url === 'string'
? config.url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ describe('OTLPTraceExporter - node with proto over http', () => {
});

it('should open the connection', done => {
collectorExporter.export(spans, () => {});

sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
assert.strictEqual(options.hostname, 'foo.bar.com');
assert.strictEqual(options.method, 'POST');
Expand All @@ -210,11 +208,11 @@ describe('OTLPTraceExporter - node with proto over http', () => {
done();
return fakeRequest as any;
});
});

it('should set custom headers', done => {
collectorExporter.export(spans, () => {});
});

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

Expand All @@ -224,11 +222,11 @@ describe('OTLPTraceExporter - node with proto over http', () => {
done();
return fakeRequest as any;
});
});

it('should have keep alive and keepAliveMsecs option set', done => {
collectorExporter.export(spans, () => {});
});

it('should have keep alive and keepAliveMsecs option set', done => {
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
assert.strictEqual(options.agent.keepAlive, true);
assert.strictEqual(options.agent.options.keepAliveMsecs, 2000);
Expand All @@ -239,6 +237,8 @@ describe('OTLPTraceExporter - node with proto over http', () => {
done();
return fakeRequest as any;
});

collectorExporter.export(spans, () => {});
});

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

collectorExporter.export(spans, result => {
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
assert.strictEqual(spyLoggerError.args.length, 0);
done();
});

sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
return fakeRequest as any;
});
});

it('should log the error message', done => {
collectorExporter.export(spans, result => {
assert.strictEqual(result.code, ExportResultCode.FAILED);
// @ts-expect-error verify error code
assert.strictEqual(result.error.code, 400);
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
assert.strictEqual(spyLoggerError.args.length, 0);
done();
});
});

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

return fakeRequest as any;
});

collectorExporter.export(spans, result => {
assert.strictEqual(result.code, ExportResultCode.FAILED);
// @ts-expect-error verify error code
assert.strictEqual(result.error.code, 400);
done();
});
});
});
describe('export - with compression', () => {
Expand Down
Loading

0 comments on commit 5d8a094

Please sign in to comment.