Skip to content

Commit

Permalink
Add methods to Flush and Close a tracer (open-telemetry#45)
Browse files Browse the repository at this point in the history
* Work on Flush-Close api

* Add Flush & Close

* Add flush and close interface

* Remove return type from Flush-Close

* Fix docs
  • Loading branch information
rnburn authored Mar 26, 2020
1 parent 3b08593 commit f5bd54c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
Span{this->shared_from_this(), std::move(span)}};
}

void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override
{
tracer_handle_->tracer().ForceFlushWithMicroseconds(timeout);
}

void CloseWithMicroseconds(uint64_t timeout) noexcept override
{
tracer_handle_->tracer().CloseWithMicroseconds(timeout);
}

private:
// Note: The order is important here.
//
Expand Down
4 changes: 4 additions & 0 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this<Noop
{
return nostd::unique_ptr<Span>{new (std::nothrow) NoopSpan{this->shared_from_this()}};
}

void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}

void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
};

/**
Expand Down
28 changes: 28 additions & 0 deletions api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "opentelemetry/trace/span.h"
#include "opentelemetry/version.h"

#include <chrono>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{
Expand All @@ -23,6 +25,32 @@ class Tracer
*/
virtual nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const StartSpanOptions &options = {}) noexcept = 0;

/**
* Force any buffered spans to flush.
* @param timeout to complete the flush
*/
template <class Rep, class Period>
void ForceFlush(std::chrono::duration<Rep, Period> timeout) noexcept
{
this->ForceFlushWithMicroseconds(
static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(timeout)));
}

virtual void ForceFlushWithMicroseconds(uint64_t timeout) noexcept = 0;

/**
* ForceFlush any buffered spans and stop reporting spans.
* @param timeout to complete the flush
*/
template <class Rep, class Period>
void Close(std::chrono::duration<Rep, Period> timeout) noexcept
{
this->CloseWithMicroseconds(
static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(timeout)));
}

virtual void CloseWithMicroseconds(uint64_t timeout) noexcept = 0;
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
4 changes: 4 additions & 0 deletions examples/plugin/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class Tracer final : public opentelemetry::trace::Tracer,
opentelemetry::nostd::unique_ptr<opentelemetry::trace::Span> StartSpan(
opentelemetry::nostd::string_view name,
const opentelemetry::trace::StartSpanOptions &options) noexcept override;

void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}

void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
};
10 changes: 10 additions & 0 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ nostd::unique_ptr<trace_api::Span> Tracer::StartSpan(
return nostd::unique_ptr<trace_api::Span>{new (std::nothrow)
Span{this->shared_from_this(), name, options}};
}

void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
{
(void)timeout;
}

void Tracer::CloseWithMicroseconds(uint64_t timeout) noexcept
{
(void)timeout;
}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
4 changes: 4 additions & 0 deletions sdk/src/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th
nostd::string_view name,
const trace_api::StartSpanOptions &options = {}) noexcept override;

void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override;

void CloseWithMicroseconds(uint64_t timeout) noexcept override;

private:
std::unique_ptr<Recorder> recorder_;
};
Expand Down

0 comments on commit f5bd54c

Please sign in to comment.