Ensure default sampler is available

This avoids the possible null pointer reference.
This commit is contained in:
Tyler Benson 2019-09-10 15:29:13 -07:00
parent 310ec93135
commit 3d1f2b9d01
2 changed files with 6 additions and 11 deletions

View File

@ -90,9 +90,10 @@ public class RateByServiceSampler implements Sampler, ResponseListener {
log.debug("Unable to parse new service rate {} -> {}", key, value); log.debug("Unable to parse new service rate {} -> {}", key, value);
} }
} }
if (!updatedServiceRates.isEmpty()) { if (!updatedServiceRates.containsKey(DEFAULT_KEY)) {
serviceRates = unmodifiableMap(updatedServiceRates); updatedServiceRates.put(DEFAULT_KEY, new RateSampler(DEFAULT_RATE));
} }
serviceRates = unmodifiableMap(updatedServiceRates);
} }
} }
@ -107,16 +108,12 @@ public class RateByServiceSampler implements Sampler, ResponseListener {
/** The sample rate used */ /** The sample rate used */
private final double sampleRate; private final double sampleRate;
public RateSampler(final String sampleRate) {
this(sampleRate == null ? 1 : Double.valueOf(sampleRate));
}
/** /**
* Build an instance of the sampler. The Sample rate is fixed for each instance. * Build an instance of the sampler. The Sample rate is fixed for each instance.
* *
* @param sampleRate a number [0,1] representing the rate ratio. * @param sampleRate a number [0,1] representing the rate ratio.
*/ */
public RateSampler(double sampleRate) { private RateSampler(double sampleRate) {
if (sampleRate < 0) { if (sampleRate < 0) {
sampleRate = 1; sampleRate = 1;

View File

@ -35,22 +35,20 @@ class RateByServiceSamplerTest extends Specification {
ObjectMapper serializer = new ObjectMapper() ObjectMapper serializer = new ObjectMapper()
when: when:
String response = '{"rate_by_service": {"service:,env:":1.0, "service:spock,env:test":0.000001}}' String response = '{"rate_by_service": {"service:spock,env:test":0.0}}'
serviceSampler.onResponse("traces", serializer.readTree(response)) serviceSampler.onResponse("traces", serializer.readTree(response))
DDSpan span1 = SpanFactory.newSpanOf("foo", "bar") DDSpan span1 = SpanFactory.newSpanOf("foo", "bar")
serviceSampler.initializeSamplingPriority(span1) serviceSampler.initializeSamplingPriority(span1)
then: then:
span1.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP span1.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP
serviceSampler.sample(span1) serviceSampler.sample(span1)
// !serviceSampler.sample(SpanFactory.newSpanOf("spock", "test"))
when: when:
response = '{"rate_by_service": {"service:,env:":0.000001, "service:spock,env:test":1.0}}' response = '{"rate_by_service": {"service:spock,env:test":1.0}}'
serviceSampler.onResponse("traces", serializer.readTree(response)) serviceSampler.onResponse("traces", serializer.readTree(response))
DDSpan span2 = SpanFactory.newSpanOf("spock", "test") DDSpan span2 = SpanFactory.newSpanOf("spock", "test")
serviceSampler.initializeSamplingPriority(span2) serviceSampler.initializeSamplingPriority(span2)
then: then:
// !serviceSampler.sample(SpanFactory.newSpanOf("foo", "bar"))
span2.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP span2.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP
serviceSampler.sample(span2) serviceSampler.sample(span2)
} }