Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Fix overflow bug. (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnburn authored and isaachier committed Nov 22, 2017
1 parent 64f66e3 commit a1bea40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/jaegertracing/samplers/ProbabilisticSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class ProbabilisticSampler : public Sampler {
public:
explicit ProbabilisticSampler(double samplingRate)
: _samplingRate(std::max(0.0, std::min(samplingRate, 1.0)))
, _samplingBoundary(
static_cast<uint64_t>(kMaxRandomNumber * _samplingRate))
, _samplingBoundary(computeSamplingBoundary(_samplingRate))
, _tags({ { kSamplerTypeTagKey, kSamplerTypeProbabilistic },
{ kSamplerParamTagKey, _samplingRate } })
{
Expand All @@ -53,6 +52,20 @@ class ProbabilisticSampler : public Sampler {
double _samplingRate;
uint64_t _samplingBoundary;
std::vector<Tag> _tags;

static uint64_t computeSamplingBoundary(long double samplingRate)
{
const long double maxRandNumber = kMaxRandomNumber;
const auto samplingBoundary = samplingRate * maxRandNumber;

// Protect against overflow in case samplingBoundary rounds
// higher than kMaxRandNumber.
if (samplingBoundary == maxRandNumber) {
return kMaxRandomNumber;
}

return static_cast<uint64_t>(samplingBoundary);
}
};

} // namespace samplers
Expand Down
11 changes: 11 additions & 0 deletions src/jaegertracing/samplers/SamplerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ TEST(Sampler, testProbabilisticSamplerErrors)

TEST(Sampler, testProbabilisticSampler)
{
{
ProbabilisticSampler sampler(0.5);
auto result =
sampler.isSampled(TraceID(0, kTestMaxID + 10), kTestOperationName);
Expand All @@ -114,6 +115,16 @@ TEST(Sampler, testProbabilisticSampler)
result = sampler.isSampled(TraceID(0, kTestMaxID - 20), kTestOperationName);
ASSERT_TRUE(result.isSampled());
CMP_TAGS(testProbablisticExpectedTags, result.tags());
}
{
ProbabilisticSampler sampler(1.0);
auto result =
sampler.isSampled(TraceID(0, kTestMaxID), kTestOperationName);
ASSERT_TRUE(result.isSampled());

result = sampler.isSampled(TraceID(0, kTestMaxID - 20), kTestOperationName);
ASSERT_TRUE(result.isSampled());
}
}

TEST(Sampler, testProbabilisticSamplerPerformance)
Expand Down

0 comments on commit a1bea40

Please sign in to comment.