Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate constant traceidratio samplers to the constant implementations. #2417

Closed
Closed
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 @@ -27,22 +27,17 @@ abstract class TraceIdRatioBasedSampler implements Sampler {

TraceIdRatioBasedSampler() {}

static TraceIdRatioBasedSampler create(double ratio) {
static Sampler create(double ratio) {
if (ratio < 0.0 || ratio > 1.0) {
throw new IllegalArgumentException("ratio must be in range [0.0, 1.0]");
}
long idUpperBound;
// Special case the limits, to avoid any possible issues with lack of precision across
// double/long boundaries. For probability == 0.0, we use Long.MIN_VALUE as this guarantees
// that we will never sample a trace, even in the case where the id == Long.MIN_VALUE, since
// Math.Abs(Long.MIN_VALUE) == Long.MIN_VALUE.
if (ratio == 0.0) {
idUpperBound = Long.MIN_VALUE;
} else if (ratio == 1.0) {
idUpperBound = Long.MAX_VALUE;
} else {
idUpperBound = (long) (ratio * Long.MAX_VALUE);
return Sampler.alwaysOff();
}
if (ratio == 1.0) {
return Sampler.alwaysOn();
}
long idUpperBound = (long) (ratio * Long.MAX_VALUE);
return new AutoValue_TraceIdRatioBasedSampler(
ratio,
idUpperBound,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class TraceIdRatioBasedSamplerTest {

@Test
void alwaysSample() {
TraceIdRatioBasedSampler sampler = TraceIdRatioBasedSampler.create(1);
assertThat(sampler.getIdUpperBound()).isEqualTo(Long.MAX_VALUE);
Sampler sampler = TraceIdRatioBasedSampler.create(1);
assertThat(sampler).isEqualTo(Sampler.alwaysOn());
}

@Test
void neverSample() {
TraceIdRatioBasedSampler sampler = TraceIdRatioBasedSampler.create(0);
assertThat(sampler.getIdUpperBound()).isEqualTo(Long.MIN_VALUE);
Sampler sampler = TraceIdRatioBasedSampler.create(0);
assertThat(sampler).isEqualTo(Sampler.alwaysOff());
}

@Test
Expand Down