Line data Source code
1 : #pragma once 2 : 3 : // This component provides sampling-related miscellanea. It's used by both 4 : // `TraceSampler` and `SpanSampler`. 5 : 6 : #include <cstdint> 7 : #include <limits> 8 : 9 : #include "rate.h" 10 : 11 : namespace datadog { 12 : namespace tracing { 13 : 14 : // Return a hash value for the specified `value`. `value` is one of the 15 : // following: 16 : // 17 : // - a 64-bit span ID 18 : // - a 64-bit trace ID 19 : // - the lower 64 bits of a 128-bit trace ID 20 : // 21 : // The resulting hash value is compared with an upper bound provided by 22 : // `max_id_from_rate` (below) to determine whether the span/trace associated 23 : // with `value` is eligible for keeping on statistical grounds. 24 84833 : inline std::uint64_t knuth_hash(std::uint64_t value) { 25 84833 : return value * UINT64_C(1111111111111111111); 26 : } 27 : 28 84833 : inline std::uint64_t max_id_from_rate(Rate rate) { 29 : // `double(std::numeric_limits<uint64_t>::max())` is slightly larger than the 30 : // largest `uint64_t`, but consider it a fun fact that the largest `double` 31 : // less than 1.0 (i.e. the "previous value" to 1.0), when multiplied by the 32 : // max `uint64_t`, results in a number not greater than the max `uint64_t`. 33 : // So, the only special case to consider is 1.0. 34 84833 : if (rate == 1.0) { 35 22814 : return std::numeric_limits<uint64_t>::max(); 36 : } 37 : 38 62019 : return rate * static_cast<double>(std::numeric_limits<std::uint64_t>::max()); 39 : } 40 : 41 : } // namespace tracing 42 : } // namespace datadog