From 38c49d5ca02229585f10ed0c73ec829465c50038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Fri, 13 Nov 2026 15:19:19 +0000 Subject: [PATCH 1/7] Add a DDSketch-compatible distribution for telemetry Raw distribution payloads grow with traffic. Like libdatadog, telemetry will send DDSketches instead. This adds the sketch; it is not wired in. --- BUILD.bazel | 4 + CMakeLists.txt | 2 + src/datadog/telemetry/log_bin_index.cpp | 245 +++++++++++ src/datadog/telemetry/log_bin_index.h | 39 ++ src/datadog/telemetry/log_binned_distr.cpp | 218 ++++++++++ src/datadog/telemetry/log_binned_distr.h | 81 ++++ test/CMakeLists.txt | 1 + test/telemetry/test_log_binned_distr.cpp | 466 +++++++++++++++++++++ 8 files changed, 1056 insertions(+) create mode 100644 src/datadog/telemetry/log_bin_index.cpp create mode 100644 src/datadog/telemetry/log_bin_index.h create mode 100644 src/datadog/telemetry/log_binned_distr.cpp create mode 100644 src/datadog/telemetry/log_binned_distr.h create mode 100644 test/telemetry/test_log_binned_distr.cpp diff --git a/BUILD.bazel b/BUILD.bazel index 0a854130..459624d9 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -75,6 +75,10 @@ cc_library( "src/datadog/tags.h", "src/datadog/telemetry/configuration.cpp", "src/datadog/telemetry/log.h", + "src/datadog/telemetry/log_bin_index.cpp", + "src/datadog/telemetry/log_bin_index.h", + "src/datadog/telemetry/log_binned_distr.cpp", + "src/datadog/telemetry/log_binned_distr.h", "src/datadog/telemetry/metric_context.h", "src/datadog/telemetry/telemetry.cpp", "src/datadog/telemetry/telemetry_impl.cpp", diff --git a/CMakeLists.txt b/CMakeLists.txt index dddf6158..42f8e26e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -183,6 +183,8 @@ target_sources(dd-trace-cpp-objects PRIVATE src/datadog/common/hash.cpp src/datadog/telemetry/configuration.cpp + src/datadog/telemetry/log_bin_index.cpp + src/datadog/telemetry/log_binned_distr.cpp src/datadog/telemetry/telemetry.cpp src/datadog/telemetry/telemetry_impl.cpp src/datadog/baggage.cpp diff --git a/src/datadog/telemetry/log_bin_index.cpp b/src/datadog/telemetry/log_bin_index.cpp new file mode 100644 index 00000000..3062dcc6 --- /dev/null +++ b/src/datadog/telemetry/log_bin_index.cpp @@ -0,0 +1,245 @@ +#include "log_bin_index.h" + +#include +#include +#include +#include + +namespace datadog::telemetry::log_bins { +namespace { + +// Finds bins without computing a logarithm. +// +// A positive double is 2^e × (1 + f). The fraction f is in [0, 1) and is +// stored in 52 bits. The top 6 bits of f give an integer m from 0 to 63. +// This means f is in [m/64, (m + 1)/64). The exponent e and m form a key. +// For example, between 1 and 2, the key with m = 0 holds [1, 1.015625). +// The key with m = 1 holds [1.015625, 1.03125). +// +// A key's doubles go from 2^e × (1 + m/64) up to, but excluding, +// 2^e × (1 + (m + 1)/64). Its largest and smallest doubles satisfy +// largest/smallest < (65 + m)/(64 + m) <= 65/64. +// The fraction (65 + m)/(64 + m) equals 65/64 only when m = 0. +// So every key has largest/smallest strictly below 65/64. +// The bin ratio is 65/64. Neighbouring bin edges are 65/64 apart. +// So at most one bin edge falls inside a key. Its doubles are in one bin +// or in two neighbouring bins. +// +// This holds for the exact logarithm. The bins come from a rounded `log`, +// so two edges may be a few doubles less than 65/64 apart. When m = 0, +// a key spans almost exactly 65/64. If an edge fell just after its first +// double, the next edge could also fall inside the key. Its doubles would +// then be in three bins. +// +// fill_entry() checks that each key holds doubles in at most two neighbouring +// bins. If the check fails, every value takes the slow path. +// +// For each key, the table stores the bin of its first double and where the +// next bin starts. It stores that start as the remaining 46 mantissa bits. +// Finding a bin is then one load and one compare. The table is filled with +// reference_bin_index(), so the table and reference_bin_index() always give +// the same bin. +// +// The table covers [2^-30, 2^65). Other values take the slow path. +class BinTable { + public: + static const BinTable& instance(); + + // Sets `bin` and returns true if the table covers the double `bits`. + bool find_bin(std::uint64_t bits, BinIndex& bin) const; + + // False if the table could not be built, and every value takes the slow + // path. + bool usable() const { return usable_keys_ == key_count; } + + private: + // A key is the exponent and the top 6 mantissa bits of a positive double. + static constexpr int key_shift = 52 - 6; + static constexpr std::uint64_t low_bits_mask = + (std::uint64_t{1} << key_shift) - 1; + static constexpr std::uint64_t first_key = std::uint64_t{1023 - 30} << 6; + static constexpr std::uint64_t key_count = std::uint64_t{30 + 65} << 6; + + static constexpr int edge_bits = key_shift + 1; + static constexpr int base_offset_bits = 64 - edge_bits; + // An edge value past every low bits, for keys whose doubles are all in one + // bin. + static constexpr std::uint64_t no_edge = low_bits_mask + 1; + + struct Entry { + // Low bits of the first double of the next bin, or `no_edge` if the next + // bin starts after the key. + std::uint64_t edge : edge_bits; + // Bin of the key's first double, minus `first_bin_`. + std::uint64_t base_offset : base_offset_bits; + }; + static_assert(sizeof(Entry) == sizeof(std::uint64_t)); + + BinTable(); + bool fill_entry(std::uint64_t key); + + BinIndex first_bin_; + // `key_count`, or 0 if the table could not be built. + std::uint64_t usable_keys_; + std::array entries_{}; +}; + +// Sets round-to-nearest while it lives, then restores the caller's mode. +class RoundToNearest { + public: + RoundToNearest() : saved_mode_(std::fegetround()) { + std::fesetround(FE_TONEAREST); + } + ~RoundToNearest() { std::fesetround(saved_mode_); } + RoundToNearest(const RoundToNearest&) = delete; + RoundToNearest& operator=(const RoundToNearest&) = delete; + + private: + int saved_mode_; +}; + +double log_multiplier(); +std::uint64_t find_edge(BinIndex bin, std::uint64_t below, + std::uint64_t at_or_above); +void narrow_around_guess(BinIndex bin, std::uint64_t& below, + std::uint64_t& at_or_above); +std::uint64_t to_bits(double value); +double from_bits(std::uint64_t bits); + +} // namespace + +BinIndex bin_index(double value) { + BinIndex bin; + if (BinTable::instance().find_bin(to_bits(value), bin)) return bin; + return reference_bin_index(value); +} + +bool fast_table_built() { return BinTable::instance().usable(); } + +BinIndex reference_bin_index(double value) { + // Checks the bits, because -ffast-math can remove std::isnan() and friends. + const std::uint64_t bits = to_bits(value); + const bool not_finite = ((bits >> 52) & 0x7ff) == 0x7ff; + const bool below_zero = (bits >> 63) != 0 && (bits << 1) != 0; + if (not_finite || below_zero) return rejected_value; + if (value < zero_threshold) return zero_bin; + + // One rounding for the multiplication and the addition. Separate roundings + // would depend on whether the compiler fuses them. + const double scaled_log = + std::fma(std::log(value), log_multiplier(), bin_index_shift); + return static_cast(std::floor(scaled_log)); +} + +namespace { + +// The table is built at run time, with the local `log`. C libraries do not +// all round `log` the same way, so a table built elsewhere could differ. +const BinTable& BinTable::instance() { + static const BinTable table; + return table; +} + +bool BinTable::find_bin(std::uint64_t bits, BinIndex& bin) const { + // Zero, negative values, NaN and infinities give keys out of range too. + const std::uint64_t key = (bits >> key_shift) - first_key; + if (key >= usable_keys_) return false; + + const Entry entry = entries_[key]; + const bool past_edge = (bits & low_bits_mask) >= entry.edge; + bin = first_bin_ + static_cast(entry.base_offset) + + static_cast(past_edge); + return true; +} + +// The first caller may have changed the rounding mode. The table must not +// depend on it. +BinTable::BinTable() : first_bin_(0), usable_keys_(key_count) { + const RoundToNearest rounding; + first_bin_ = reference_bin_index(from_bits(first_key << key_shift)); + for (std::uint64_t key = 0; key < key_count; ++key) { + if (!fill_entry(key)) { + usable_keys_ = 0; + return; + } + } +} + +bool BinTable::fill_entry(std::uint64_t key) { + const std::uint64_t first_bits = (first_key + key) << key_shift; + const std::uint64_t last_bits = first_bits | low_bits_mask; + const BinIndex base_bin = reference_bin_index(from_bits(first_bits)); + const BinIndex last_bin = reference_bin_index(from_bits(last_bits)); + const std::int64_t base_offset = std::int64_t{base_bin} - first_bin_; + if (base_offset < 0 || base_offset >> base_offset_bits != 0 || + last_bin < base_bin || last_bin > base_bin + 1) { + return false; + } + + Entry& entry = entries_[key]; + entry.base_offset = static_cast(base_offset); + entry.edge = no_edge; + if (last_bin > base_bin) { + entry.edge = find_edge(last_bin, first_bits, last_bits) - first_bits; + } + return true; +} + +// Same formula as libdatadog: 1 / ln(bin_ratio). +double log_multiplier() { + static const double multiplier = 1.0 / std::log(bin_ratio); + return multiplier; +} + +// Returns the bits of the smallest double whose bin is `bin`. Expects the bin +// of `below` to be lower than `bin`, and the bin of `at_or_above` not to be. +std::uint64_t find_edge(BinIndex bin, std::uint64_t below, + std::uint64_t at_or_above) { + narrow_around_guess(bin, below, at_or_above); + while (at_or_above - below > 1) { + const std::uint64_t middle = below + (at_or_above - below) / 2; + if (reference_bin_index(from_bits(middle)) >= bin) { + at_or_above = middle; + } else { + below = middle; + } + } + return at_or_above; +} + +// Moves the bounds close to the edge computed with `exp`, so that the search +// needs few steps. The bounds keep their meaning. +void narrow_around_guess(BinIndex bin, std::uint64_t& below, + std::uint64_t& at_or_above) { + const double guess_value = + std::exp((bin - bin_index_shift) / log_multiplier()); + const std::uint64_t guess = to_bits(guess_value); + if (guess <= below || guess >= at_or_above) return; + + for (std::uint64_t width = 64; width < at_or_above - below; width *= 64) { + const std::uint64_t low = guess > width ? guess - width : 0; + const std::uint64_t high = guess + width; + if (low > below && reference_bin_index(from_bits(low)) < bin) { + below = low; + } + if (high < at_or_above && reference_bin_index(from_bits(high)) >= bin) { + at_or_above = high; + } + if (below == low && at_or_above == high) return; + } +} + +std::uint64_t to_bits(double value) { + std::uint64_t bits; + std::memcpy(&bits, &value, sizeof bits); + return bits; +} + +double from_bits(std::uint64_t bits) { + double value; + std::memcpy(&value, &bits, sizeof value); + return value; +} + +} // namespace +} // namespace datadog::telemetry::log_bins diff --git a/src/datadog/telemetry/log_bin_index.h b/src/datadog/telemetry/log_bin_index.h new file mode 100644 index 00000000..cc8b0b74 --- /dev/null +++ b/src/datadog/telemetry/log_bin_index.h @@ -0,0 +1,39 @@ +#pragma once + +// Bin index of the log-binned distributions. See log_binned_distr.h for the +// mapping. + +#include +#include + +namespace datadog::telemetry::log_bins { + +// The number of a bin. Same type as LogBinnedDistr's private BinIndex. +using BinIndex = std::int32_t; + +// Each bin's upper edge is this many times its lower edge. +inline constexpr double bin_ratio = 65.0 / 64.0; +// Shift so that 1e-9 falls in bin 1. +inline constexpr double bin_index_shift = 1338.5; +// Values below this, zero included, are counted apart. +inline constexpr double zero_threshold = + std::numeric_limits::min() * bin_ratio; + +// Special results of bin_index(). +inline constexpr BinIndex zero_bin = std::numeric_limits::min(); +inline constexpr BinIndex rejected_value = zero_bin + 1; + +// Returns the bin of `value`. Returns `zero_bin` for values below +// `zero_threshold`, and `rejected_value` for negative values, NaN and +// infinities. Same result as reference_bin_index(), but faster. +BinIndex bin_index(double value); + +// Computes the bin as floor(ln(value) × m + bin_index_shift), with +// m = 1 / ln(bin_ratio). The multiplication and the addition are rounded once. +// Declared here for the tests. The library only uses bin_index(). +BinIndex reference_bin_index(double value); + +// Returns true if the fast table was built. Declared here for the tests. +bool fast_table_built(); + +} // namespace datadog::telemetry::log_bins diff --git a/src/datadog/telemetry/log_binned_distr.cpp b/src/datadog/telemetry/log_binned_distr.cpp new file mode 100644 index 00000000..454c49ab --- /dev/null +++ b/src/datadog/telemetry/log_binned_distr.cpp @@ -0,0 +1,218 @@ +#include "log_binned_distr.h" + +#include +#include +#include + +#include "log_bin_index.h" + +namespace datadog::telemetry { +namespace { + +using BinIndex = log_bins::BinIndex; + +constexpr std::int32_t max_bins = 2048; +constexpr BinIndex no_clamp = std::numeric_limits::min(); +constexpr std::uint32_t min_window_size = 64; +constexpr std::uint32_t max_window_size = 2 * max_bins; + +std::string encode_mapping(); +std::string encode_bin(BinIndex bin, std::uint64_t count); +void append_message(std::string& out, std::uint32_t field, + const std::string& message); +void append_double(std::string& out, std::uint32_t field, double value); +void append_varint(std::string& out, std::uint64_t value); +std::uint32_t zigzag(std::int32_t value); + +} // namespace + +bool LogBinnedDistr::record(double value) { + static_assert(std::is_same_v); + const BinIndex bin = log_bins::bin_index(value); + if (bin == log_bins::rejected_value) return false; + + std::lock_guard lock(mutex_); + if (bin == log_bins::zero_bin) { + ++zero_count_; + } else { + add_to_bin(bin); + } + return true; +} + +// DDSketch message: mapping = 1, positiveValues = 2, negativeValues = 3, +// zeroCount = 4. +std::string LogBinnedDistr::encode_protobuf() const { + std::lock_guard lock(mutex_); + std::string sketch; + append_message(sketch, 1, encode_mapping()); + append_message(sketch, 2, encode_bins()); + append_message(sketch, 3, ""); + if (zero_count_ != 0) { + append_double(sketch, 4, static_cast(zero_count_)); + } + return sketch; +} + +void LogBinnedDistr::add_to_bin(BinIndex bin) { + bin = std::max(bin, clamp_floor_); + const std::uint32_t position = + static_cast(bin) - static_cast(lowest_bin_); + if (position < bin_span_) { + ++count_at(bin); + return; + } + extend_range(bin); + const bool full = bin_span_ == static_cast(max_bins); + clamp_floor_ = full ? lowest_bin_ : no_clamp; +} + +// Same rules as libdatadog's LowCollapsingDenseStore. +void LogBinnedDistr::extend_range(BinIndex bin) { + if (bin_span_ == 0) { + start_range(bin); + return; + } + const BinIndex highest = + lowest_bin_ + static_cast(bin_span_) - 1; + if (bin < lowest_bin_) { + extend_down(bin, highest); + } else { + extend_up(bin, highest); + } +} + +void LogBinnedDistr::start_range(BinIndex bin) { + cover(bin, bin); + lowest_bin_ = bin; + bin_span_ = 1; + ++count_at(bin); +} + +// The range grows down while it has room. Values below it go to its lowest +// bin. +void LogBinnedDistr::extend_down(BinIndex bin, BinIndex highest) { + const BinIndex new_lowest = std::max(bin, highest - (max_bins - 1)); + cover(new_lowest, highest); + lowest_bin_ = new_lowest; + bin_span_ = static_cast(highest - new_lowest + 1); + ++count_at(new_lowest); +} + +// The range grows up. If it gets too wide, its lowest bins are merged into +// the new lowest bin. +void LogBinnedDistr::extend_up(BinIndex bin, BinIndex highest) { + const BinIndex new_lowest = std::max(lowest_bin_, bin - (max_bins - 1)); + const std::uint64_t merged = + take_counts(lowest_bin_, std::min(new_lowest - 1, highest)); + cover(new_lowest, bin); + lowest_bin_ = new_lowest; + bin_span_ = static_cast(bin - new_lowest + 1); + count_at(new_lowest) += merged; + ++count_at(bin); +} + +// Sets the counts of bins [lowest, highest] to zero and returns their sum. +std::uint64_t LogBinnedDistr::take_counts(BinIndex lowest, BinIndex highest) { + std::uint64_t sum = 0; + for (BinIndex bin = lowest; bin <= highest; ++bin) { + std::uint64_t& count = count_at(bin); + sum += count; + count = 0; + } + return sum; +} + +// Makes the window cover bins [lowest, highest]. The new window has room on +// both sides, so that the range can grow without moving it each time. +void LogBinnedDistr::cover(BinIndex lowest, BinIndex highest) { + const BinIndex window_end = + window_start_ + static_cast(counts_.size()); + if (!counts_.empty() && lowest >= window_start_ && highest < window_end) { + return; + } + + const std::uint32_t needed = static_cast(highest - lowest + 1); + const std::uint32_t size = + std::clamp(2 * needed, min_window_size, max_window_size); + const BinIndex start = + lowest - static_cast((size - needed) / 2); + std::vector counts(size); + const BinIndex copy_start = std::max(window_start_, start); + const BinIndex copy_end = + std::min(window_end, start + static_cast(size)); + for (BinIndex bin = copy_start; bin < copy_end; ++bin) { + counts[static_cast(bin - start)] = count_at(bin); + } + counts_.swap(counts); + window_start_ = start; +} + +std::uint64_t& LogBinnedDistr::count_at(BinIndex bin) { + return counts_[static_cast(bin - window_start_)]; +} + +// Store message: binCounts = 1 (map), with only the bins that +// are not empty. +std::string LogBinnedDistr::encode_bins() const { + std::string store; + for (std::uint32_t position = 0; position < bin_span_; ++position) { + const BinIndex bin = lowest_bin_ + static_cast(position); + const std::uint64_t count = + counts_[static_cast(bin - window_start_)]; + if (count != 0) append_message(store, 1, encode_bin(bin, count)); + } + return store; +} + +namespace { + +// IndexMapping message: gamma = 1, indexOffset = 2. The interpolation is +// NONE, the default, so it is left out. +std::string encode_mapping() { + std::string mapping; + append_double(mapping, 1, log_bins::bin_ratio); + append_double(mapping, 2, log_bins::bin_index_shift); + return mapping; +} + +// Map entry message: key = 1 (sint32), value = 2 (double). +std::string encode_bin(BinIndex bin, std::uint64_t count) { + std::string entry; + append_varint(entry, (1 << 3) | 0); + append_varint(entry, zigzag(bin)); + append_double(entry, 2, static_cast(count)); + return entry; +} + +void append_message(std::string& out, std::uint32_t field, + const std::string& message) { + append_varint(out, (field << 3) | 2); + append_varint(out, message.size()); + out += message; +} + +void append_double(std::string& out, std::uint32_t field, double value) { + append_varint(out, (field << 3) | 1); + std::uint64_t bits; + std::memcpy(&bits, &value, sizeof bits); + for (int byte = 0; byte < 8; ++byte) { + out.push_back(static_cast((bits >> (8 * byte)) & 0xff)); + } +} + +void append_varint(std::string& out, std::uint64_t value) { + while (value >= 0x80) { + out.push_back(static_cast((value & 0x7f) | 0x80)); + value >>= 7; + } + out.push_back(static_cast(value)); +} + +std::uint32_t zigzag(std::int32_t value) { + const std::uint32_t shifted = static_cast(value) << 1; + return value < 0 ? ~shifted : shifted; +} + +} // namespace +} // namespace datadog::telemetry diff --git a/src/datadog/telemetry/log_binned_distr.h b/src/datadog/telemetry/log_binned_distr.h new file mode 100644 index 00000000..144fd9c0 --- /dev/null +++ b/src/datadog/telemetry/log_binned_distr.h @@ -0,0 +1,81 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace datadog::telemetry { + +// A distribution whose bins form a geometric series, compatible with +// DDSketch. Bin i covers [low(i), γ × low(i)), so each bin's upper edge is γ +// times its lower edge. +// +// Every value in bin i is reported as the same bin value x̂(i). It is the +// harmonic mean of the bin's edges: +// +// x̂ = 2 × low × γ low / (low + γ low) = low × (1 + α) +// +// This puts x̂ at the same relative error from both edges. So for any value x +// in the bin, |x̂ - x| / x <= α. +// +// Design parameters: +// * The maximum relative error, α = 1/129. That is about 2.1 significant +// digits. The bin ratio follows from it: γ = (1 + α) / (1 - α) = 65/64. +// * Where the bins start. Bin 1 is the bin that holds 1e-9, the smallest value +// the backend's sketches use. So the bin number is +// +// i(x) = floor(log_γ(x) + s), with s = 1 - floor(log_γ(1e-9)) + 0.5 +// +// which gives s = 1338.5. Values between 0 and 1e-9 get bins below 1. +// +// Other properties: +// * Values below DBL_MIN × γ, zero included, are counted apart, in the zero +// count. Negative values, NaN and infinities are rejected. +// * At most 2048 bins are kept: i_max - i_min <= 2047. When a new value would +// go past that, the lowest bins are merged into the lowest bin kept. Values +// below the range go to that lowest bin too. The highest bins are never +// merged. +// * `log` rounds, so about one value in 10^12, right next to a bin edge, goes +// to the neighbouring bin. The worst relative error is then +// α + 4 × 10^-13. This also moves a few bin edges by a double or so between +// C libraries, and from libdatadog's. +// * All member functions are thread-safe. Each distribution has its own lock. +class LogBinnedDistr { + public: + // Returns false if the value is rejected. + bool record(double value); + + // Returns the distribution as a serialized DDSketch protobuf message. The + // bins are in the sparse form (`binCounts`). + std::string encode_protobuf() const; + + private: + // The number of a bin, i(x) above. Same type as log_bins::BinIndex. + using BinIndex = std::int32_t; + + void add_to_bin(BinIndex bin); + void extend_range(BinIndex bin); + void start_range(BinIndex bin); + void extend_down(BinIndex bin, BinIndex highest); + void extend_up(BinIndex bin, BinIndex highest); + std::uint64_t take_counts(BinIndex lowest, BinIndex highest); + void cover(BinIndex lowest, BinIndex highest); + std::uint64_t& count_at(BinIndex bin); + std::string encode_bins() const; + + mutable std::mutex mutex_; + // counts_[k] is the count of bin window_start_ + k. Bins outside the range + // [lowest_bin_, lowest_bin_ + bin_span_) are always zero. + std::vector counts_; + BinIndex window_start_ = 0; + BinIndex lowest_bin_ = 0; + std::uint32_t bin_span_ = 0; + // The lowest bin, once the range holds the maximum number of bins. Lower + // values are moved up to it without a branch. + BinIndex clamp_floor_ = std::numeric_limits::min(); + std::uint64_t zero_count_ = 0; +}; + +} // namespace datadog::telemetry diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 86a47e77..148e9e0b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(tests # telemetry test cases telemetry/test_configuration.cpp + telemetry/test_log_binned_distr.cpp telemetry/test_telemetry.cpp # test cases diff --git a/test/telemetry/test_log_binned_distr.cpp b/test/telemetry/test_log_binned_distr.cpp new file mode 100644 index 00000000..a659b4ce --- /dev/null +++ b/test/telemetry/test_log_binned_distr.cpp @@ -0,0 +1,466 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "datadog/telemetry/log_bin_index.h" +#include "datadog/telemetry/log_binned_distr.h" +#include "test.h" + +using namespace datadog::telemetry; + +#define LOG_BINNED_TEST(x) TEST_CASE(x, "[telemetry][log_binned_distr]") + +namespace { + +using BinIndex = log_bins::BinIndex; +using Bins = std::map; + +struct DecodedSketch { + double gamma = 0; + double index_offset = 0; + std::uint64_t interpolation = 0; + Bins bins; + bool has_negative_store = false; + Bins negative_bins; + double zero_count = 0; +}; + +DecodedSketch decode_sketch(const std::string& data); +std::uint64_t to_bits(double value); +double from_bits(std::uint64_t bits); + +// The bins expected once `values` are recorded. Each value counts in its own +// bin, but only the 2048 highest bins are kept. Lower values count in the +// lowest bin kept. +Bins expected_bins(const std::vector& values) { + Bins exact; + for (const double value : values) { + const BinIndex bin = log_bins::bin_index(value); + if (bin != log_bins::zero_bin) exact[bin] += 1; + } + if (exact.empty()) return exact; + + const BinIndex lowest_kept = exact.rbegin()->first - 2047; + Bins expected; + for (const auto& [bin, count] : exact) { + expected[std::max(bin, lowest_kept)] += count; + } + return expected; +} + +// A value in the middle of `bin`, half a bin from both edges. Bin 1338 holds +// 1.0. +double value_in_bin(BinIndex bin) { + return std::pow(log_bins::bin_ratio, bin - 1338); +} + +Bins recorded_bins(const std::vector& bins) { + LogBinnedDistr distribution; + for (const BinIndex bin : bins) distribution.record(value_in_bin(bin)); + return decode_sketch(distribution.encode_protobuf()).bins; +} + +// Returns the bits of the smallest double whose reference bin is at least +// `bin`, searching in (below, at_or_above]. +std::uint64_t reference_edge(BinIndex bin, std::uint64_t below, + std::uint64_t at_or_above) { + while (at_or_above - below > 1) { + const std::uint64_t middle = below + (at_or_above - below) / 2; + if (log_bins::reference_bin_index(from_bits(middle)) >= bin) { + at_or_above = middle; + } else { + below = middle; + } + } + return at_or_above; +} + +void check_same_bin(double value) { + INFO("value " << value << ", bits " << to_bits(value)); + CHECK(log_bins::bin_index(value) == log_bins::reference_bin_index(value)); +} + +void check_bins(const std::vector& values) { + LogBinnedDistr distribution; + for (const double value : values) distribution.record(value); + const double zero_count = + static_cast(std::count(values.begin(), values.end(), 0.0)); + + const DecodedSketch decoded = decode_sketch(distribution.encode_protobuf()); + CHECK(decoded.bins == expected_bins(values)); + CHECK(decoded.zero_count == zero_count); +} + +std::vector log_uniform(std::size_t count, double lowest, + double highest, std::uint64_t seed) { + std::mt19937_64 generator(seed); + std::uniform_real_distribution exponent(std::log(lowest), + std::log(highest)); + std::vector values(count); + for (double& value : values) value = std::exp(exponent(generator)); + return values; +} + +} // namespace + +LOG_BINNED_TEST("mapping constants follow libdatadog's formulas") { + const double relative_accuracy = 0.007751937984496124; + CHECK((1 + relative_accuracy) / (1 - relative_accuracy) == + log_bins::bin_ratio); + + const double log_ratio = std::log(log_bins::bin_ratio); + CHECK((1 - std::floor(std::log(1e-9) / log_ratio)) + 0.5 == + log_bins::bin_index_shift); + + const double multiplier = 1 / log_ratio; + const double int32_limit = + std::exp((INT32_MIN - log_bins::bin_index_shift) / multiplier + 1.0); + CHECK(std::max(DBL_MIN * log_bins::bin_ratio, int32_limit) == + log_bins::zero_threshold); +} + +LOG_BINNED_TEST("bin index of special values") { + const double infinity = std::numeric_limits::infinity(); + const std::vector> cases{ + {std::nan(""), log_bins::rejected_value}, + {infinity, log_bins::rejected_value}, + {-infinity, log_bins::rejected_value}, + {-1.0, log_bins::rejected_value}, + {-DBL_MIN, log_bins::rejected_value}, + {0.0, log_bins::zero_bin}, + {-0.0, log_bins::zero_bin}, + {std::numeric_limits::denorm_min(), log_bins::zero_bin}, + {DBL_MIN, log_bins::zero_bin}, + {std::nextafter(log_bins::zero_threshold, 0.0), log_bins::zero_bin}, + {log_bins::zero_threshold, -44352}, + {1e-9, 1}, + {1.0, 1338}, + {1000.0, 1784}, + {DBL_MAX, 47118}, + }; + for (const auto& [value, expected_bin] : cases) { + INFO("value " << value); + CHECK(log_bins::bin_index(value) == expected_bin); + CHECK(log_bins::reference_bin_index(value) == expected_bin); + } +} + +LOG_BINNED_TEST("the fast table is built") { + CHECK(log_bins::fast_table_built()); +} + +LOG_BINNED_TEST("bin index of values far from an edge is exact") { + // Exact bins, computed with 300-bit arithmetic (mpmath): the floor of + // ln(x) / ln(65/64) + 1338.5. Each value is at least 0.04 bins from an edge. + const std::vector> cases{ + {1e-300, -43216}, {5e-300, -43112}, {1e-20, -1632}, {3.3e-15, -813}, + {1e-9, 1}, {0.001, 892}, {0.37, 1274}, {0.5, 1293}, + {1.0, 1338}, {2.5, 1397}, {3.0, 1409}, {123.456, 1649}, + {1000.0, 1784}, {65536.0, 2053}, {1e9, 2675}, {1e12, 3120}, + {1e18, 4011}, {1e25, 5051}, {1e100, 16189}, {1e300, 45892}, + }; + for (const auto& [value, expected_bin] : cases) { + INFO("value " << value); + CHECK(log_bins::bin_index(value) == expected_bin); + } +} + +LOG_BINNED_TEST("bin index rounds the multiplication and the addition once") { + // ln(x) / ln(65/64) + 1338.5 is 3253.99999999999995516 for this x, so the + // exact bin is 3253. Rounding the multiplication first would round the + // result up to 3254. + const double value = 7'903'045'517'381.0; + CHECK(log_bins::reference_bin_index(value) == 3253); + CHECK(log_bins::bin_index(value) == 3253); +} + +LOG_BINNED_TEST("bin index equals the reference around every bin edge") { + // Covers the fast table, [2^-30, 2^65), and some bins on each side. + const std::uint64_t lowest_bits = to_bits(std::ldexp(1.0, -32)); + const std::uint64_t highest_bits = to_bits(std::ldexp(1.0, 67)); + const BinIndex first_bin = + log_bins::reference_bin_index(from_bits(lowest_bits)); + const BinIndex last_bin = + log_bins::reference_bin_index(from_bits(highest_bits)); + + std::uint64_t previous_edge = lowest_bits; + for (BinIndex bin = first_bin + 1; bin <= last_bin; ++bin) { + const std::uint64_t edge = reference_edge(bin, previous_edge, highest_bits); + for (std::uint64_t step = 0; step <= 6; ++step) { + check_same_bin(from_bits(edge - 3 + step)); + } + previous_edge = edge; + } +} + +LOG_BINNED_TEST("bin index equals the reference on many values") { + SECTION("log-uniform values") { + for (const double value : log_uniform(1'000'000, 1e-15, 1e25, 1)) { + check_same_bin(value); + } + } + SECTION("random bit patterns") { + std::mt19937_64 generator(2); + for (int count = 0; count < 1'000'000; ++count) { + check_same_bin(from_bits(generator())); + } + } + SECTION("integers and decimal fractions") { + for (int number = 0; number < 200'000; ++number) { + check_same_bin(number); + check_same_bin(number / 1000.0); + } + } +} + +LOG_BINNED_TEST("record rejects invalid values") { + LogBinnedDistr distribution; + CHECK_FALSE(distribution.record(std::nan(""))); + CHECK_FALSE(distribution.record(-1.0)); + CHECK_FALSE(distribution.record(std::numeric_limits::infinity())); + CHECK(distribution.record(0.0)); + CHECK(distribution.record(-0.0)); + + const DecodedSketch decoded = decode_sketch(distribution.encode_protobuf()); + CHECK(decoded.bins.empty()); + CHECK(decoded.zero_count == 2); +} + +LOG_BINNED_TEST("encoding of an empty distribution") { + const LogBinnedDistr distribution; + const unsigned char expected[] = { + 0x0a, 0x12, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x3f, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x94, 0x40, 0x12, 0x00, 0x1a, 0x00}; + CHECK(distribution.encode_protobuf() == + std::string(reinterpret_cast(expected), sizeof expected)); +} + +LOG_BINNED_TEST("encoding of one value") { + LogBinnedDistr distribution; + distribution.record(1.0); + const unsigned char expected[] = { + 0x0a, 0x12, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, + 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x94, 0x40, + 0x12, 0x0e, 0x0a, 0x0c, 0x08, 0xf4, 0x14, 0x11, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x1a, 0x00}; + CHECK(distribution.encode_protobuf() == + std::string(reinterpret_cast(expected), sizeof expected)); + + const DecodedSketch decoded = decode_sketch(distribution.encode_protobuf()); + CHECK(decoded.gamma == 65.0 / 64.0); + CHECK(decoded.index_offset == 1338.5); + CHECK(decoded.interpolation == 0); + CHECK(decoded.bins == Bins{{1338, 1.0}}); + CHECK(decoded.has_negative_store); + CHECK(decoded.negative_bins.empty()); +} + +LOG_BINNED_TEST("the store keeps the 2048 highest bins") { + SECTION("a range of 2048 bins keeps every bin") { + CHECK(recorded_bins({1000, 3047}) == Bins{{1000, 1}, {3047, 1}}); + } + SECTION("a higher value merges the lowest bins into the lowest bin kept") { + CHECK(recorded_bins({1000, 1000, 1500, 3048}) == + Bins{{1001, 2}, {1500, 1}, {3048, 1}}); + CHECK(recorded_bins({1000, 1500, 9000}) == Bins{{6953, 2}, {9000, 1}}); + } + SECTION("a lower value goes to the lowest bin kept") { + CHECK(recorded_bins({3047, 1000, 999, 10}) == Bins{{1000, 3}, {3047, 1}}); + CHECK(recorded_bins({3047, 500}) == Bins{{1000, 1}, {3047, 1}}); + CHECK(recorded_bins({1000, 3048, 5}) == Bins{{1001, 2}, {3048, 1}}); + } +} + +LOG_BINNED_TEST("bins of many values") { + SECTION("narrow durations") { + check_bins(log_uniform(100'000, 100, 10'000, 3)); + } + SECTION("wide range, which collapses the lowest bins") { + check_bins(log_uniform(100'000, 1e-12, 1e22, 4)); + } + SECTION("values that only increase") { + std::vector values = log_uniform(20'000, 1e-9, 1e20, 5); + std::sort(values.begin(), values.end()); + check_bins(values); + } + SECTION("values that only decrease") { + std::vector values = log_uniform(20'000, 1e-9, 1e20, 6); + std::sort(values.begin(), values.end(), std::greater()); + check_bins(values); + } + SECTION("values alternating between both ends") { + std::vector values; + for (int step = 0; step < 5'000; ++step) { + values.push_back(1e-6 * (1 + step % 7)); + values.push_back(1e15 * (1 + step % 5)); + } + check_bins(values); + } + SECTION("low values, then high ones, then low ones again") { + std::vector values = log_uniform(5'000, 1, 100, 7); + const std::vector high = log_uniform(5'000, 1e14, 1e16, 8); + const std::vector low = log_uniform(5'000, 1e-3, 10, 9); + values.insert(values.end(), high.begin(), high.end()); + values.insert(values.end(), low.begin(), low.end()); + check_bins(values); + } + SECTION("zeros and fractions mixed") { + std::vector values = log_uniform(50'000, 1e-4, 1, 10); + for (std::size_t index = 0; index < values.size(); index += 10) { + values[index] = 0; + } + check_bins(values); + } +} + +LOG_BINNED_TEST("records from several threads are all counted") { + LogBinnedDistr distribution; + static constexpr int thread_count = 4; + static constexpr int values_per_thread = 50'000; + std::vector threads; + for (int thread_number = 0; thread_number < thread_count; ++thread_number) { + threads.emplace_back([&distribution, thread_number] { + for (int index = 0; index < values_per_thread; ++index) { + distribution.record(1.0 + thread_number + index % 100); + } + }); + } + for (std::thread& thread : threads) thread.join(); + + double total = 0; + for (const auto& [bin, count] : + decode_sketch(distribution.encode_protobuf()).bins) { + total += count; + } + CHECK(total == thread_count * values_per_thread); +} + +namespace { + +// A minimal protobuf reader, for the messages of a DDSketch. +class ProtobufReader { + public: + explicit ProtobufReader(const std::string& data) : data_(data) {} + + bool done() const { return position_ >= data_.size(); } + + std::uint64_t read_varint() { + std::uint64_t value = 0; + for (int shift = 0;; shift += 7) { + const auto byte = static_cast(data_.at(position_++)); + value |= std::uint64_t{byte & 0x7fu} << shift; + if ((byte & 0x80) == 0) return value; + } + } + + double read_double() { + std::uint64_t bits = 0; + for (int byte = 0; byte < 8; ++byte) { + const auto value = static_cast(data_.at(position_++)); + bits |= std::uint64_t{value} << (8 * byte); + } + return from_bits(bits); + } + + std::string read_bytes() { + const std::size_t length = read_varint(); + REQUIRE(position_ + length <= data_.size()); + std::string bytes = data_.substr(position_, length); + position_ += length; + return bytes; + } + + private: + const std::string& data_; + std::size_t position_ = 0; +}; + +std::int32_t unzigzag(std::uint64_t value) { + const auto low = static_cast(value); + return static_cast((low >> 1) ^ (0u - (low & 1))); +} + +void decode_bin(const std::string& data, Bins& bins) { + ProtobufReader reader(data); + BinIndex bin = 0; + double count = 0; + while (!reader.done()) { + const std::uint64_t tag = reader.read_varint(); + if (tag == ((1 << 3) | 0)) { + bin = unzigzag(reader.read_varint()); + } else if (tag == ((2 << 3) | 1)) { + count = reader.read_double(); + } else { + FAIL("unexpected field in a bin entry: " << tag); + } + } + bins[bin] += count; +} + +Bins decode_store(const std::string& data) { + ProtobufReader reader(data); + Bins bins; + while (!reader.done()) { + const std::uint64_t tag = reader.read_varint(); + REQUIRE(tag == ((1 << 3) | 2)); + decode_bin(reader.read_bytes(), bins); + } + return bins; +} + +void decode_mapping(const std::string& data, DecodedSketch& sketch) { + ProtobufReader reader(data); + while (!reader.done()) { + const std::uint64_t tag = reader.read_varint(); + if (tag == ((1 << 3) | 1)) { + sketch.gamma = reader.read_double(); + } else if (tag == ((2 << 3) | 1)) { + sketch.index_offset = reader.read_double(); + } else if (tag == ((3 << 3) | 0)) { + sketch.interpolation = reader.read_varint(); + } else { + FAIL("unexpected field in the mapping: " << tag); + } + } +} + +DecodedSketch decode_sketch(const std::string& data) { + ProtobufReader reader(data); + DecodedSketch sketch; + while (!reader.done()) { + const std::uint64_t tag = reader.read_varint(); + if (tag == ((1 << 3) | 2)) { + decode_mapping(reader.read_bytes(), sketch); + } else if (tag == ((2 << 3) | 2)) { + sketch.bins = decode_store(reader.read_bytes()); + } else if (tag == ((3 << 3) | 2)) { + sketch.has_negative_store = true; + sketch.negative_bins = decode_store(reader.read_bytes()); + } else if (tag == ((4 << 3) | 1)) { + sketch.zero_count = reader.read_double(); + } else { + FAIL("unexpected field in the sketch: " << tag); + } + } + return sketch; +} + +std::uint64_t to_bits(double value) { + std::uint64_t bits; + std::memcpy(&bits, &value, sizeof bits); + return bits; +} + +double from_bits(std::uint64_t bits) { + double value; + std::memcpy(&value, &bits, sizeof value); + return value; +} + +} // namespace From 0ee78e002c63ee48061cc58392ec78aec5d02c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Wed, 18 Nov 2026 18:28:43 +0000 Subject: [PATCH 2/7] Simplify the telemetry sketch after review Review showed three parts that cost something and gave nothing back. The sketch's own mutex was redundant: its only user, a distribution handle's store, already locks around every call. The clamp for values below a full range only helps when one series spans more than a factor of 6e13, which no metric does, and it slowed the usual case on x86. The empty negative store was copied from libdatadog, but neither the DDSketch protobuf definition nor the intake needs it. The other changes are for readability: full names, a single copy of the bit-cast helpers, named protobuf wire types and a const count_at(). The tests now cover a negative bin, and the exactness checks on random values run ten times fewer values, since a separate test already checks every bin edge. --- src/datadog/telemetry/log_bin_index.cpp | 15 --- src/datadog/telemetry/log_bin_index.h | 20 +++- src/datadog/telemetry/log_binned_distr.cpp | 82 +++++++++------- src/datadog/telemetry/log_binned_distr.h | 19 ++-- test/telemetry/test_log_binned_distr.cpp | 107 +++++++++------------ 5 files changed, 114 insertions(+), 129 deletions(-) diff --git a/src/datadog/telemetry/log_bin_index.cpp b/src/datadog/telemetry/log_bin_index.cpp index 3062dcc6..dd2b90e3 100644 --- a/src/datadog/telemetry/log_bin_index.cpp +++ b/src/datadog/telemetry/log_bin_index.cpp @@ -3,7 +3,6 @@ #include #include #include -#include namespace datadog::telemetry::log_bins { namespace { @@ -103,8 +102,6 @@ std::uint64_t find_edge(BinIndex bin, std::uint64_t below, std::uint64_t at_or_above); void narrow_around_guess(BinIndex bin, std::uint64_t& below, std::uint64_t& at_or_above); -std::uint64_t to_bits(double value); -double from_bits(std::uint64_t bits); } // namespace @@ -229,17 +226,5 @@ void narrow_around_guess(BinIndex bin, std::uint64_t& below, } } -std::uint64_t to_bits(double value) { - std::uint64_t bits; - std::memcpy(&bits, &value, sizeof bits); - return bits; -} - -double from_bits(std::uint64_t bits) { - double value; - std::memcpy(&value, &bits, sizeof value); - return value; -} - } // namespace } // namespace datadog::telemetry::log_bins diff --git a/src/datadog/telemetry/log_bin_index.h b/src/datadog/telemetry/log_bin_index.h index cc8b0b74..2e4d1119 100644 --- a/src/datadog/telemetry/log_bin_index.h +++ b/src/datadog/telemetry/log_bin_index.h @@ -4,11 +4,12 @@ // mapping. #include +#include #include namespace datadog::telemetry::log_bins { -// The number of a bin. Same type as LogBinnedDistr's private BinIndex. +// The number of a bin. Same type as LogBinnedDistribution's private BinIndex. using BinIndex = std::int32_t; // Each bin's upper edge is this many times its lower edge. @@ -36,4 +37,21 @@ BinIndex reference_bin_index(double value); // Returns true if the fast table was built. Declared here for the tests. bool fast_table_built(); +// TODO: replace to_bits() and from_bits() with std::bit_cast after the upgrade +// to C++20. + +// The bits of a double, as an integer. +inline std::uint64_t to_bits(double value) { + std::uint64_t bits; + std::memcpy(&bits, &value, sizeof bits); + return bits; +} + +// The double with these bits. +inline double from_bits(std::uint64_t bits) { + double value; + std::memcpy(&value, &bits, sizeof value); + return value; +} + } // namespace datadog::telemetry::log_bins diff --git a/src/datadog/telemetry/log_binned_distr.cpp b/src/datadog/telemetry/log_binned_distr.cpp index 454c49ab..9cd6d021 100644 --- a/src/datadog/telemetry/log_binned_distr.cpp +++ b/src/datadog/telemetry/log_binned_distr.cpp @@ -1,7 +1,6 @@ #include "log_binned_distr.h" #include -#include #include #include "log_bin_index.h" @@ -12,26 +11,32 @@ namespace { using BinIndex = log_bins::BinIndex; constexpr std::int32_t max_bins = 2048; -constexpr BinIndex no_clamp = std::numeric_limits::min(); constexpr std::uint32_t min_window_size = 64; constexpr std::uint32_t max_window_size = 2 * max_bins; +// How a protobuf field's value is encoded. +enum class WireType : std::uint32_t { + varint = 0, + fixed64 = 1, + length_delimited = 2 +}; + std::string encode_mapping(); std::string encode_bin(BinIndex bin, std::uint64_t count); void append_message(std::string& out, std::uint32_t field, const std::string& message); void append_double(std::string& out, std::uint32_t field, double value); +void append_tag(std::string& out, std::uint32_t field, WireType wire_type); void append_varint(std::string& out, std::uint64_t value); std::uint32_t zigzag(std::int32_t value); } // namespace -bool LogBinnedDistr::record(double value) { +bool LogBinnedDistribution::record(double value) { static_assert(std::is_same_v); const BinIndex bin = log_bins::bin_index(value); if (bin == log_bins::rejected_value) return false; - std::lock_guard lock(mutex_); if (bin == log_bins::zero_bin) { ++zero_count_; } else { @@ -41,40 +46,35 @@ bool LogBinnedDistr::record(double value) { } // DDSketch message: mapping = 1, positiveValues = 2, negativeValues = 3, -// zeroCount = 4. -std::string LogBinnedDistr::encode_protobuf() const { - std::lock_guard lock(mutex_); +// zeroCount = 4. Values are never negative, so negativeValues is left out. +std::string LogBinnedDistribution::encode_protobuf() const { std::string sketch; append_message(sketch, 1, encode_mapping()); append_message(sketch, 2, encode_bins()); - append_message(sketch, 3, ""); if (zero_count_ != 0) { append_double(sketch, 4, static_cast(zero_count_)); } return sketch; } -void LogBinnedDistr::add_to_bin(BinIndex bin) { - bin = std::max(bin, clamp_floor_); +void LogBinnedDistribution::add_to_bin(BinIndex bin) { const std::uint32_t position = static_cast(bin) - static_cast(lowest_bin_); - if (position < bin_span_) { + if (position < bin_range_size_) { ++count_at(bin); return; } extend_range(bin); - const bool full = bin_span_ == static_cast(max_bins); - clamp_floor_ = full ? lowest_bin_ : no_clamp; } // Same rules as libdatadog's LowCollapsingDenseStore. -void LogBinnedDistr::extend_range(BinIndex bin) { - if (bin_span_ == 0) { +void LogBinnedDistribution::extend_range(BinIndex bin) { + if (bin_range_size_ == 0) { start_range(bin); return; } const BinIndex highest = - lowest_bin_ + static_cast(bin_span_) - 1; + lowest_bin_ + static_cast(bin_range_size_) - 1; if (bin < lowest_bin_) { extend_down(bin, highest); } else { @@ -82,38 +82,39 @@ void LogBinnedDistr::extend_range(BinIndex bin) { } } -void LogBinnedDistr::start_range(BinIndex bin) { - cover(bin, bin); +void LogBinnedDistribution::start_range(BinIndex bin) { + ensure_window_covers(bin, bin); lowest_bin_ = bin; - bin_span_ = 1; + bin_range_size_ = 1; ++count_at(bin); } // The range grows down while it has room. Values below it go to its lowest // bin. -void LogBinnedDistr::extend_down(BinIndex bin, BinIndex highest) { +void LogBinnedDistribution::extend_down(BinIndex bin, BinIndex highest) { const BinIndex new_lowest = std::max(bin, highest - (max_bins - 1)); - cover(new_lowest, highest); + ensure_window_covers(new_lowest, highest); lowest_bin_ = new_lowest; - bin_span_ = static_cast(highest - new_lowest + 1); + bin_range_size_ = static_cast(highest - new_lowest + 1); ++count_at(new_lowest); } // The range grows up. If it gets too wide, its lowest bins are merged into // the new lowest bin. -void LogBinnedDistr::extend_up(BinIndex bin, BinIndex highest) { +void LogBinnedDistribution::extend_up(BinIndex bin, BinIndex highest) { const BinIndex new_lowest = std::max(lowest_bin_, bin - (max_bins - 1)); const std::uint64_t merged = take_counts(lowest_bin_, std::min(new_lowest - 1, highest)); - cover(new_lowest, bin); + ensure_window_covers(new_lowest, bin); lowest_bin_ = new_lowest; - bin_span_ = static_cast(bin - new_lowest + 1); + bin_range_size_ = static_cast(bin - new_lowest + 1); count_at(new_lowest) += merged; ++count_at(bin); } // Sets the counts of bins [lowest, highest] to zero and returns their sum. -std::uint64_t LogBinnedDistr::take_counts(BinIndex lowest, BinIndex highest) { +std::uint64_t LogBinnedDistribution::take_counts(BinIndex lowest, + BinIndex highest) { std::uint64_t sum = 0; for (BinIndex bin = lowest; bin <= highest; ++bin) { std::uint64_t& count = count_at(bin); @@ -125,7 +126,8 @@ std::uint64_t LogBinnedDistr::take_counts(BinIndex lowest, BinIndex highest) { // Makes the window cover bins [lowest, highest]. The new window has room on // both sides, so that the range can grow without moving it each time. -void LogBinnedDistr::cover(BinIndex lowest, BinIndex highest) { +void LogBinnedDistribution::ensure_window_covers(BinIndex lowest, + BinIndex highest) { const BinIndex window_end = window_start_ + static_cast(counts_.size()); if (!counts_.empty() && lowest >= window_start_ && highest < window_end) { @@ -148,18 +150,21 @@ void LogBinnedDistr::cover(BinIndex lowest, BinIndex highest) { window_start_ = start; } -std::uint64_t& LogBinnedDistr::count_at(BinIndex bin) { +std::uint64_t& LogBinnedDistribution::count_at(BinIndex bin) { + return counts_[static_cast(bin - window_start_)]; +} + +std::uint64_t LogBinnedDistribution::count_at(BinIndex bin) const { return counts_[static_cast(bin - window_start_)]; } // Store message: binCounts = 1 (map), with only the bins that // are not empty. -std::string LogBinnedDistr::encode_bins() const { +std::string LogBinnedDistribution::encode_bins() const { std::string store; - for (std::uint32_t position = 0; position < bin_span_; ++position) { + for (std::uint32_t position = 0; position < bin_range_size_; ++position) { const BinIndex bin = lowest_bin_ + static_cast(position); - const std::uint64_t count = - counts_[static_cast(bin - window_start_)]; + const std::uint64_t count = count_at(bin); if (count != 0) append_message(store, 1, encode_bin(bin, count)); } return store; @@ -179,7 +184,7 @@ std::string encode_mapping() { // Map entry message: key = 1 (sint32), value = 2 (double). std::string encode_bin(BinIndex bin, std::uint64_t count) { std::string entry; - append_varint(entry, (1 << 3) | 0); + append_tag(entry, 1, WireType::varint); append_varint(entry, zigzag(bin)); append_double(entry, 2, static_cast(count)); return entry; @@ -187,20 +192,23 @@ std::string encode_bin(BinIndex bin, std::uint64_t count) { void append_message(std::string& out, std::uint32_t field, const std::string& message) { - append_varint(out, (field << 3) | 2); + append_tag(out, field, WireType::length_delimited); append_varint(out, message.size()); out += message; } void append_double(std::string& out, std::uint32_t field, double value) { - append_varint(out, (field << 3) | 1); - std::uint64_t bits; - std::memcpy(&bits, &value, sizeof bits); + append_tag(out, field, WireType::fixed64); + const std::uint64_t bits = log_bins::to_bits(value); for (int byte = 0; byte < 8; ++byte) { out.push_back(static_cast((bits >> (8 * byte)) & 0xff)); } } +void append_tag(std::string& out, std::uint32_t field, WireType wire_type) { + append_varint(out, (field << 3) | static_cast(wire_type)); +} + void append_varint(std::string& out, std::uint64_t value) { while (value >= 0x80) { out.push_back(static_cast((value & 0x7f) | 0x80)); diff --git a/src/datadog/telemetry/log_binned_distr.h b/src/datadog/telemetry/log_binned_distr.h index 144fd9c0..d02f15b5 100644 --- a/src/datadog/telemetry/log_binned_distr.h +++ b/src/datadog/telemetry/log_binned_distr.h @@ -1,8 +1,6 @@ #pragma once #include -#include -#include #include #include @@ -15,7 +13,7 @@ namespace datadog::telemetry { // Every value in bin i is reported as the same bin value x̂(i). It is the // harmonic mean of the bin's edges: // -// x̂ = 2 × low × γ low / (low + γ low) = low × (1 + α) +// x̂ = 2 × low × γ × low / (low + γ × low) = low × (1 + α) // // This puts x̂ at the same relative error from both edges. So for any value x // in the bin, |x̂ - x| / x <= α. @@ -41,8 +39,8 @@ namespace datadog::telemetry { // to the neighbouring bin. The worst relative error is then // α + 4 × 10^-13. This also moves a few bin edges by a double or so between // C libraries, and from libdatadog's. -// * All member functions are thread-safe. Each distribution has its own lock. -class LogBinnedDistr { +// * Not thread-safe: callers must lock. +class LogBinnedDistribution { public: // Returns false if the value is rejected. bool record(double value); @@ -61,20 +59,17 @@ class LogBinnedDistr { void extend_down(BinIndex bin, BinIndex highest); void extend_up(BinIndex bin, BinIndex highest); std::uint64_t take_counts(BinIndex lowest, BinIndex highest); - void cover(BinIndex lowest, BinIndex highest); + void ensure_window_covers(BinIndex lowest, BinIndex highest); std::uint64_t& count_at(BinIndex bin); + std::uint64_t count_at(BinIndex bin) const; std::string encode_bins() const; - mutable std::mutex mutex_; // counts_[k] is the count of bin window_start_ + k. Bins outside the range - // [lowest_bin_, lowest_bin_ + bin_span_) are always zero. + // [lowest_bin_, lowest_bin_ + bin_range_size_) are always zero. std::vector counts_; BinIndex window_start_ = 0; BinIndex lowest_bin_ = 0; - std::uint32_t bin_span_ = 0; - // The lowest bin, once the range holds the maximum number of bins. Lower - // values are moved up to it without a branch. - BinIndex clamp_floor_ = std::numeric_limits::min(); + std::uint32_t bin_range_size_ = 0; std::uint64_t zero_count_ = 0; }; diff --git a/test/telemetry/test_log_binned_distr.cpp b/test/telemetry/test_log_binned_distr.cpp index a659b4ce..04924296 100644 --- a/test/telemetry/test_log_binned_distr.cpp +++ b/test/telemetry/test_log_binned_distr.cpp @@ -1,11 +1,9 @@ #include -#include #include -#include #include +#include #include #include -#include #include #include "datadog/telemetry/log_bin_index.h" @@ -20,6 +18,8 @@ namespace { using BinIndex = log_bins::BinIndex; using Bins = std::map; +using log_bins::from_bits; +using log_bins::to_bits; struct DecodedSketch { double gamma = 0; @@ -32,8 +32,6 @@ struct DecodedSketch { }; DecodedSketch decode_sketch(const std::string& data); -std::uint64_t to_bits(double value); -double from_bits(std::uint64_t bits); // The bins expected once `values` are recorded. Each value counts in its own // bin, but only the 2048 highest bins are kept. Lower values count in the @@ -61,7 +59,7 @@ double value_in_bin(BinIndex bin) { } Bins recorded_bins(const std::vector& bins) { - LogBinnedDistr distribution; + LogBinnedDistribution distribution; for (const BinIndex bin : bins) distribution.record(value_in_bin(bin)); return decode_sketch(distribution.encode_protobuf()).bins; } @@ -87,7 +85,7 @@ void check_same_bin(double value) { } void check_bins(const std::vector& values) { - LogBinnedDistr distribution; + LogBinnedDistribution distribution; for (const double value : values) distribution.record(value); const double zero_count = static_cast(std::count(values.begin(), values.end(), 0.0)); @@ -119,10 +117,11 @@ LOG_BINNED_TEST("mapping constants follow libdatadog's formulas") { log_bins::bin_index_shift); const double multiplier = 1 / log_ratio; + const double lowest_bin = std::numeric_limits::min(); const double int32_limit = - std::exp((INT32_MIN - log_bins::bin_index_shift) / multiplier + 1.0); - CHECK(std::max(DBL_MIN * log_bins::bin_ratio, int32_limit) == - log_bins::zero_threshold); + std::exp((lowest_bin - log_bins::bin_index_shift) / multiplier + 1.0); + CHECK(std::max(std::numeric_limits::min() * log_bins::bin_ratio, + int32_limit) == log_bins::zero_threshold); } LOG_BINNED_TEST("bin index of special values") { @@ -132,17 +131,17 @@ LOG_BINNED_TEST("bin index of special values") { {infinity, log_bins::rejected_value}, {-infinity, log_bins::rejected_value}, {-1.0, log_bins::rejected_value}, - {-DBL_MIN, log_bins::rejected_value}, + {-std::numeric_limits::min(), log_bins::rejected_value}, {0.0, log_bins::zero_bin}, {-0.0, log_bins::zero_bin}, {std::numeric_limits::denorm_min(), log_bins::zero_bin}, - {DBL_MIN, log_bins::zero_bin}, + {std::numeric_limits::min(), log_bins::zero_bin}, {std::nextafter(log_bins::zero_threshold, 0.0), log_bins::zero_bin}, {log_bins::zero_threshold, -44352}, {1e-9, 1}, {1.0, 1338}, {1000.0, 1784}, - {DBL_MAX, 47118}, + {std::numeric_limits::max(), 47118}, }; for (const auto& [value, expected_bin] : cases) { INFO("value " << value); @@ -201,18 +200,18 @@ LOG_BINNED_TEST("bin index equals the reference around every bin edge") { LOG_BINNED_TEST("bin index equals the reference on many values") { SECTION("log-uniform values") { - for (const double value : log_uniform(1'000'000, 1e-15, 1e25, 1)) { + for (const double value : log_uniform(100'000, 1e-15, 1e25, 1)) { check_same_bin(value); } } SECTION("random bit patterns") { std::mt19937_64 generator(2); - for (int count = 0; count < 1'000'000; ++count) { + for (int count = 0; count < 100'000; ++count) { check_same_bin(from_bits(generator())); } } SECTION("integers and decimal fractions") { - for (int number = 0; number < 200'000; ++number) { + for (int number = 0; number < 20'000; ++number) { check_same_bin(number); check_same_bin(number / 1000.0); } @@ -220,7 +219,7 @@ LOG_BINNED_TEST("bin index equals the reference on many values") { } LOG_BINNED_TEST("record rejects invalid values") { - LogBinnedDistr distribution; + LogBinnedDistribution distribution; CHECK_FALSE(distribution.record(std::nan(""))); CHECK_FALSE(distribution.record(-1.0)); CHECK_FALSE(distribution.record(std::numeric_limits::infinity())); @@ -233,22 +232,21 @@ LOG_BINNED_TEST("record rejects invalid values") { } LOG_BINNED_TEST("encoding of an empty distribution") { - const LogBinnedDistr distribution; + const LogBinnedDistribution distribution; const unsigned char expected[] = { - 0x0a, 0x12, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x3f, 0x11, - 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x94, 0x40, 0x12, 0x00, 0x1a, 0x00}; + 0x0a, 0x12, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x3f, + 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x94, 0x40, 0x12, 0x00}; CHECK(distribution.encode_protobuf() == std::string(reinterpret_cast(expected), sizeof expected)); } LOG_BINNED_TEST("encoding of one value") { - LogBinnedDistr distribution; + LogBinnedDistribution distribution; distribution.record(1.0); const unsigned char expected[] = { - 0x0a, 0x12, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, - 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x94, 0x40, - 0x12, 0x0e, 0x0a, 0x0c, 0x08, 0xf4, 0x14, 0x11, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x1a, 0x00}; + 0x0a, 0x12, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x3f, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x94, 0x40, 0x12, 0x0e, 0x0a, 0x0c, + 0x08, 0xf4, 0x14, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f}; CHECK(distribution.encode_protobuf() == std::string(reinterpret_cast(expected), sizeof expected)); @@ -257,8 +255,21 @@ LOG_BINNED_TEST("encoding of one value") { CHECK(decoded.index_offset == 1338.5); CHECK(decoded.interpolation == 0); CHECK(decoded.bins == Bins{{1338, 1.0}}); - CHECK(decoded.has_negative_store); - CHECK(decoded.negative_bins.empty()); + CHECK_FALSE(decoded.has_negative_store); +} + +LOG_BINNED_TEST("encoding of one value in a negative bin") { + // Bin -147 holds values around 1e-10. Its key is zigzag(-147) = 293. + LogBinnedDistribution distribution; + distribution.record(value_in_bin(-147)); + const unsigned char expected[] = { + 0x0a, 0x12, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x3f, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x94, 0x40, 0x12, 0x0e, 0x0a, 0x0c, + 0x08, 0xa5, 0x02, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f}; + CHECK(distribution.encode_protobuf() == + std::string(reinterpret_cast(expected), sizeof expected)); + CHECK(decode_sketch(distribution.encode_protobuf()).bins == + Bins{{-147, 1.0}}); } LOG_BINNED_TEST("the store keeps the 2048 highest bins") { @@ -319,28 +330,6 @@ LOG_BINNED_TEST("bins of many values") { } } -LOG_BINNED_TEST("records from several threads are all counted") { - LogBinnedDistr distribution; - static constexpr int thread_count = 4; - static constexpr int values_per_thread = 50'000; - std::vector threads; - for (int thread_number = 0; thread_number < thread_count; ++thread_number) { - threads.emplace_back([&distribution, thread_number] { - for (int index = 0; index < values_per_thread; ++index) { - distribution.record(1.0 + thread_number + index % 100); - } - }); - } - for (std::thread& thread : threads) thread.join(); - - double total = 0; - for (const auto& [bin, count] : - decode_sketch(distribution.encode_protobuf()).bins) { - total += count; - } - CHECK(total == thread_count * values_per_thread); -} - namespace { // A minimal protobuf reader, for the messages of a DDSketch. @@ -353,7 +342,8 @@ class ProtobufReader { std::uint64_t read_varint() { std::uint64_t value = 0; for (int shift = 0;; shift += 7) { - const auto byte = static_cast(data_.at(position_++)); + const unsigned char byte = + static_cast(data_.at(position_++)); value |= std::uint64_t{byte & 0x7fu} << shift; if ((byte & 0x80) == 0) return value; } @@ -362,7 +352,8 @@ class ProtobufReader { double read_double() { std::uint64_t bits = 0; for (int byte = 0; byte < 8; ++byte) { - const auto value = static_cast(data_.at(position_++)); + const unsigned char value = + static_cast(data_.at(position_++)); bits |= std::uint64_t{value} << (8 * byte); } return from_bits(bits); @@ -382,7 +373,7 @@ class ProtobufReader { }; std::int32_t unzigzag(std::uint64_t value) { - const auto low = static_cast(value); + const std::uint32_t low = static_cast(value); return static_cast((low >> 1) ^ (0u - (low & 1))); } @@ -451,16 +442,4 @@ DecodedSketch decode_sketch(const std::string& data) { return sketch; } -std::uint64_t to_bits(double value) { - std::uint64_t bits; - std::memcpy(&bits, &value, sizeof bits); - return bits; -} - -double from_bits(std::uint64_t bits) { - double value; - std::memcpy(&value, &bits, sizeof value); - return value; -} - } // namespace From 928d42c8933799ac2098f7ab9dcd15db8e492c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Mon, 23 Nov 2026 16:24:14 +0000 Subject: [PATCH 3/7] Compute telemetry sketch bins with std::log Removes the lookup table. --- src/datadog/telemetry/log_bin_index.cpp | 194 ----------------------- src/datadog/telemetry/log_bin_index.h | 15 +- test/telemetry/test_log_binned_distr.cpp | 66 -------- 3 files changed, 4 insertions(+), 271 deletions(-) diff --git a/src/datadog/telemetry/log_bin_index.cpp b/src/datadog/telemetry/log_bin_index.cpp index dd2b90e3..b0257e1f 100644 --- a/src/datadog/telemetry/log_bin_index.cpp +++ b/src/datadog/telemetry/log_bin_index.cpp @@ -1,119 +1,15 @@ #include "log_bin_index.h" -#include -#include #include namespace datadog::telemetry::log_bins { namespace { -// Finds bins without computing a logarithm. -// -// A positive double is 2^e × (1 + f). The fraction f is in [0, 1) and is -// stored in 52 bits. The top 6 bits of f give an integer m from 0 to 63. -// This means f is in [m/64, (m + 1)/64). The exponent e and m form a key. -// For example, between 1 and 2, the key with m = 0 holds [1, 1.015625). -// The key with m = 1 holds [1.015625, 1.03125). -// -// A key's doubles go from 2^e × (1 + m/64) up to, but excluding, -// 2^e × (1 + (m + 1)/64). Its largest and smallest doubles satisfy -// largest/smallest < (65 + m)/(64 + m) <= 65/64. -// The fraction (65 + m)/(64 + m) equals 65/64 only when m = 0. -// So every key has largest/smallest strictly below 65/64. -// The bin ratio is 65/64. Neighbouring bin edges are 65/64 apart. -// So at most one bin edge falls inside a key. Its doubles are in one bin -// or in two neighbouring bins. -// -// This holds for the exact logarithm. The bins come from a rounded `log`, -// so two edges may be a few doubles less than 65/64 apart. When m = 0, -// a key spans almost exactly 65/64. If an edge fell just after its first -// double, the next edge could also fall inside the key. Its doubles would -// then be in three bins. -// -// fill_entry() checks that each key holds doubles in at most two neighbouring -// bins. If the check fails, every value takes the slow path. -// -// For each key, the table stores the bin of its first double and where the -// next bin starts. It stores that start as the remaining 46 mantissa bits. -// Finding a bin is then one load and one compare. The table is filled with -// reference_bin_index(), so the table and reference_bin_index() always give -// the same bin. -// -// The table covers [2^-30, 2^65). Other values take the slow path. -class BinTable { - public: - static const BinTable& instance(); - - // Sets `bin` and returns true if the table covers the double `bits`. - bool find_bin(std::uint64_t bits, BinIndex& bin) const; - - // False if the table could not be built, and every value takes the slow - // path. - bool usable() const { return usable_keys_ == key_count; } - - private: - // A key is the exponent and the top 6 mantissa bits of a positive double. - static constexpr int key_shift = 52 - 6; - static constexpr std::uint64_t low_bits_mask = - (std::uint64_t{1} << key_shift) - 1; - static constexpr std::uint64_t first_key = std::uint64_t{1023 - 30} << 6; - static constexpr std::uint64_t key_count = std::uint64_t{30 + 65} << 6; - - static constexpr int edge_bits = key_shift + 1; - static constexpr int base_offset_bits = 64 - edge_bits; - // An edge value past every low bits, for keys whose doubles are all in one - // bin. - static constexpr std::uint64_t no_edge = low_bits_mask + 1; - - struct Entry { - // Low bits of the first double of the next bin, or `no_edge` if the next - // bin starts after the key. - std::uint64_t edge : edge_bits; - // Bin of the key's first double, minus `first_bin_`. - std::uint64_t base_offset : base_offset_bits; - }; - static_assert(sizeof(Entry) == sizeof(std::uint64_t)); - - BinTable(); - bool fill_entry(std::uint64_t key); - - BinIndex first_bin_; - // `key_count`, or 0 if the table could not be built. - std::uint64_t usable_keys_; - std::array entries_{}; -}; - -// Sets round-to-nearest while it lives, then restores the caller's mode. -class RoundToNearest { - public: - RoundToNearest() : saved_mode_(std::fegetround()) { - std::fesetround(FE_TONEAREST); - } - ~RoundToNearest() { std::fesetround(saved_mode_); } - RoundToNearest(const RoundToNearest&) = delete; - RoundToNearest& operator=(const RoundToNearest&) = delete; - - private: - int saved_mode_; -}; - double log_multiplier(); -std::uint64_t find_edge(BinIndex bin, std::uint64_t below, - std::uint64_t at_or_above); -void narrow_around_guess(BinIndex bin, std::uint64_t& below, - std::uint64_t& at_or_above); } // namespace BinIndex bin_index(double value) { - BinIndex bin; - if (BinTable::instance().find_bin(to_bits(value), bin)) return bin; - return reference_bin_index(value); -} - -bool fast_table_built() { return BinTable::instance().usable(); } - -BinIndex reference_bin_index(double value) { // Checks the bits, because -ffast-math can remove std::isnan() and friends. const std::uint64_t bits = to_bits(value); const bool not_finite = ((bits >> 52) & 0x7ff) == 0x7ff; @@ -130,101 +26,11 @@ BinIndex reference_bin_index(double value) { namespace { -// The table is built at run time, with the local `log`. C libraries do not -// all round `log` the same way, so a table built elsewhere could differ. -const BinTable& BinTable::instance() { - static const BinTable table; - return table; -} - -bool BinTable::find_bin(std::uint64_t bits, BinIndex& bin) const { - // Zero, negative values, NaN and infinities give keys out of range too. - const std::uint64_t key = (bits >> key_shift) - first_key; - if (key >= usable_keys_) return false; - - const Entry entry = entries_[key]; - const bool past_edge = (bits & low_bits_mask) >= entry.edge; - bin = first_bin_ + static_cast(entry.base_offset) + - static_cast(past_edge); - return true; -} - -// The first caller may have changed the rounding mode. The table must not -// depend on it. -BinTable::BinTable() : first_bin_(0), usable_keys_(key_count) { - const RoundToNearest rounding; - first_bin_ = reference_bin_index(from_bits(first_key << key_shift)); - for (std::uint64_t key = 0; key < key_count; ++key) { - if (!fill_entry(key)) { - usable_keys_ = 0; - return; - } - } -} - -bool BinTable::fill_entry(std::uint64_t key) { - const std::uint64_t first_bits = (first_key + key) << key_shift; - const std::uint64_t last_bits = first_bits | low_bits_mask; - const BinIndex base_bin = reference_bin_index(from_bits(first_bits)); - const BinIndex last_bin = reference_bin_index(from_bits(last_bits)); - const std::int64_t base_offset = std::int64_t{base_bin} - first_bin_; - if (base_offset < 0 || base_offset >> base_offset_bits != 0 || - last_bin < base_bin || last_bin > base_bin + 1) { - return false; - } - - Entry& entry = entries_[key]; - entry.base_offset = static_cast(base_offset); - entry.edge = no_edge; - if (last_bin > base_bin) { - entry.edge = find_edge(last_bin, first_bits, last_bits) - first_bits; - } - return true; -} - // Same formula as libdatadog: 1 / ln(bin_ratio). double log_multiplier() { static const double multiplier = 1.0 / std::log(bin_ratio); return multiplier; } -// Returns the bits of the smallest double whose bin is `bin`. Expects the bin -// of `below` to be lower than `bin`, and the bin of `at_or_above` not to be. -std::uint64_t find_edge(BinIndex bin, std::uint64_t below, - std::uint64_t at_or_above) { - narrow_around_guess(bin, below, at_or_above); - while (at_or_above - below > 1) { - const std::uint64_t middle = below + (at_or_above - below) / 2; - if (reference_bin_index(from_bits(middle)) >= bin) { - at_or_above = middle; - } else { - below = middle; - } - } - return at_or_above; -} - -// Moves the bounds close to the edge computed with `exp`, so that the search -// needs few steps. The bounds keep their meaning. -void narrow_around_guess(BinIndex bin, std::uint64_t& below, - std::uint64_t& at_or_above) { - const double guess_value = - std::exp((bin - bin_index_shift) / log_multiplier()); - const std::uint64_t guess = to_bits(guess_value); - if (guess <= below || guess >= at_or_above) return; - - for (std::uint64_t width = 64; width < at_or_above - below; width *= 64) { - const std::uint64_t low = guess > width ? guess - width : 0; - const std::uint64_t high = guess + width; - if (low > below && reference_bin_index(from_bits(low)) < bin) { - below = low; - } - if (high < at_or_above && reference_bin_index(from_bits(high)) >= bin) { - at_or_above = high; - } - if (below == low && at_or_above == high) return; - } -} - } // namespace } // namespace datadog::telemetry::log_bins diff --git a/src/datadog/telemetry/log_bin_index.h b/src/datadog/telemetry/log_bin_index.h index 2e4d1119..dcebbc78 100644 --- a/src/datadog/telemetry/log_bin_index.h +++ b/src/datadog/telemetry/log_bin_index.h @@ -24,18 +24,11 @@ inline constexpr double zero_threshold = inline constexpr BinIndex zero_bin = std::numeric_limits::min(); inline constexpr BinIndex rejected_value = zero_bin + 1; -// Returns the bin of `value`. Returns `zero_bin` for values below -// `zero_threshold`, and `rejected_value` for negative values, NaN and -// infinities. Same result as reference_bin_index(), but faster. -BinIndex bin_index(double value); - -// Computes the bin as floor(ln(value) × m + bin_index_shift), with +// Returns the bin of `value`, floor(ln(value) × m + bin_index_shift), with // m = 1 / ln(bin_ratio). The multiplication and the addition are rounded once. -// Declared here for the tests. The library only uses bin_index(). -BinIndex reference_bin_index(double value); - -// Returns true if the fast table was built. Declared here for the tests. -bool fast_table_built(); +// Returns `zero_bin` for values below `zero_threshold`, and `rejected_value` +// for negative values, NaN and infinities. +BinIndex bin_index(double value); // TODO: replace to_bits() and from_bits() with std::bit_cast after the upgrade // to C++20. diff --git a/test/telemetry/test_log_binned_distr.cpp b/test/telemetry/test_log_binned_distr.cpp index 04924296..fc77ae9b 100644 --- a/test/telemetry/test_log_binned_distr.cpp +++ b/test/telemetry/test_log_binned_distr.cpp @@ -19,7 +19,6 @@ namespace { using BinIndex = log_bins::BinIndex; using Bins = std::map; using log_bins::from_bits; -using log_bins::to_bits; struct DecodedSketch { double gamma = 0; @@ -64,26 +63,6 @@ Bins recorded_bins(const std::vector& bins) { return decode_sketch(distribution.encode_protobuf()).bins; } -// Returns the bits of the smallest double whose reference bin is at least -// `bin`, searching in (below, at_or_above]. -std::uint64_t reference_edge(BinIndex bin, std::uint64_t below, - std::uint64_t at_or_above) { - while (at_or_above - below > 1) { - const std::uint64_t middle = below + (at_or_above - below) / 2; - if (log_bins::reference_bin_index(from_bits(middle)) >= bin) { - at_or_above = middle; - } else { - below = middle; - } - } - return at_or_above; -} - -void check_same_bin(double value) { - INFO("value " << value << ", bits " << to_bits(value)); - CHECK(log_bins::bin_index(value) == log_bins::reference_bin_index(value)); -} - void check_bins(const std::vector& values) { LogBinnedDistribution distribution; for (const double value : values) distribution.record(value); @@ -146,14 +125,9 @@ LOG_BINNED_TEST("bin index of special values") { for (const auto& [value, expected_bin] : cases) { INFO("value " << value); CHECK(log_bins::bin_index(value) == expected_bin); - CHECK(log_bins::reference_bin_index(value) == expected_bin); } } -LOG_BINNED_TEST("the fast table is built") { - CHECK(log_bins::fast_table_built()); -} - LOG_BINNED_TEST("bin index of values far from an edge is exact") { // Exact bins, computed with 300-bit arithmetic (mpmath): the floor of // ln(x) / ln(65/64) + 1338.5. Each value is at least 0.04 bins from an edge. @@ -175,49 +149,9 @@ LOG_BINNED_TEST("bin index rounds the multiplication and the addition once") { // exact bin is 3253. Rounding the multiplication first would round the // result up to 3254. const double value = 7'903'045'517'381.0; - CHECK(log_bins::reference_bin_index(value) == 3253); CHECK(log_bins::bin_index(value) == 3253); } -LOG_BINNED_TEST("bin index equals the reference around every bin edge") { - // Covers the fast table, [2^-30, 2^65), and some bins on each side. - const std::uint64_t lowest_bits = to_bits(std::ldexp(1.0, -32)); - const std::uint64_t highest_bits = to_bits(std::ldexp(1.0, 67)); - const BinIndex first_bin = - log_bins::reference_bin_index(from_bits(lowest_bits)); - const BinIndex last_bin = - log_bins::reference_bin_index(from_bits(highest_bits)); - - std::uint64_t previous_edge = lowest_bits; - for (BinIndex bin = first_bin + 1; bin <= last_bin; ++bin) { - const std::uint64_t edge = reference_edge(bin, previous_edge, highest_bits); - for (std::uint64_t step = 0; step <= 6; ++step) { - check_same_bin(from_bits(edge - 3 + step)); - } - previous_edge = edge; - } -} - -LOG_BINNED_TEST("bin index equals the reference on many values") { - SECTION("log-uniform values") { - for (const double value : log_uniform(100'000, 1e-15, 1e25, 1)) { - check_same_bin(value); - } - } - SECTION("random bit patterns") { - std::mt19937_64 generator(2); - for (int count = 0; count < 100'000; ++count) { - check_same_bin(from_bits(generator())); - } - } - SECTION("integers and decimal fractions") { - for (int number = 0; number < 20'000; ++number) { - check_same_bin(number); - check_same_bin(number / 1000.0); - } - } -} - LOG_BINNED_TEST("record rejects invalid values") { LogBinnedDistribution distribution; CHECK_FALSE(distribution.record(std::nan(""))); From 7a8b11db88720a5c721c57cffcfa5c996e477117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Mon, 23 Nov 2026 16:30:58 +0000 Subject: [PATCH 4/7] Rename the telemetry sketch files after the class The class was renamed to LogBinnedDistribution in review, to use full words. The files kept the short log_binned_distr name, so they no longer matched the class they hold. They now do. --- BUILD.bazel | 4 ++-- CMakeLists.txt | 2 +- src/datadog/telemetry/log_bin_index.h | 4 ++-- .../{log_binned_distr.cpp => log_binned_distribution.cpp} | 2 +- .../{log_binned_distr.h => log_binned_distribution.h} | 0 test/CMakeLists.txt | 2 +- ..._log_binned_distr.cpp => test_log_binned_distribution.cpp} | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-) rename src/datadog/telemetry/{log_binned_distr.cpp => log_binned_distribution.cpp} (99%) rename src/datadog/telemetry/{log_binned_distr.h => log_binned_distribution.h} (100%) rename test/telemetry/{test_log_binned_distr.cpp => test_log_binned_distribution.cpp} (99%) diff --git a/BUILD.bazel b/BUILD.bazel index 459624d9..a869a1a2 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -77,8 +77,8 @@ cc_library( "src/datadog/telemetry/log.h", "src/datadog/telemetry/log_bin_index.cpp", "src/datadog/telemetry/log_bin_index.h", - "src/datadog/telemetry/log_binned_distr.cpp", - "src/datadog/telemetry/log_binned_distr.h", + "src/datadog/telemetry/log_binned_distribution.cpp", + "src/datadog/telemetry/log_binned_distribution.h", "src/datadog/telemetry/metric_context.h", "src/datadog/telemetry/telemetry.cpp", "src/datadog/telemetry/telemetry_impl.cpp", diff --git a/CMakeLists.txt b/CMakeLists.txt index 42f8e26e..f459c3f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,7 +184,7 @@ target_sources(dd-trace-cpp-objects src/datadog/common/hash.cpp src/datadog/telemetry/configuration.cpp src/datadog/telemetry/log_bin_index.cpp - src/datadog/telemetry/log_binned_distr.cpp + src/datadog/telemetry/log_binned_distribution.cpp src/datadog/telemetry/telemetry.cpp src/datadog/telemetry/telemetry_impl.cpp src/datadog/baggage.cpp diff --git a/src/datadog/telemetry/log_bin_index.h b/src/datadog/telemetry/log_bin_index.h index dcebbc78..54821310 100644 --- a/src/datadog/telemetry/log_bin_index.h +++ b/src/datadog/telemetry/log_bin_index.h @@ -1,7 +1,7 @@ #pragma once -// Bin index of the log-binned distributions. See log_binned_distr.h for the -// mapping. +// Bin index of the log-binned distributions. See log_binned_distribution.h +// for the mapping. #include #include diff --git a/src/datadog/telemetry/log_binned_distr.cpp b/src/datadog/telemetry/log_binned_distribution.cpp similarity index 99% rename from src/datadog/telemetry/log_binned_distr.cpp rename to src/datadog/telemetry/log_binned_distribution.cpp index 9cd6d021..da6608c2 100644 --- a/src/datadog/telemetry/log_binned_distr.cpp +++ b/src/datadog/telemetry/log_binned_distribution.cpp @@ -1,4 +1,4 @@ -#include "log_binned_distr.h" +#include "log_binned_distribution.h" #include #include diff --git a/src/datadog/telemetry/log_binned_distr.h b/src/datadog/telemetry/log_binned_distribution.h similarity index 100% rename from src/datadog/telemetry/log_binned_distr.h rename to src/datadog/telemetry/log_binned_distribution.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 148e9e0b..0c65bdae 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,7 +20,7 @@ add_executable(tests # telemetry test cases telemetry/test_configuration.cpp - telemetry/test_log_binned_distr.cpp + telemetry/test_log_binned_distribution.cpp telemetry/test_telemetry.cpp # test cases diff --git a/test/telemetry/test_log_binned_distr.cpp b/test/telemetry/test_log_binned_distribution.cpp similarity index 99% rename from test/telemetry/test_log_binned_distr.cpp rename to test/telemetry/test_log_binned_distribution.cpp index fc77ae9b..4d44b61e 100644 --- a/test/telemetry/test_log_binned_distr.cpp +++ b/test/telemetry/test_log_binned_distribution.cpp @@ -7,12 +7,12 @@ #include #include "datadog/telemetry/log_bin_index.h" -#include "datadog/telemetry/log_binned_distr.h" +#include "datadog/telemetry/log_binned_distribution.h" #include "test.h" using namespace datadog::telemetry; -#define LOG_BINNED_TEST(x) TEST_CASE(x, "[telemetry][log_binned_distr]") +#define LOG_BINNED_TEST(x) TEST_CASE(x, "[telemetry][log_binned_distribution]") namespace { From cfad94e909529b86edf2677f6a1bcade8fefcd33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Mon, 23 Nov 2026 17:40:25 +0000 Subject: [PATCH 5/7] Give the telemetry sketch a move that empties the source The implicit move took the counts vector but left the source's range behind. Recording into the source or encoding it then read past the end of an empty vector. The move now takes every member and resets the source to an empty sketch, as review suggested. Making a move fall back to a copy would also have kept the source valid, but every move would then allocate and copy up to 32 KiB of counts, and could throw. The move constructor goes through the move assignment, so the members are listed only once. --- .../telemetry/log_binned_distribution.cpp | 17 +++++++++ .../telemetry/log_binned_distribution.h | 8 ++++ .../test_log_binned_distribution.cpp | 38 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/datadog/telemetry/log_binned_distribution.cpp b/src/datadog/telemetry/log_binned_distribution.cpp index da6608c2..8876e306 100644 --- a/src/datadog/telemetry/log_binned_distribution.cpp +++ b/src/datadog/telemetry/log_binned_distribution.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "log_bin_index.h" @@ -32,6 +33,22 @@ std::uint32_t zigzag(std::int32_t value); } // namespace +LogBinnedDistribution::LogBinnedDistribution( + LogBinnedDistribution&& other) noexcept { + *this = std::move(other); +} + +LogBinnedDistribution& LogBinnedDistribution::operator=( + LogBinnedDistribution&& other) noexcept { + if (this == &other) return *this; + counts_ = std::exchange(other.counts_, {}); + window_start_ = std::exchange(other.window_start_, 0); + lowest_bin_ = std::exchange(other.lowest_bin_, 0); + bin_range_size_ = std::exchange(other.bin_range_size_, 0); + zero_count_ = std::exchange(other.zero_count_, 0); + return *this; +} + bool LogBinnedDistribution::record(double value) { static_assert(std::is_same_v); const BinIndex bin = log_bins::bin_index(value); diff --git a/src/datadog/telemetry/log_binned_distribution.h b/src/datadog/telemetry/log_binned_distribution.h index d02f15b5..e9849d9d 100644 --- a/src/datadog/telemetry/log_binned_distribution.h +++ b/src/datadog/telemetry/log_binned_distribution.h @@ -42,6 +42,14 @@ namespace datadog::telemetry { // * Not thread-safe: callers must lock. class LogBinnedDistribution { public: + LogBinnedDistribution() = default; + LogBinnedDistribution(const LogBinnedDistribution&) = default; + LogBinnedDistribution& operator=(const LogBinnedDistribution&) = default; + // A move leaves the source empty. The implicit move would leave the + // source's range without counts. + LogBinnedDistribution(LogBinnedDistribution&& other) noexcept; + LogBinnedDistribution& operator=(LogBinnedDistribution&& other) noexcept; + // Returns false if the value is rejected. bool record(double value); diff --git a/test/telemetry/test_log_binned_distribution.cpp b/test/telemetry/test_log_binned_distribution.cpp index 4d44b61e..e00fb7de 100644 --- a/test/telemetry/test_log_binned_distribution.cpp +++ b/test/telemetry/test_log_binned_distribution.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #include #include "datadog/telemetry/log_bin_index.h" @@ -152,6 +155,41 @@ LOG_BINNED_TEST("bin index rounds the multiplication and the addition once") { CHECK(log_bins::bin_index(value) == 3253); } +LOG_BINNED_TEST("a move takes the values and empties the source") { + static_assert(std::is_nothrow_move_constructible_v); + static_assert(std::is_nothrow_move_assignable_v); + LogBinnedDistribution source; + source.record(0.0); + source.record(1.0); + source.record(1000.0); + const std::string encoded = source.encode_protobuf(); + + SECTION("move construction") { + const LogBinnedDistribution target(std::move(source)); + CHECK(target.encode_protobuf() == encoded); + } + SECTION("move assignment") { + LogBinnedDistribution target; + target.record(5.0); + target = std::move(source); + CHECK(target.encode_protobuf() == encoded); + } + CHECK(source.encode_protobuf() == LogBinnedDistribution().encode_protobuf()); + CHECK(source.record(1000.0)); + LogBinnedDistribution expected; + expected.record(1000.0); + CHECK(source.encode_protobuf() == expected.encode_protobuf()); +} + +LOG_BINNED_TEST("a self-move keeps the values") { + LogBinnedDistribution distribution; + distribution.record(1.0); + const std::string encoded = distribution.encode_protobuf(); + LogBinnedDistribution& same = distribution; + distribution = std::move(same); + CHECK(distribution.encode_protobuf() == encoded); +} + LOG_BINNED_TEST("record rejects invalid values") { LogBinnedDistribution distribution; CHECK_FALSE(distribution.record(std::nan(""))); From dcf047873dde6677a600ed8ae25570fa055dd5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Mon, 23 Nov 2026 17:40:27 +0000 Subject: [PATCH 6/7] Keep telemetry sketch counts if the window allocation fails When a high value merged the lowest bins and also needed a larger window, the merged bins were zeroed before the new window was allocated. If that allocation threw and the caller caught it, the counts were gone, and later encodes silently left them out. Now the merged counts are summed first, the window grows, and only then are the merged bins cleared. The window growth is the only step that can throw, and it changes nothing until its new vector exists, so a failed record() leaves the sketch as it was. The test replaces the global operator new to fail on demand, only while a flag is set. --- .../telemetry/log_binned_distribution.cpp | 25 +++++++----- .../telemetry/log_binned_distribution.h | 3 +- .../test_log_binned_distribution.cpp | 40 +++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/datadog/telemetry/log_binned_distribution.cpp b/src/datadog/telemetry/log_binned_distribution.cpp index 8876e306..3d6f83b8 100644 --- a/src/datadog/telemetry/log_binned_distribution.cpp +++ b/src/datadog/telemetry/log_binned_distribution.cpp @@ -120,27 +120,32 @@ void LogBinnedDistribution::extend_down(BinIndex bin, BinIndex highest) { // the new lowest bin. void LogBinnedDistribution::extend_up(BinIndex bin, BinIndex highest) { const BinIndex new_lowest = std::max(lowest_bin_, bin - (max_bins - 1)); - const std::uint64_t merged = - take_counts(lowest_bin_, std::min(new_lowest - 1, highest)); + const BinIndex last_merged = std::min(new_lowest - 1, highest); + const std::uint64_t merged = sum_counts(lowest_bin_, last_merged); + // The window grows before any count changes. If the allocation throws, no + // count is lost. ensure_window_covers(new_lowest, bin); + // The new window may have dropped some merged bins. Clears the others. + clear_counts(std::max(lowest_bin_, window_start_), last_merged); lowest_bin_ = new_lowest; bin_range_size_ = static_cast(bin - new_lowest + 1); count_at(new_lowest) += merged; ++count_at(bin); } -// Sets the counts of bins [lowest, highest] to zero and returns their sum. -std::uint64_t LogBinnedDistribution::take_counts(BinIndex lowest, - BinIndex highest) { +// Returns the sum of the counts of bins [lowest, highest]. +std::uint64_t LogBinnedDistribution::sum_counts(BinIndex lowest, + BinIndex highest) const { std::uint64_t sum = 0; - for (BinIndex bin = lowest; bin <= highest; ++bin) { - std::uint64_t& count = count_at(bin); - sum += count; - count = 0; - } + for (BinIndex bin = lowest; bin <= highest; ++bin) sum += count_at(bin); return sum; } +// Sets the counts of bins [lowest, highest] to zero. +void LogBinnedDistribution::clear_counts(BinIndex lowest, BinIndex highest) { + for (BinIndex bin = lowest; bin <= highest; ++bin) count_at(bin) = 0; +} + // Makes the window cover bins [lowest, highest]. The new window has room on // both sides, so that the range can grow without moving it each time. void LogBinnedDistribution::ensure_window_covers(BinIndex lowest, diff --git a/src/datadog/telemetry/log_binned_distribution.h b/src/datadog/telemetry/log_binned_distribution.h index e9849d9d..e3fdb439 100644 --- a/src/datadog/telemetry/log_binned_distribution.h +++ b/src/datadog/telemetry/log_binned_distribution.h @@ -66,7 +66,8 @@ class LogBinnedDistribution { void start_range(BinIndex bin); void extend_down(BinIndex bin, BinIndex highest); void extend_up(BinIndex bin, BinIndex highest); - std::uint64_t take_counts(BinIndex lowest, BinIndex highest); + std::uint64_t sum_counts(BinIndex lowest, BinIndex highest) const; + void clear_counts(BinIndex lowest, BinIndex highest); void ensure_window_covers(BinIndex lowest, BinIndex highest); std::uint64_t& count_at(BinIndex bin); std::uint64_t count_at(BinIndex bin) const; diff --git a/test/telemetry/test_log_binned_distribution.cpp b/test/telemetry/test_log_binned_distribution.cpp index e00fb7de..2bc65869 100644 --- a/test/telemetry/test_log_binned_distribution.cpp +++ b/test/telemetry/test_log_binned_distribution.cpp @@ -1,8 +1,10 @@ #include #include +#include #include #include #include +#include #include #include #include @@ -13,6 +15,24 @@ #include "datadog/telemetry/log_binned_distribution.h" #include "test.h" +namespace { + +// While true, operator new throws std::bad_alloc. +bool fail_allocations = false; + +} // namespace + +// Replaced so that a test can make the window allocation fail. +void* operator new(std::size_t size) { + if (fail_allocations) throw std::bad_alloc(); + if (void* memory = std::malloc(size == 0 ? 1 : size)) return memory; + throw std::bad_alloc(); +} + +void operator delete(void* memory) noexcept { std::free(memory); } + +void operator delete(void* memory, std::size_t) noexcept { std::free(memory); } + using namespace datadog::telemetry; #define LOG_BINNED_TEST(x) TEST_CASE(x, "[telemetry][log_binned_distribution]") @@ -190,6 +210,26 @@ LOG_BINNED_TEST("a self-move keeps the values") { CHECK(distribution.encode_protobuf() == encoded); } +LOG_BINNED_TEST("record keeps old counts if the window allocation fails") { + LogBinnedDistribution distribution; + distribution.record(value_in_bin(0)); + distribution.record(value_in_bin(2047)); + + // Bin 3100 merges bins 0 to 1052, and needs a new window. + fail_allocations = true; + bool threw = false; + try { + distribution.record(value_in_bin(3100)); + } catch (const std::bad_alloc&) { + threw = true; + } + fail_allocations = false; + + CHECK(threw); + CHECK(decode_sketch(distribution.encode_protobuf()).bins == + Bins{{0, 1}, {2047, 1}}); +} + LOG_BINNED_TEST("record rejects invalid values") { LogBinnedDistribution distribution; CHECK_FALSE(distribution.record(std::nan(""))); From 14bdff619ea4a17c5417b798644fdb9cc3386d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Tue, 24 Nov 2026 11:09:20 +0000 Subject: [PATCH 7/7] Add nothrow overload for new/delete. Remove alloc_dealloc_mismatch=0 --- .github/workflows/dev.yml | 2 -- test/telemetry/test_log_binned_distribution.cpp | 12 ++++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 0d7d1dae..04c63b3c 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -64,8 +64,6 @@ jobs: run: cmake --build ${BUILD_DIR} -j -v - name: Test run: cd ${BUILD_DIR} && test/tests -r junit -o report.xml - env: - ASAN_OPTIONS: alloc_dealloc_mismatch=0 - name: Get Datadog credentials if: ${{ !cancelled() && github.event_name != 'pull_request' }} uses: ./.github/actions/dd-sts-credentials diff --git a/test/telemetry/test_log_binned_distribution.cpp b/test/telemetry/test_log_binned_distribution.cpp index 2bc65869..f5c7827f 100644 --- a/test/telemetry/test_log_binned_distribution.cpp +++ b/test/telemetry/test_log_binned_distribution.cpp @@ -29,10 +29,22 @@ void* operator new(std::size_t size) { throw std::bad_alloc(); } +void* operator new(std::size_t size, const std::nothrow_t&) noexcept { + try { + return ::operator new(size); + } catch (const std::bad_alloc&) { + return nullptr; + } +} + void operator delete(void* memory) noexcept { std::free(memory); } void operator delete(void* memory, std::size_t) noexcept { std::free(memory); } +void operator delete(void* memory, const std::nothrow_t&) noexcept { + std::free(memory); +} + using namespace datadog::telemetry; #define LOG_BINNED_TEST(x) TEST_CASE(x, "[telemetry][log_binned_distribution]")