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(exporters)!: use transport interface in node.js exporters #4743

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(exporters)!: use transport interface in node.js exporters
  • Loading branch information
pichlermarc committed May 31, 2024
commit fb97582ea86e40909cae09506e80eae6982f743d
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { PassThrough, Stream } from 'stream';
import { IExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';
import { ExportResultCode } from '@opentelemetry/core';
import { VERSION } from '../../src/version';
import {nextTick} from "process";

let fakeRequest: PassThrough;

Expand All @@ -43,7 +44,7 @@ class MockedResponse extends Stream {
super();
}

send(data: string) {
send(data: Uint8Array) {
this.emit('data', data);
this.emit('end');
}
Expand All @@ -65,6 +66,9 @@ describe('OTLPLogExporter', () => {

afterEach(() => {
fakeRequest = new Stream.PassThrough();
Object.defineProperty(fakeRequest, 'setTimeout', {
value: function (_timeout: number) {},
});
sinon.restore();
});

Expand Down Expand Up @@ -152,10 +156,12 @@ describe('OTLPLogExporter', () => {
assert.strictEqual(options.method, 'POST');
assert.strictEqual(options.path, '/');

const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
done();
nextTick(() => {
legendecas marked this conversation as resolved.
Show resolved Hide resolved
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send(Buffer.from('success'));
done();
});
return fakeRequest as any;
});
collectorExporter.export(logs, () => {});
Expand All @@ -165,10 +171,12 @@ describe('OTLPLogExporter', () => {
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
assert.strictEqual(options.headers['foo'], 'bar');

const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
done();
nextTick(() => {
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send(Buffer.from('success'));
done();
});
return fakeRequest as any;
});

Expand All @@ -180,10 +188,12 @@ describe('OTLPLogExporter', () => {
assert.strictEqual(options.agent.keepAlive, true);
assert.strictEqual(options.agent.options.keepAliveMsecs, 2000);

const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
done();
nextTick(() => {
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send(Buffer.from('success'));
done();
});
return fakeRequest as any;
});

Expand All @@ -192,10 +202,13 @@ describe('OTLPLogExporter', () => {

it('should successfully send the logs', done => {
const fakeRequest = new Stream.PassThrough();
sinon.stub(http, 'request').returns(fakeRequest as any);
Object.defineProperty(fakeRequest, 'setTimeout', {
value: function (_timeout: number) {},
});

sinon.stub(http, 'request').returns(fakeRequest as any);
let buff = Buffer.from('');
fakeRequest.on('end', () => {
fakeRequest.on('finish', () => {
const responseBody = buff.toString();
const json = JSON.parse(responseBody) as IExportLogsServiceRequest;
const log1 = json.resourceLogs?.[0].scopeLogs?.[0].logRecords?.[0];
Expand All @@ -222,9 +235,11 @@ describe('OTLPLogExporter', () => {
const spyLoggerError = sinon.stub(diag, 'error');

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

Expand All @@ -237,9 +252,11 @@ describe('OTLPLogExporter', () => {

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');
nextTick(() => {
const mockRes = new MockedResponse(400);
cb(mockRes);
mockRes.send(Buffer.from('failure'));
});

return fakeRequest as any;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class MockedResponse extends Stream {
super();
}

send(data: string) {
send(data: Uint8Array) {
this.emit('data', data);
this.emit('end');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ReadableLogRecord } from '@opentelemetry/sdk-logs';
import { VERSION } from '../../src/version';
import { Root } from 'protobufjs';
import * as path from 'path';
import { nextTick } from 'process';

let fakeRequest: PassThrough;

Expand All @@ -61,6 +62,9 @@ describe('OTLPLogExporter - node with proto over http', () => {

afterEach(() => {
fakeRequest = new Stream.PassThrough();
Object.defineProperty(fakeRequest, 'setTimeout', {
value: function (_timeout: number) {},
});
sinon.restore();
});

Expand Down Expand Up @@ -209,10 +213,12 @@ describe('OTLPLogExporter - node with proto over http', () => {
assert.strictEqual(options.method, 'POST');
assert.strictEqual(options.path, '/');

const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
done();
nextTick(() => {
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send(Buffer.from('success'));
done();
});
return fakeRequest as any;
});
collectorExporter.export(logs, () => {});
Expand All @@ -222,10 +228,12 @@ describe('OTLPLogExporter - node with proto over http', () => {
sinon.stub(http, 'request').callsFake((options: any, cb: any) => {
assert.strictEqual(options.headers['foo'], 'bar');

const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
done();
nextTick(() => {
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send(Buffer.from('success'));
done();
});
return fakeRequest as any;
});
collectorExporter.export(logs, () => {});
Expand All @@ -236,17 +244,22 @@ describe('OTLPLogExporter - node with proto over http', () => {
assert.strictEqual(options.agent.keepAlive, true);
assert.strictEqual(options.agent.options.keepAliveMsecs, 2000);

const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send('success');
done();
nextTick(() => {
const mockRes = new MockedResponse(200);
cb(mockRes);
mockRes.send(Buffer.from('success'));
done();
});
return fakeRequest as any;
});
collectorExporter.export(logs, () => {});
});

it('should successfully send the logs', done => {
const fakeRequest = new Stream.PassThrough();
Object.defineProperty(fakeRequest, 'setTimeout', {
value: function (_timeout: number) {},
});
sinon.stub(http, 'request').returns(fakeRequest as any);

let buff = Buffer.from('');
Expand Down Expand Up @@ -277,9 +290,11 @@ describe('OTLPLogExporter - node with proto over http', () => {
const spyLoggerError = sinon.stub(diag, 'error');

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

Expand All @@ -292,9 +307,11 @@ describe('OTLPLogExporter - node with proto over http', () => {

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');
nextTick(() => {
const mockRes = new MockedResponse(400);
cb(mockRes);
mockRes.send(Buffer.from('failure'));
});

return fakeRequest as any;
});
Expand Down Expand Up @@ -329,6 +346,9 @@ describe('OTLPLogExporter - node with proto over http', () => {

it('should successfully send the logs', done => {
const fakeRequest = new Stream.PassThrough();
Object.defineProperty(fakeRequest, 'setTimeout', {
value: function (_timeout: number) {},
});
sinon.stub(http, 'request').returns(fakeRequest as any);
const spySetHeader = sinon.spy();
(fakeRequest as any).setHeader = spySetHeader;
Expand Down Expand Up @@ -368,7 +388,7 @@ describe('export - real http request destroyed before response received', () =>
setTimeout(() => {
res.statusCode = 200;
res.end();
}, 200);
}, 1000);
});
before(done => {
server.listen(8082, done);
Expand All @@ -386,11 +406,15 @@ describe('export - real http request destroyed before response received', () =>
logs.push(Object.assign({}, mockedReadableLogRecord));

collectorExporter.export(logs, result => {
assert.strictEqual(result.code, ExportResultCode.FAILED);
const error = result.error as OTLPExporterError;
assert.ok(error !== undefined);
assert.strictEqual(error.message, 'Request Timeout');
done();
try {
assert.strictEqual(result.code, ExportResultCode.FAILED);
const error = result.error as OTLPExporterError;
assert.ok(error !== undefined);
assert.strictEqual(error.message, 'Request Timeout');
done();
} catch (e) {
done(e);
}
});
});
it('should log the timeout request error message when timeout is 100', done => {
Expand Down
Loading