Skip to content

Commit

Permalink
Expose an explicit Tracer.Flush() method
Browse files Browse the repository at this point in the history
Extend the OpenTracing API with an explicit jaegertracing::Tracer::flush()
method to force spans to be flushed eagerly without closing the tracer. It
returns only when the spans are flushed.

Fixes jaegertracing#53

Call flush() from Close(), but not from the Tracer dtor. So we follow the spec
and ensure we flush buffers on explicit Close only.

Fixes jaegertracing#52

WIP. For as-yet-undiagnosed reasons `flush()` sometimes waits a full
bufferFlushInterval before returning.

Signed-off-by: Craig Ringer <[email protected]>
  • Loading branch information
ringerc committed Feb 9, 2018
1 parent c36adcf commit 521f442
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/jaegertracing/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Tracer : public opentracing::Tracer,
serviceName, sampler, reporter, logger, metrics, options));
}

~Tracer() { Close(); }
~Tracer() { close(); }

std::unique_ptr<opentracing::Span>
StartSpanWithOptions(string_view operationName,
Expand Down Expand Up @@ -181,6 +181,12 @@ class Tracer : public opentracing::Tracer,
}

void Close() noexcept override
{
flush();
close();
}

void close() noexcept
{
try {
_reporter->close();
Expand All @@ -192,7 +198,10 @@ class Tracer : public opentracing::Tracer,
}
}

void close() noexcept { Close(); }
void flush()
{
_reporter->flush();
}

const std::string& serviceName() const { return _serviceName; }

Expand Down
14 changes: 12 additions & 2 deletions src/jaegertracing/reporters/RemoteReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void RemoteReporter::sweepQueue()
sendSpan(span);
}
else if (bufferFlushIntervalExpired()) {
flush();
async_flush();
}
} catch (...) {
auto logger = logging::consoleLogger();
Expand Down Expand Up @@ -122,12 +122,13 @@ void RemoteReporter::sendSpan(const Span& span)
}
}

void RemoteReporter::flush()
void RemoteReporter::async_flush()
{
try {
const auto flushed = _sender->flush();
if (flushed > 0) {
_metrics.reporterSuccess().inc(flushed);
_cv.notify_one();
}
} catch (const Transport::Exception& ex) {
_metrics.reporterFailure().inc(ex.numFailed());
Expand All @@ -137,5 +138,14 @@ void RemoteReporter::flush()
_lastFlush = Clock::now();
}

void RemoteReporter::flush()
{
std::unique_lock<std::mutex> lock(_mutex);
_cv.wait(lock, [this]() {
async_flush();
return !_running || _queue.empty();
});
}

} // namespace reporters
} // namespace jaegertracing
4 changes: 3 additions & 1 deletion src/jaegertracing/reporters/RemoteReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ class RemoteReporter : public Reporter {

void close() override;

void flush() override;

private:
void sweepQueue();

void sendSpan(const Span& span);

void flush();
void async_flush();

bool bufferFlushIntervalExpired() const
{
Expand Down
2 changes: 2 additions & 0 deletions src/jaegertracing/reporters/Reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Reporter {
virtual void report(const Span& span) = 0;

virtual void close() = 0;

virtual void flush() {};
};

} // namespace reporters
Expand Down

0 comments on commit 521f442

Please sign in to comment.