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