diff --git a/packages/opentelemetry-exporter-jaeger/src/jaeger.ts b/packages/opentelemetry-exporter-jaeger/src/jaeger.ts index f6977c07f68..f7cc50092d6 100644 --- a/packages/opentelemetry-exporter-jaeger/src/jaeger.ts +++ b/packages/opentelemetry-exporter-jaeger/src/jaeger.ts @@ -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 = { @@ -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. */ diff --git a/packages/opentelemetry-exporter-jaeger/src/types.ts b/packages/opentelemetry-exporter-jaeger/src/types.ts index 73bce51ba89..89f39bf6a93 100644 --- a/packages/opentelemetry-exporter-jaeger/src/types.ts +++ b/packages/opentelemetry-exporter-jaeger/src/types.ts @@ -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, diff --git a/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts b/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts index 811357c4ce7..38b1cb59e78 100644 --- a/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts +++ b/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts @@ -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', () => {