Skip to content

Commit f5bd54c

Browse files
authored
Add methods to Flush and Close a tracer (open-telemetry#45)
* Work on Flush-Close api * Add Flush & Close * Add flush and close interface * Remove return type from Flush-Close * Fix docs
1 parent 3b08593 commit f5bd54c

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

api/include/opentelemetry/plugin/tracer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
6565
Span{this->shared_from_this(), std::move(span)}};
6666
}
6767

68+
void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override
69+
{
70+
tracer_handle_->tracer().ForceFlushWithMicroseconds(timeout);
71+
}
72+
73+
void CloseWithMicroseconds(uint64_t timeout) noexcept override
74+
{
75+
tracer_handle_->tracer().CloseWithMicroseconds(timeout);
76+
}
77+
6878
private:
6979
// Note: The order is important here.
7080
//

api/include/opentelemetry/trace/noop.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this<Noop
5454
{
5555
return nostd::unique_ptr<Span>{new (std::nothrow) NoopSpan{this->shared_from_this()}};
5656
}
57+
58+
void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
59+
60+
void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
5761
};
5862

5963
/**

api/include/opentelemetry/trace/tracer.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "opentelemetry/trace/span.h"
66
#include "opentelemetry/version.h"
77

8+
#include <chrono>
9+
810
OPENTELEMETRY_BEGIN_NAMESPACE
911
namespace trace
1012
{
@@ -23,6 +25,32 @@ class Tracer
2325
*/
2426
virtual nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
2527
const StartSpanOptions &options = {}) noexcept = 0;
28+
29+
/**
30+
* Force any buffered spans to flush.
31+
* @param timeout to complete the flush
32+
*/
33+
template <class Rep, class Period>
34+
void ForceFlush(std::chrono::duration<Rep, Period> timeout) noexcept
35+
{
36+
this->ForceFlushWithMicroseconds(
37+
static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(timeout)));
38+
}
39+
40+
virtual void ForceFlushWithMicroseconds(uint64_t timeout) noexcept = 0;
41+
42+
/**
43+
* ForceFlush any buffered spans and stop reporting spans.
44+
* @param timeout to complete the flush
45+
*/
46+
template <class Rep, class Period>
47+
void Close(std::chrono::duration<Rep, Period> timeout) noexcept
48+
{
49+
this->CloseWithMicroseconds(
50+
static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(timeout)));
51+
}
52+
53+
virtual void CloseWithMicroseconds(uint64_t timeout) noexcept = 0;
2654
};
2755
} // namespace trace
2856
OPENTELEMETRY_END_NAMESPACE

examples/plugin/plugin/tracer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ class Tracer final : public opentelemetry::trace::Tracer,
1414
opentelemetry::nostd::unique_ptr<opentelemetry::trace::Span> StartSpan(
1515
opentelemetry::nostd::string_view name,
1616
const opentelemetry::trace::StartSpanOptions &options) noexcept override;
17+
18+
void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
19+
20+
void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
1721
};

sdk/src/trace/tracer.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ nostd::unique_ptr<trace_api::Span> Tracer::StartSpan(
1515
return nostd::unique_ptr<trace_api::Span>{new (std::nothrow)
1616
Span{this->shared_from_this(), name, options}};
1717
}
18+
19+
void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
20+
{
21+
(void)timeout;
22+
}
23+
24+
void Tracer::CloseWithMicroseconds(uint64_t timeout) noexcept
25+
{
26+
(void)timeout;
27+
}
1828
} // namespace trace
1929
} // namespace sdk
2030
OPENTELEMETRY_END_NAMESPACE

sdk/src/trace/tracer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th
2424
nostd::string_view name,
2525
const trace_api::StartSpanOptions &options = {}) noexcept override;
2626

27+
void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override;
28+
29+
void CloseWithMicroseconds(uint64_t timeout) noexcept override;
30+
2731
private:
2832
std::unique_ptr<Recorder> recorder_;
2933
};

0 commit comments

Comments
 (0)