add probability property null check (#1401)
This commit is contained in:
committed by
Marcin Grzejszczak
parent
3b7aa54516
commit
06ccdd60ab
@@ -22,6 +22,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import brave.sampler.Sampler;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* This sampler is appropriate for low-traffic instrumentation (ex servers that each
|
||||
* receive <100K requests), or those who do not provision random trace ids. It not
|
||||
@@ -52,6 +54,8 @@ public class ProbabilityBasedSampler extends Sampler {
|
||||
private final SamplerProperties configuration;
|
||||
|
||||
public ProbabilityBasedSampler(SamplerProperties configuration) {
|
||||
Assert.notNull(configuration.getProbability(),
|
||||
"probability property is required for ProbabilityBasedSampler");
|
||||
int outOf100 = (int) (configuration.getProbability() * 100.0f);
|
||||
this.sampleDecisions = randomBitSet(100, outOf100, new Random());
|
||||
this.configuration = configuration;
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Random;
|
||||
import brave.sampler.Sampler;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
/**
|
||||
@@ -78,6 +79,13 @@ public class ProbabilityBasedSamplerTests {
|
||||
then(numberOfSampledElements).isEqualTo(threshold);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_fail_given_no_probability() {
|
||||
assertThatThrownBy(() -> new ProbabilityBasedSampler(this.samplerConfiguration))
|
||||
.isInstanceOf(IllegalArgumentException.class).hasMessage(
|
||||
"probability property is required for ProbabilityBasedSampler");
|
||||
}
|
||||
|
||||
private int countNumberOfSampledElements(int numberOfIterations) {
|
||||
Sampler sampler = new ProbabilityBasedSampler(this.samplerConfiguration);
|
||||
int passedCounter = 0;
|
||||
|
||||
Reference in New Issue
Block a user