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
|
||||
Reference in new issue
Block a user