Skip to content

Commit

Permalink
feat(exporter-jaeger): add forceFlush and timeout options (open-telem…
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Sep 23, 2019
1 parent cee42d8 commit 288bf68
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/opentelemetry-exporter-jaeger/src/jaeger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ export class JaegerExporter implements SpanExporter {
private readonly _logger: types.Logger;
private readonly _process: jaegerTypes.ThriftProcess;
private readonly _sender: typeof jaegerTypes.UDPSender;
private readonly _forceFlush: boolean = true;
private readonly _flushTimeout: number;

constructor(config: jaegerTypes.ExporterConfig) {
this._logger = config.logger || new NoopLogger();
const tags: jaegerTypes.Tag[] = config.tags || [];
if (config.forceFlush !== undefined) {
this._forceFlush = config.forceFlush;
}
this._flushTimeout = config.flushTimeout || 2000;

this._sender = new jaegerTypes.UDPSender(config);
this._process = {
Expand All @@ -55,15 +61,18 @@ export class JaegerExporter implements SpanExporter {

/** Shutdown exporter. */
shutdown(): void {
if (!this._forceFlush) return;
// Make an optimistic flush.
this._sender.flush((numSpans: number, err?: string) => {
if (err) {
this._logger.error(`failed to flush span: ${err}`);
}
});
// Sleeping 2 seconds before closing the sender's connection to ensure all spans are flushed.
// Sleeping x seconds before closing the sender's connection to ensure
// all spans are flushed.
setTimeout(() => {
this._sender.close();
}, 2000);
}, this._flushTimeout);
}

/** Transform spans and sends to Jaeger service. */
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-exporter-jaeger/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface ExporterConfig {
host?: string; // default: 'localhost'
port?: number; // default: 6832
maxPacketSize?: number; // default: 65000
forceFlush?: boolean; // default: true
flushTimeout?: number; // default: 2000
}

// Below require is needed as jaeger-client types does not expose the thrift,
Expand Down
35 changes: 35 additions & 0 deletions packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ describe('JaegerExporter', () => {
assert.strictEqual(process.tags[0].vType, 'STRING');
assert.strictEqual(process.tags[0].vStr, '0.0.1');
});

it('should construct an exporter with forceFlush and flushTimeout', () => {
const exporter = new JaegerExporter({
serviceName: 'opentelemetry',
forceFlush: true,
flushTimeout: 5000,
});
assert.ok(typeof exporter.export === 'function');
assert.ok(typeof exporter.shutdown === 'function');

assert.ok(exporter['_forceFlush']);
assert.strictEqual(exporter['_flushTimeout'], 5000);
});

it('should construct an exporter without forceFlush and flushTimeout', () => {
const exporter = new JaegerExporter({
serviceName: 'opentelemetry',
});
assert.ok(typeof exporter.export === 'function');
assert.ok(typeof exporter.shutdown === 'function');

assert.ok(exporter['_forceFlush']);
assert.strictEqual(exporter['_flushTimeout'], 2000);
});

it('should construct an exporter with forceFlush = false', () => {
const exporter = new JaegerExporter({
serviceName: 'opentelemetry',
forceFlush: false,
});
assert.ok(typeof exporter.export === 'function');
assert.ok(typeof exporter.shutdown === 'function');

assert.ok(!exporter['_forceFlush']);
});
});

describe('export', () => {
Expand Down

0 comments on commit 288bf68

Please sign in to comment.