Ensure default sampler is available
This avoids the possible null pointer reference.
MODIFY
dd-trace-ot/src/main/java/datadog/trace/common/sampling/RateByServiceSampler.java
dd-trace-ot/src/main/java/datadog/trace/common/sampling/RateByServiceSampler.java
@@ -90,9 +90,10 @@ public class RateByServiceSampler implements Sampler, ResponseListener {
log.debug("Unable to parse new service rate {} -> {}", key, value);
}
}
- if (!updatedServiceRates.isEmpty()) {
- serviceRates = unmodifiableMap(updatedServiceRates);
+ if (!updatedServiceRates.containsKey(DEFAULT_KEY)) {
+ 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 */
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.
*
* @param sampleRate a number [0,1] representing the rate ratio.
*/
- public RateSampler(double sampleRate) {
+ private RateSampler(double sampleRate) {
if (sampleRate < 0) {
sampleRate = 1;

MODIFY
dd-trace-ot/src/test/groovy/datadog/trace/api/sampling/RateByServiceSamplerTest.groovy
dd-trace-ot/src/test/groovy/datadog/trace/api/sampling/RateByServiceSamplerTest.groovy
@@ -35,22 +35,20 @@ class RateByServiceSamplerTest extends Specification {
ObjectMapper serializer = new ObjectMapper()
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))
DDSpan span1 = SpanFactory.newSpanOf("foo", "bar")
serviceSampler.initializeSamplingPriority(span1)
then:
span1.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP
serviceSampler.sample(span1)
- // !serviceSampler.sample(SpanFactory.newSpanOf("spock", "test"))
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))
DDSpan span2 = SpanFactory.newSpanOf("spock", "test")
serviceSampler.initializeSamplingPriority(span2)
then:
- // !serviceSampler.sample(SpanFactory.newSpanOf("foo", "bar"))
span2.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP
serviceSampler.sample(span2)
}

Ensure default sampler is available

This avoids the possible null pointer reference.