Skip to content

Commit

Permalink
feat(#362): adds flush method to http reporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Jul 20, 2019
1 parent 4a876f3 commit c8fdc59
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/zipkin/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ declare namespace zipkin {
class BatchRecorder implements Recorder {
constructor(args: { logger: Logger, timeout?: number });
record: (rec: Record) => void;
flush: () => void;
}

class ConsoleRecorder implements Recorder {
Expand Down
25 changes: 23 additions & 2 deletions packages/zipkin/src/batch-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const {Span, Endpoint} = require('./model');
*/
const defaultTimeout = 60 * 1000000;

/**
* default batch interval 1 second (in miliseconds)
* @type {number}
*/
const defaultBatchInterval = 1000;

/**
* defaultTags property name
* @type {symbol}
Expand Down Expand Up @@ -63,13 +69,19 @@ class BatchRecorder {
* @param {Object} options
* @property {Logger} logger logs the data to zipkin server
* @property {number} timeout timeout for span in microseconds
* @property {number} batchInterval interval for reporting in miliseconds.
* The value -1 means that there won't be batch reporting on interval basis.
*/
constructor({logger, timeout = defaultTimeout}) {
constructor({logger, timeout = defaultTimeout, batchInterval = defaultBatchInterval}) {
this.logger = logger;
this.timeout = timeout;
this.partialSpans = new Map();
this[defaultTagsSymbol] = {};

if (batchInterval === -1) {
return;
}

// read through the partials spans regularly
// and collect any timed-out ones
const timer = setInterval(() => {
Expand All @@ -82,7 +94,7 @@ class BatchRecorder {
this._writeSpan(id, span);
}
});
}, 1000);
}, batchInterval);
if (timer.unref) { // unref might not be available in browsers
timer.unref(); // Allows Node to terminate instead of blocking on timer
}
Expand Down Expand Up @@ -135,6 +147,15 @@ class BatchRecorder {
}
}

flush() {
this.partialSpans.forEach((span, id) => {
if (span.delegate.duration === undefined) {
span.delegate.addAnnotation(now(), 'zipkin-js.flush');
}
this._writeSpan(id, span);
});
}

record(rec) {
const id = rec.traceId;

Expand Down
36 changes: 36 additions & 0 deletions packages/zipkin/test/batch-recorder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,42 @@ describe('Batch Recorder', () => {
clock.uninstall();
});

it('should only flush spans when calling flush method', () => {
const clock = lolex.install();

const logSpan = sinon.spy();
const ctxImpl = new ExplicitContext();
const logger = {logSpan};
const recorder = new BatchRecorder({logger, batchInterval: -1});
const trace = new Tracer({ctxImpl, recorder});
const traceId = new TraceId({
traceId: 'a',
parentId: new Some('a'),
spanId: 'c',
sampled: new Some(true)
});

ctxImpl.scoped(() => {
trace.setId(traceId);

trace.recordServiceName('SmoothieStore');
trace.recordAnnotation(new Annotation.ServerRecv());
});

clock.tick('01:00'); // 1 minute is the default timeout

expect(logSpan.calledOnce).to.equal(false);

recorder.flush();

expect(logSpan.calledOnce).to.equal(true);

const loggedSpan = logSpan.getCall(0).args[0];
expect(loggedSpan.annotations[0].value).to.equal('zipkin-js.flush');

clock.uninstall();
});

it('should capture ServerAddr event', () => {
const logSpan = sinon.spy();

Expand Down

0 comments on commit c8fdc59

Please sign in to comment.