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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include <cstdint>
#include <string>
#include <vector>

Expand Down Expand Up @@ -42,8 +43,8 @@ struct RuleBasedPredicateOptions
bool match_span_kind_producer = false;
bool match_span_kind_consumer = false;

// Significant digits used to format double attribute values for matching.
int double_precision = 6;
// Significant digits used to format double attribute values for matching, capped at 17.
std::uint32_t double_precision = 6;
};

/**
Expand Down
12 changes: 9 additions & 3 deletions sdk/src/trace/samplers/rule_based_predicate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <limits>
#include <string>
#include <utility>

Expand All @@ -33,12 +34,17 @@ namespace nostd = opentelemetry::nostd;
// Fits the widest value below: a 17 significant digit double takes 24 characters, an int64 20.
constexpr std::size_t kValueBufferSize = 32;

// No two doubles differ past max_digits10 significant digits.
constexpr std::uint32_t kMaxDoublePrecision =
static_cast<std::uint32_t>(std::numeric_limits<double>::max_digits10);

// Checks every string form of an attribute value; array values match if any item matches.
class ValueMatcher
{
public:
ValueMatcher(function_ref<bool(nostd::string_view)> check, int double_precision)
: check_(check), double_precision_(double_precision)
ValueMatcher(function_ref<bool(nostd::string_view)> check, std::uint32_t double_precision)
: check_(check),
double_precision_(static_cast<int>((std::min)(double_precision, kMaxDoublePrecision)))
{}

bool operator()(bool v) const { return check_(v ? "true" : "false"); }
Expand Down Expand Up @@ -111,7 +117,7 @@ class ValueMatcher

bool AttributeMatches(const opentelemetry::common::KeyValueIterable &attributes,
const std::string &key,
int double_precision,
std::uint32_t double_precision,
function_ref<bool(nostd::string_view)> check) noexcept
{
bool matched = false;
Expand Down
19 changes: 19 additions & 0 deletions sdk/test/trace/composable_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,25 @@ TEST(RuleBasedPredicate, AttributePatterns)
RuleBasedPredicate wide(std::move(precise));
EXPECT_TRUE(Matches(wide, {{"value", 1234567.89}}));
EXPECT_FALSE(Matches(wide, {{"value", 1234567.0}}));

// A double_precision of zero means one significant digit, not zero.
RuleBasedPredicateOptions coarse;
coarse.match_patterns = true;
coarse.patterns_key = "value";
coarse.included = {"4e+02"};
coarse.double_precision = 0;
RuleBasedPredicate single_digit(std::move(coarse));
EXPECT_TRUE(Matches(single_digit, {{"value", 404.0}}));
EXPECT_TRUE(Matches(single_digit, {{"value", 444.0}}));

// Precision past the digits a double carries is capped at 17, not passed on to snprintf.
RuleBasedPredicateOptions unbounded;
unbounded.match_patterns = true;
unbounded.patterns_key = "value";
unbounded.included = {"0.10000000000000001"};
unbounded.double_precision = std::numeric_limits<uint32_t>::max();
RuleBasedPredicate capped(std::move(unbounded));
EXPECT_TRUE(Matches(capped, {{"value", 0.1}}));
}

TEST(RuleBasedPredicate, ActiveGroupsAreAnded)
Expand Down
Loading