Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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_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",
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_distribution.cpp
src/datadog/telemetry/telemetry.cpp
src/datadog/telemetry/telemetry_impl.cpp
src/datadog/baggage.cpp
Expand Down
36 changes: 36 additions & 0 deletions src/datadog/telemetry/log_bin_index.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "log_bin_index.h"

#include <cmath>

namespace datadog::telemetry::log_bins {
namespace {

double log_multiplier();

} // namespace

BinIndex 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<BinIndex>(std::floor(scaled_log));
}

namespace {

// Same formula as libdatadog: 1 / ln(bin_ratio).
double log_multiplier() {
static const double multiplier = 1.0 / std::log(bin_ratio);
return multiplier;
}

} // namespace
} // namespace datadog::telemetry::log_bins
50 changes: 50 additions & 0 deletions src/datadog/telemetry/log_bin_index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

// Bin index of the log-binned distributions. See log_binned_distribution.h
// for the mapping.

#include <cstdint>
#include <cstring>
#include <limits>

namespace datadog::telemetry::log_bins {

// 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.
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<double>::min() * bin_ratio;

// Special results of bin_index().
inline constexpr BinIndex zero_bin = std::numeric_limits<BinIndex>::min();
inline constexpr BinIndex rejected_value = zero_bin + 1;
Comment thread
xlamorlette-datadog marked this conversation as resolved.
Resolved

// 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.
// 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.

// 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
248 changes: 248 additions & 0 deletions src/datadog/telemetry/log_binned_distribution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
#include "log_binned_distribution.h"

#include <algorithm>
#include <type_traits>
#include <utility>

#include "log_bin_index.h"

namespace datadog::telemetry {
namespace {

using BinIndex = log_bins::BinIndex;

constexpr std::int32_t max_bins = 2048;
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

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<BinIndex, log_bins::BinIndex>);
const BinIndex bin = log_bins::bin_index(value);
if (bin == log_bins::rejected_value) return false;

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. 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());
if (zero_count_ != 0) {
append_double(sketch, 4, static_cast<double>(zero_count_));
}
return sketch;
}

void LogBinnedDistribution::add_to_bin(BinIndex bin) {
const std::uint32_t position =
static_cast<std::uint32_t>(bin) - static_cast<std::uint32_t>(lowest_bin_);
if (position < bin_range_size_) {
++count_at(bin);
return;
}
extend_range(bin);
}

// Same rules as libdatadog's LowCollapsingDenseStore.
void LogBinnedDistribution::extend_range(BinIndex bin) {
if (bin_range_size_ == 0) {
start_range(bin);
return;
}
const BinIndex highest =
lowest_bin_ + static_cast<std::int32_t>(bin_range_size_) - 1;
if (bin < lowest_bin_) {
extend_down(bin, highest);
} else {
extend_up(bin, highest);
}
}

void LogBinnedDistribution::start_range(BinIndex bin) {
ensure_window_covers(bin, bin);
lowest_bin_ = bin;
bin_range_size_ = 1;
++count_at(bin);
}

// The range grows down while it has room. Values below it go to its lowest
// bin.
void LogBinnedDistribution::extend_down(BinIndex bin, BinIndex highest) {
const BinIndex new_lowest = std::max(bin, highest - (max_bins - 1));
ensure_window_covers(new_lowest, highest);
lowest_bin_ = new_lowest;
bin_range_size_ = static_cast<std::uint32_t>(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 LogBinnedDistribution::extend_up(BinIndex bin, BinIndex highest) {
const BinIndex new_lowest = std::max(lowest_bin_, bin - (max_bins - 1));
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<std::uint32_t>(bin - new_lowest + 1);
count_at(new_lowest) += merged;
++count_at(bin);
}

// 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) 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,
BinIndex highest) {
const BinIndex window_end =
window_start_ + static_cast<std::int32_t>(counts_.size());
if (!counts_.empty() && lowest >= window_start_ && highest < window_end) {
return;
}

const std::uint32_t needed = static_cast<std::uint32_t>(highest - lowest + 1);
const std::uint32_t size =
std::clamp(2 * needed, min_window_size, max_window_size);
const BinIndex start =
lowest - static_cast<std::int32_t>((size - needed) / 2);
std::vector<std::uint64_t> counts(size);
const BinIndex copy_start = std::max(window_start_, start);
const BinIndex copy_end =
std::min(window_end, start + static_cast<std::int32_t>(size));
for (BinIndex bin = copy_start; bin < copy_end; ++bin) {
counts[static_cast<std::size_t>(bin - start)] = count_at(bin);
}
counts_.swap(counts);
window_start_ = start;
}

std::uint64_t& LogBinnedDistribution::count_at(BinIndex bin) {
return counts_[static_cast<std::size_t>(bin - window_start_)];
}

std::uint64_t LogBinnedDistribution::count_at(BinIndex bin) const {
return counts_[static_cast<std::size_t>(bin - window_start_)];
}

// Store message: binCounts = 1 (map<sint32, double>), with only the bins that
// are not empty.
std::string LogBinnedDistribution::encode_bins() const {
std::string store;
for (std::uint32_t position = 0; position < bin_range_size_; ++position) {
const BinIndex bin = lowest_bin_ + static_cast<std::int32_t>(position);
const std::uint64_t count = count_at(bin);
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_tag(entry, 1, WireType::varint);
append_varint(entry, zigzag(bin));
append_double(entry, 2, static_cast<double>(count));
return entry;
}

void append_message(std::string& out, std::uint32_t field,
const std::string& message) {
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_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<char>((bits >> (8 * byte)) & 0xff));
}
}

void append_tag(std::string& out, std::uint32_t field, WireType wire_type) {
append_varint(out, (field << 3) | static_cast<std::uint32_t>(wire_type));
}

void append_varint(std::string& out, std::uint64_t value) {
while (value >= 0x80) {
out.push_back(static_cast<char>((value & 0x7f) | 0x80));
value >>= 7;
}
out.push_back(static_cast<char>(value));
}

std::uint32_t zigzag(std::int32_t value) {
const std::uint32_t shifted = static_cast<std::uint32_t>(value) << 1;
return value < 0 ? ~shifted : shifted;
}

} // namespace
} // namespace datadog::telemetry
Loading
Loading