Skip to content

Commit

Permalink
report zero values for unexpected overflows in MonotonicCounterUint
Browse files Browse the repository at this point in the history
Resolves #98.
  • Loading branch information
copperlight committed Aug 16, 2024
1 parent 03fcddd commit ea69ce1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion spectator/monotonic_counter_uint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace spectator {

static constexpr auto kOverflow = 9.223372e+18; // 2^63
static constexpr auto kNaN = std::numeric_limits<double>::quiet_NaN();
static constexpr auto kMax = std::numeric_limits<uint64_t>::max();

Expand Down Expand Up @@ -36,7 +37,12 @@ void MonotonicCounterUint::Measure(Measurements* results) const noexcept {
if (!count_id_) {
count_id_ = std::make_unique<Id>(MeterId().WithDefaultStat(refs().count()));
}
results->emplace_back(*count_id_, delta);
if (delta > kOverflow) {
// deltas that appear to be unexpected overflows will be reported as zeroes instead
results->emplace_back(*count_id_, 0);
} else {
results->emplace_back(*count_id_, delta);
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions spectator/monotonic_counter_uint_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ TEST(MonotonicCounterUint, Overflow) {
EXPECT_EQ(expected3, ms.back());
}

TEST(MonotonicCounterUint, UnexpectedOverflow) {
auto c = getMonotonicCounterUint("overflow");
EXPECT_TRUE(std::isnan(c.Delta()));

// initialize overflow condition
spectator::Measurements ms;
c.Set(0);
c.Measure(&ms);
EXPECT_TRUE(ms.empty());

c.Set(100000);
c.Measure(&ms);
EXPECT_EQ(ms.size(), 1);
auto id = c.MeterId().WithStat(refs().count());
auto expected1 = spectator::Measurement{id, 100000};
EXPECT_EQ(expected1, ms.back());

// trigger overflow condition
c.Set(0);
c.Measure(&ms);
EXPECT_EQ(ms.size(), 2);
auto expected2 = spectator::Measurement{id, 0};
EXPECT_EQ(expected2, ms.back());

// normal increment condition
c.Set(5);
c.Measure(&ms);
EXPECT_EQ(ms.size(), 3);
auto expected3 = spectator::Measurement{id, 5};
EXPECT_EQ(expected3, ms.back());
}

TEST(MonotonicCounterUint, Id) {
auto c = getMonotonicCounterUint("id");
auto id = spectator::Id("id", spectator::Tags{});
Expand Down

0 comments on commit ea69ce1

Please sign in to comment.