diff --git a/api/include/opentelemetry/plugin/tracer.h b/api/include/opentelemetry/plugin/tracer.h index 5c327f8ee2a..0e44d6c71a7 100644 --- a/api/include/opentelemetry/plugin/tracer.h +++ b/api/include/opentelemetry/plugin/tracer.h @@ -65,6 +65,16 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_thisshared_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. // diff --git a/api/include/opentelemetry/trace/noop.h b/api/include/opentelemetry/trace/noop.h index b8374bbea9f..63335867c22 100644 --- a/api/include/opentelemetry/trace/noop.h +++ b/api/include/opentelemetry/trace/noop.h @@ -54,6 +54,10 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this{new (std::nothrow) NoopSpan{this->shared_from_this()}}; } + + void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {} + + void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {} }; /** diff --git a/api/include/opentelemetry/trace/tracer.h b/api/include/opentelemetry/trace/tracer.h index 603d24e2bbc..29aa288123e 100644 --- a/api/include/opentelemetry/trace/tracer.h +++ b/api/include/opentelemetry/trace/tracer.h @@ -5,6 +5,8 @@ #include "opentelemetry/trace/span.h" #include "opentelemetry/version.h" +#include + OPENTELEMETRY_BEGIN_NAMESPACE namespace trace { @@ -23,6 +25,32 @@ class Tracer */ virtual nostd::unique_ptr StartSpan(nostd::string_view name, const StartSpanOptions &options = {}) noexcept = 0; + + /** + * Force any buffered spans to flush. + * @param timeout to complete the flush + */ + template + void ForceFlush(std::chrono::duration timeout) noexcept + { + this->ForceFlushWithMicroseconds( + static_cast(std::chrono::duration_cast(timeout))); + } + + virtual void ForceFlushWithMicroseconds(uint64_t timeout) noexcept = 0; + + /** + * ForceFlush any buffered spans and stop reporting spans. + * @param timeout to complete the flush + */ + template + void Close(std::chrono::duration timeout) noexcept + { + this->CloseWithMicroseconds( + static_cast(std::chrono::duration_cast(timeout))); + } + + virtual void CloseWithMicroseconds(uint64_t timeout) noexcept = 0; }; } // namespace trace OPENTELEMETRY_END_NAMESPACE diff --git a/examples/plugin/plugin/tracer.h b/examples/plugin/plugin/tracer.h index 42a23084a7d..4d653f32e9b 100644 --- a/examples/plugin/plugin/tracer.h +++ b/examples/plugin/plugin/tracer.h @@ -14,4 +14,8 @@ class Tracer final : public opentelemetry::trace::Tracer, opentelemetry::nostd::unique_ptr 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 {} }; diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 4a4c484bc92..73052e93f42 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -15,6 +15,16 @@ nostd::unique_ptr Tracer::StartSpan( return nostd::unique_ptr{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 diff --git a/sdk/src/trace/tracer.h b/sdk/src/trace/tracer.h index c242b3c3a75..65d1318e5b4 100644 --- a/sdk/src/trace/tracer.h +++ b/sdk/src/trace/tracer.h @@ -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_; };