Skip to content

Commit 288bf68

Browse files
authored
feat(exporter-jaeger): add forceFlush and timeout options (open-telemetry#301)
1 parent cee42d8 commit 288bf68

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

packages/opentelemetry-exporter-jaeger/src/jaeger.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ export class JaegerExporter implements SpanExporter {
3131
private readonly _logger: types.Logger;
3232
private readonly _process: jaegerTypes.ThriftProcess;
3333
private readonly _sender: typeof jaegerTypes.UDPSender;
34+
private readonly _forceFlush: boolean = true;
35+
private readonly _flushTimeout: number;
3436

3537
constructor(config: jaegerTypes.ExporterConfig) {
3638
this._logger = config.logger || new NoopLogger();
3739
const tags: jaegerTypes.Tag[] = config.tags || [];
40+
if (config.forceFlush !== undefined) {
41+
this._forceFlush = config.forceFlush;
42+
}
43+
this._flushTimeout = config.flushTimeout || 2000;
3844

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

5662
/** Shutdown exporter. */
5763
shutdown(): void {
64+
if (!this._forceFlush) return;
65+
// Make an optimistic flush.
5866
this._sender.flush((numSpans: number, err?: string) => {
5967
if (err) {
6068
this._logger.error(`failed to flush span: ${err}`);
6169
}
6270
});
63-
// Sleeping 2 seconds before closing the sender's connection to ensure all spans are flushed.
71+
// Sleeping x seconds before closing the sender's connection to ensure
72+
// all spans are flushed.
6473
setTimeout(() => {
6574
this._sender.close();
66-
}, 2000);
75+
}, this._flushTimeout);
6776
}
6877

6978
/** Transform spans and sends to Jaeger service. */

packages/opentelemetry-exporter-jaeger/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export interface ExporterConfig {
2626
host?: string; // default: 'localhost'
2727
port?: number; // default: 6832
2828
maxPacketSize?: number; // default: 65000
29+
forceFlush?: boolean; // default: true
30+
flushTimeout?: number; // default: 2000
2931
}
3032

3133
// Below require is needed as jaeger-client types does not expose the thrift,

packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@ describe('JaegerExporter', () => {
5050
assert.strictEqual(process.tags[0].vType, 'STRING');
5151
assert.strictEqual(process.tags[0].vStr, '0.0.1');
5252
});
53+
54+
it('should construct an exporter with forceFlush and flushTimeout', () => {
55+
const exporter = new JaegerExporter({
56+
serviceName: 'opentelemetry',
57+
forceFlush: true,
58+
flushTimeout: 5000,
59+
});
60+
assert.ok(typeof exporter.export === 'function');
61+
assert.ok(typeof exporter.shutdown === 'function');
62+
63+
assert.ok(exporter['_forceFlush']);
64+
assert.strictEqual(exporter['_flushTimeout'], 5000);
65+
});
66+
67+
it('should construct an exporter without forceFlush and flushTimeout', () => {
68+
const exporter = new JaegerExporter({
69+
serviceName: 'opentelemetry',
70+
});
71+
assert.ok(typeof exporter.export === 'function');
72+
assert.ok(typeof exporter.shutdown === 'function');
73+
74+
assert.ok(exporter['_forceFlush']);
75+
assert.strictEqual(exporter['_flushTimeout'], 2000);
76+
});
77+
78+
it('should construct an exporter with forceFlush = false', () => {
79+
const exporter = new JaegerExporter({
80+
serviceName: 'opentelemetry',
81+
forceFlush: false,
82+
});
83+
assert.ok(typeof exporter.export === 'function');
84+
assert.ok(typeof exporter.shutdown === 'function');
85+
86+
assert.ok(!exporter['_forceFlush']);
87+
});
5388
});
5489

5590
describe('export', () => {

0 commit comments

Comments
 (0)