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 @@
|
||||
#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