Observability: bounded tail sampling with explicit eviction (S-OBSERVABILITY-06)
This commit is contained in:
1 parent
cc41e5b1d9
commit
4b2bc8b25f
3 files changed
+399
No files matched your search
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Observability/Context.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace Void::Observability
|
||||
{
|
||||
/// Fixed horizons for retaining trace state before a decision.
|
||||
struct TailSamplingLimits {
|
||||
/// Maximum number of retained traces.
|
||||
std::size_t max_traces{1024};
|
||||
/// Maximum retained bytes. Each trace record has a fixed cost, so
|
||||
/// this also caps the number of retained traces.
|
||||
std::size_t max_bytes{1024ULL * 64ULL};
|
||||
/// A trace older than this is decided and released on expire().
|
||||
std::chrono::milliseconds horizon{std::chrono::seconds{30}};
|
||||
/// Spans at least this slow mark a trace as interesting.
|
||||
std::chrono::milliseconds slow_span{std::chrono::milliseconds{500}};
|
||||
|
||||
/// Approximate bytes retained per trace record.
|
||||
std::size_t record_bytes{64};
|
||||
|
||||
[[nodiscard]] std::size_t effective_max_traces() const noexcept
|
||||
{
|
||||
if (record_bytes == 0) {
|
||||
return max_traces;
|
||||
}
|
||||
return max_traces < max_bytes / record_bytes
|
||||
? max_traces
|
||||
: max_bytes / record_bytes;
|
||||
}
|
||||
};
|
||||
|
||||
enum class TailDecision : std::uint8_t { Keep, Drop, Evicted, Pending };
|
||||
|
||||
struct TailSampleOutcome {
|
||||
TailDecision decision{TailDecision::Pending};
|
||||
std::string reason{};
|
||||
};
|
||||
|
||||
struct TailSamplingStats {
|
||||
std::size_t retained_traces{0};
|
||||
std::size_t retained_bytes{0};
|
||||
std::uint64_t kept{0};
|
||||
std::uint64_t dropped{0};
|
||||
std::uint64_t evicted{0};
|
||||
std::uint64_t expired{0};
|
||||
};
|
||||
|
||||
/// Bounded tail sampler with explicit eviction.
|
||||
///
|
||||
/// A trace is decided when it ends or when it ages past the horizon: traces
|
||||
/// containing an error or a slow span are kept, others are dropped (or kept
|
||||
/// by the deterministic ratio). When capacity is exceeded the oldest trace
|
||||
/// is evicted without being sampled; evictions are counted, never hidden.
|
||||
class TailSampler
|
||||
{
|
||||
public:
|
||||
|
||||
explicit TailSampler(TailSamplingLimits limits = {},
|
||||
double keep_ratio = 0.0);
|
||||
|
||||
/// Register a trace. Returns false when the trace was evicted
|
||||
/// immediately (its id may be decided as Evicted).
|
||||
[[nodiscard]] TailDecision
|
||||
begin(TraceId id, Void::Time::MonotonicClock::time_point now = clock_now());
|
||||
|
||||
/// Record one span outcome for a trace. Unknown ids are ignored.
|
||||
void record_span(TraceId id,
|
||||
std::chrono::milliseconds duration,
|
||||
bool error);
|
||||
|
||||
/// Finish a trace and make its sampling decision.
|
||||
[[nodiscard]] TailSampleOutcome
|
||||
end(TraceId id);
|
||||
|
||||
/// Decide and release traces older than the horizon.
|
||||
std::size_t expire(Void::Time::MonotonicClock::time_point now);
|
||||
|
||||
[[nodiscard]] TailSamplingStats stats() const noexcept;
|
||||
[[nodiscard]] const TailSamplingLimits& limits() const noexcept
|
||||
{
|
||||
return m_limits;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Void::Time::MonotonicClock::time_point
|
||||
clock_now();
|
||||
|
||||
private:
|
||||
|
||||
struct Record {
|
||||
Void::Time::MonotonicClock::time_point start{};
|
||||
std::uint32_t spans{0};
|
||||
bool error{false};
|
||||
bool slow{false};
|
||||
};
|
||||
|
||||
[[nodiscard]] TailSampleOutcome decide(const Record& record) const;
|
||||
void evict_oldest();
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
TailSamplingLimits m_limits;
|
||||
double m_keep_ratio{0.0};
|
||||
// Keyed by the raw id bytes because TraceId has no ordering.
|
||||
std::map<std::array<std::byte, 16>, Record> m_records;
|
||||
TailSamplingStats m_stats;
|
||||
};
|
||||
} // namespace Void::Observability
|
||||
@@ -0,0 +1,165 @@
|
||||
#include <Void/Observability/TailSampling.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Observability
|
||||
{
|
||||
namespace
|
||||
{
|
||||
using Key = std::array<std::byte, 16>;
|
||||
|
||||
[[nodiscard]] Key key_of(const TraceId& id) noexcept
|
||||
{
|
||||
return id.bytes;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint64_t hash_key(const Key& key) noexcept
|
||||
{
|
||||
std::uint64_t hash = 0xcbf29ce484222325ULL;
|
||||
for (const std::byte value : key) {
|
||||
hash ^= std::to_integer<std::uint64_t>(value);
|
||||
hash *= 0x100000001b3ULL;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Time::MonotonicClock::time_point TailSampler::clock_now()
|
||||
{
|
||||
return Void::Time::SystemMonotonicClock::instance().now();
|
||||
}
|
||||
|
||||
TailSampler::TailSampler(TailSamplingLimits limits, double keep_ratio)
|
||||
: m_limits(limits)
|
||||
, m_keep_ratio(std::clamp(keep_ratio, 0.0, 1.0))
|
||||
{
|
||||
if (m_limits.record_bytes == 0) {
|
||||
m_limits.record_bytes = 1;
|
||||
}
|
||||
}
|
||||
|
||||
TailDecision TailSampler::begin(TraceId id,
|
||||
Void::Time::MonotonicClock::time_point now)
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
const Key key = key_of(id);
|
||||
if (m_records.contains(key)) {
|
||||
return TailDecision::Pending;
|
||||
}
|
||||
|
||||
const std::size_t capacity = m_limits.effective_max_traces();
|
||||
if (capacity == 0) {
|
||||
++m_stats.evicted;
|
||||
return TailDecision::Evicted;
|
||||
}
|
||||
while (m_records.size() >= capacity) {
|
||||
evict_oldest();
|
||||
}
|
||||
m_records.emplace(key, Record{.start = now});
|
||||
return TailDecision::Pending;
|
||||
}
|
||||
|
||||
void TailSampler::record_span(TraceId id,
|
||||
std::chrono::milliseconds duration,
|
||||
bool error)
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
const auto found = m_records.find(key_of(id));
|
||||
if (found == m_records.end()) {
|
||||
return;
|
||||
}
|
||||
++found->second.spans;
|
||||
found->second.error = found->second.error || error;
|
||||
found->second.slow = found->second.slow || duration >= m_limits.slow_span;
|
||||
}
|
||||
|
||||
TailSampleOutcome TailSampler::decide(const Record& record) const
|
||||
{
|
||||
if (record.error) {
|
||||
return TailSampleOutcome{TailDecision::Keep, "error"};
|
||||
}
|
||||
if (record.slow) {
|
||||
return TailSampleOutcome{TailDecision::Keep, "slow"};
|
||||
}
|
||||
return TailSampleOutcome{TailDecision::Drop, "not interesting"};
|
||||
}
|
||||
|
||||
TailSampleOutcome TailSampler::end(TraceId id)
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
const auto found = m_records.find(key_of(id));
|
||||
if (found == m_records.end()) {
|
||||
return TailSampleOutcome{TailDecision::Evicted,
|
||||
"trace was evicted or never began"};
|
||||
}
|
||||
const Key key = found->first;
|
||||
const Record record = found->second;
|
||||
m_records.erase(found);
|
||||
|
||||
TailSampleOutcome outcome = decide(record);
|
||||
if (outcome.decision == TailDecision::Drop && m_keep_ratio > 0.0) {
|
||||
const double bucket =
|
||||
static_cast<double>(hash_key(key) % 10000) / 10000.0;
|
||||
if (bucket < m_keep_ratio) {
|
||||
outcome = TailSampleOutcome{TailDecision::Keep, "ratio"};
|
||||
}
|
||||
}
|
||||
if (outcome.decision == TailDecision::Keep) {
|
||||
++m_stats.kept;
|
||||
} else {
|
||||
++m_stats.dropped;
|
||||
}
|
||||
return outcome;
|
||||
}
|
||||
|
||||
std::size_t TailSampler::expire(Void::Time::MonotonicClock::time_point now)
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
std::size_t expired = 0;
|
||||
for (auto it = m_records.begin(); it != m_records.end();) {
|
||||
const auto age = now - it->second.start;
|
||||
if (age < m_limits.horizon) {
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
const TailSampleOutcome outcome = decide(it->second);
|
||||
if (outcome.decision == TailDecision::Keep) {
|
||||
++m_stats.kept;
|
||||
} else {
|
||||
++m_stats.dropped;
|
||||
}
|
||||
++m_stats.expired;
|
||||
it = m_records.erase(it);
|
||||
++expired;
|
||||
}
|
||||
return expired;
|
||||
}
|
||||
|
||||
void TailSampler::evict_oldest()
|
||||
{
|
||||
if (m_records.empty()) {
|
||||
return;
|
||||
}
|
||||
auto oldest = m_records.begin();
|
||||
for (auto it = std::next(m_records.begin()); it != m_records.end(); ++it) {
|
||||
if (it->second.start < oldest->second.start) {
|
||||
oldest = it;
|
||||
}
|
||||
}
|
||||
m_records.erase(oldest);
|
||||
++m_stats.evicted;
|
||||
}
|
||||
|
||||
TailSamplingStats TailSampler::stats() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
TailSamplingStats out = m_stats;
|
||||
out.retained_traces = m_records.size();
|
||||
out.retained_bytes = m_records.size() * m_limits.record_bytes;
|
||||
return out;
|
||||
}
|
||||
} // namespace Void::Observability
|
||||
@@ -0,0 +1,117 @@
|
||||
#include <Void/Observability/TailSampling.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::Observability;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TraceId make_trace(std::uint8_t tag)
|
||||
{
|
||||
TraceId id;
|
||||
id.bytes[0] = std::byte{tag};
|
||||
id.bytes[15] = std::byte{0x7f};
|
||||
return id;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test observability_tail_sampling_decides_on_content()
|
||||
{
|
||||
TailSampler sampler{TailSamplingLimits{.slow_span = 100ms}};
|
||||
Void::Time::ManualClock clock;
|
||||
const auto now = clock.now();
|
||||
|
||||
// A trace with an error is kept.
|
||||
Void::Testing::expect(sampler.begin(make_trace(1), now) ==
|
||||
TailDecision::Pending);
|
||||
sampler.record_span(make_trace(1), 1ms, true);
|
||||
Void::Testing::expect(sampler.end(make_trace(1)).decision ==
|
||||
TailDecision::Keep);
|
||||
|
||||
// A trace with a slow span is kept.
|
||||
Void::Testing::require(sampler.begin(make_trace(2), now) ==
|
||||
TailDecision::Pending);
|
||||
sampler.record_span(make_trace(2), 150ms, false);
|
||||
Void::Testing::expect(sampler.end(make_trace(2)).decision ==
|
||||
TailDecision::Keep);
|
||||
|
||||
// An ordinary trace is dropped.
|
||||
Void::Testing::require(sampler.begin(make_trace(3), now) ==
|
||||
TailDecision::Pending);
|
||||
sampler.record_span(make_trace(3), 1ms, false);
|
||||
Void::Testing::expect(sampler.end(make_trace(3)).decision ==
|
||||
TailDecision::Drop);
|
||||
|
||||
const auto stats = sampler.stats();
|
||||
Void::Testing::expect_eq(stats.kept, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(stats.dropped, std::uint64_t{1});
|
||||
Void::Testing::expect_eq(stats.retained_traces, std::size_t{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test observability_tail_sampling_is_bounded_with_eviction()
|
||||
{
|
||||
// Two traces fit (128 bytes / 64-byte record), the third evicts the oldest.
|
||||
TailSampler sampler{
|
||||
TailSamplingLimits{.max_traces = 16,
|
||||
.max_bytes = 128,
|
||||
.horizon = 10s,
|
||||
.record_bytes = 64}};
|
||||
Void::Time::ManualClock clock;
|
||||
const auto start = clock.now();
|
||||
|
||||
Void::Testing::require(sampler.begin(make_trace(1), start) ==
|
||||
TailDecision::Pending);
|
||||
Void::Testing::require(clock.advance(1ms).has_value());
|
||||
Void::Testing::require(sampler.begin(make_trace(2), clock.now()) ==
|
||||
TailDecision::Pending);
|
||||
Void::Testing::require(clock.advance(1ms).has_value());
|
||||
Void::Testing::require(sampler.begin(make_trace(3), clock.now()) ==
|
||||
TailDecision::Pending);
|
||||
|
||||
const auto stats = sampler.stats();
|
||||
Void::Testing::expect_eq(stats.retained_traces, std::size_t{2});
|
||||
Void::Testing::expect(stats.retained_bytes <= std::size_t{128});
|
||||
Void::Testing::expect_eq(stats.evicted, std::uint64_t{1});
|
||||
|
||||
// The evicted trace is reported as evicted, not silently sampled.
|
||||
Void::Testing::expect(sampler.end(make_trace(1)).decision ==
|
||||
TailDecision::Evicted);
|
||||
}
|
||||
|
||||
Void::Testing::Test observability_tail_sampling_expires_and_keeps_ratio()
|
||||
{
|
||||
TailSampler sampler{TailSamplingLimits{.horizon = 100ms}};
|
||||
Void::Time::ManualClock clock;
|
||||
const auto start = clock.now();
|
||||
|
||||
Void::Testing::require(sampler.begin(make_trace(1), start) ==
|
||||
TailDecision::Pending);
|
||||
sampler.record_span(make_trace(1), 1ms, false);
|
||||
|
||||
// Not yet expired.
|
||||
Void::Testing::require(clock.advance(50ms).has_value());
|
||||
Void::Testing::expect_eq(sampler.expire(clock.now()), std::size_t{0});
|
||||
|
||||
// Past the horizon it is decided and released.
|
||||
Void::Testing::require(clock.advance(100ms).has_value());
|
||||
Void::Testing::expect_eq(sampler.expire(clock.now()), std::size_t{1});
|
||||
Void::Testing::expect_eq(sampler.stats().retained_traces, std::size_t{0});
|
||||
Void::Testing::expect_eq(sampler.stats().expired, std::uint64_t{1});
|
||||
|
||||
// A full keep ratio keeps ordinary traces deterministically.
|
||||
TailSampler keep_all{TailSamplingLimits{}, 1.0};
|
||||
Void::Testing::require(keep_all.begin(make_trace(9), start) ==
|
||||
TailDecision::Pending);
|
||||
Void::Testing::expect(keep_all.end(make_trace(9)).decision ==
|
||||
TailDecision::Keep);
|
||||
TailSampler keep_none{TailSamplingLimits{}, 0.0};
|
||||
Void::Testing::require(keep_none.begin(make_trace(9), start) ==
|
||||
TailDecision::Pending);
|
||||
Void::Testing::expect(keep_none.end(make_trace(9)).decision ==
|
||||
TailDecision::Drop);
|
||||
}
|
||||
Reference in new issue
Block a user