|
| 1 | +package datadog.trace.common.sampling; |
| 2 | + |
| 3 | +import static datadog.trace.api.config.TracerConfig.TRACE_SAMPLE_RATE; |
| 4 | +import static datadog.trace.api.config.TracerConfig.TRACE_SAMPLING_RULES; |
| 5 | +import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_DROP; |
| 6 | +import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_KEEP; |
| 7 | +import static datadog.trace.api.sampling.PrioritySampling.USER_KEEP; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 12 | + |
| 13 | +import datadog.trace.api.Config; |
| 14 | +import datadog.trace.common.writer.ListWriter; |
| 15 | +import datadog.trace.common.writer.RemoteResponseListener; |
| 16 | +import datadog.trace.core.CoreTracer; |
| 17 | +import datadog.trace.core.DDCoreJavaSpecification; |
| 18 | +import datadog.trace.core.DDSpan; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Properties; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +/** |
| 25 | + * Agent published sampling rates must still apply to spans that match no sampling rule. |
| 26 | + * |
| 27 | + * <p>Only one sampler is registered to receive those rates, so a rule based sampler has to forward |
| 28 | + * them to the fallback it delegates to. |
| 29 | + */ |
| 30 | +class RuleBasedSamplerAgentRatesTest extends DDCoreJavaSpecification { |
| 31 | + |
| 32 | + private static final String RULE_MISSING_SERVICE = |
| 33 | + "[{\"service\": \"other-service\", \"sample_rate\": 1}]"; |
| 34 | + |
| 35 | + @Test |
| 36 | + void ruleBasedSamplerForwardsAgentRatesToFallback() { |
| 37 | + Properties properties = new Properties(); |
| 38 | + properties.setProperty(TRACE_SAMPLING_RULES, RULE_MISSING_SERVICE); |
| 39 | + Sampler sampler = Sampler.Builder.forConfig(properties); |
| 40 | + assertInstanceOf(RuleBasedTraceSampler.class, sampler); |
| 41 | + |
| 42 | + // The agent asks for everything to be dropped for this service. |
| 43 | + assertInstanceOf(RemoteResponseListener.class, sampler); |
| 44 | + ((RemoteResponseListener) sampler).onResponse("traces", rateByService("service", "bar", 0.0)); |
| 45 | + |
| 46 | + CoreTracer tracer = tracerBuilder().writer(new ListWriter()).build(); |
| 47 | + try { |
| 48 | + DDSpan span = buildSpan(tracer, "service"); |
| 49 | + ((PrioritySampler) sampler).setSamplingPriority(span); |
| 50 | + |
| 51 | + assertEquals(SAMPLER_DROP, (int) span.getSamplingPriority()); |
| 52 | + assertEquals(0.0, span.getTag(RateByServiceTraceSampler.SAMPLING_AGENT_RATE)); |
| 53 | + } finally { |
| 54 | + tracer.close(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void agentRatesDoNotOverrideMatchedRules() { |
| 60 | + Properties properties = new Properties(); |
| 61 | + properties.setProperty( |
| 62 | + TRACE_SAMPLING_RULES, "[{\"service\": \"service\", \"sample_rate\": 1}]"); |
| 63 | + Sampler sampler = Sampler.Builder.forConfig(properties); |
| 64 | + |
| 65 | + ((RemoteResponseListener) sampler).onResponse("traces", rateByService("service", "bar", 0.0)); |
| 66 | + |
| 67 | + CoreTracer tracer = tracerBuilder().writer(new ListWriter()).build(); |
| 68 | + try { |
| 69 | + DDSpan span = buildSpan(tracer, "service"); |
| 70 | + ((PrioritySampler) sampler).setSamplingPriority(span); |
| 71 | + |
| 72 | + assertEquals(USER_KEEP, (int) span.getSamplingPriority()); |
| 73 | + assertEquals(1.0, span.getTag(RuleBasedTraceSampler.SAMPLING_RULE_RATE)); |
| 74 | + assertNull(span.getTag(RateByServiceTraceSampler.SAMPLING_AGENT_RATE)); |
| 75 | + } finally { |
| 76 | + tracer.close(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void defaultRateKeepsAgentRatesUnused() { |
| 82 | + Properties properties = new Properties(); |
| 83 | + properties.setProperty(TRACE_SAMPLING_RULES, RULE_MISSING_SERVICE); |
| 84 | + // A default rate matches every span, so the fallback is never reached. |
| 85 | + properties.setProperty(TRACE_SAMPLE_RATE, "1"); |
| 86 | + Sampler sampler = Sampler.Builder.forConfig(properties); |
| 87 | + |
| 88 | + ((RemoteResponseListener) sampler).onResponse("traces", rateByService("service", "bar", 0.0)); |
| 89 | + |
| 90 | + CoreTracer tracer = tracerBuilder().writer(new ListWriter()).build(); |
| 91 | + try { |
| 92 | + DDSpan span = buildSpan(tracer, "service"); |
| 93 | + ((PrioritySampler) sampler).setSamplingPriority(span); |
| 94 | + |
| 95 | + assertEquals(USER_KEEP, (int) span.getSamplingPriority()); |
| 96 | + assertNull(span.getTag(RateByServiceTraceSampler.SAMPLING_AGENT_RATE)); |
| 97 | + } finally { |
| 98 | + tracer.close(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void rulesKeepTheSuppliedAgentSampler() { |
| 104 | + RateByServiceTraceSampler agentSampler = new RateByServiceTraceSampler(); |
| 105 | + Properties properties = new Properties(); |
| 106 | + properties.setProperty(TRACE_SAMPLING_RULES, RULE_MISSING_SERVICE); |
| 107 | + |
| 108 | + Sampler sampler = Sampler.Builder.forConfig(Config.get(properties), null, agentSampler); |
| 109 | + |
| 110 | + assertInstanceOf(RuleBasedTraceSampler.class, sampler); |
| 111 | + assertSame(agentSampler, ((RuleBasedTraceSampler<?>) sampler).agentSampler()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void noRulesReturnsTheSuppliedAgentSampler() { |
| 116 | + RateByServiceTraceSampler agentSampler = new RateByServiceTraceSampler(); |
| 117 | + |
| 118 | + Sampler sampler = Sampler.Builder.forConfig(Config.get(new Properties()), null, agentSampler); |
| 119 | + |
| 120 | + assertSame(agentSampler, sampler); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void ratesLearnedBeforeARebuildStillApplyAfterIt() { |
| 125 | + RateByServiceTraceSampler agentSampler = new RateByServiceTraceSampler(); |
| 126 | + agentSampler.onResponse("traces", rateByService("service", "bar", 0.0)); |
| 127 | + |
| 128 | + // Rebuilding the sampler with the same agent sampler keeps the rates it has already learned. |
| 129 | + Properties properties = new Properties(); |
| 130 | + properties.setProperty(TRACE_SAMPLING_RULES, RULE_MISSING_SERVICE); |
| 131 | + Sampler sampler = Sampler.Builder.forConfig(Config.get(properties), null, agentSampler); |
| 132 | + |
| 133 | + CoreTracer tracer = tracerBuilder().writer(new ListWriter()).build(); |
| 134 | + try { |
| 135 | + DDSpan span = buildSpan(tracer, "service"); |
| 136 | + ((PrioritySampler) sampler).setSamplingPriority(span); |
| 137 | + |
| 138 | + assertEquals(SAMPLER_DROP, (int) span.getSamplingPriority()); |
| 139 | + } finally { |
| 140 | + tracer.close(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void unknownServiceIsKeptUntilTheAgentReportsARate() { |
| 146 | + Properties properties = new Properties(); |
| 147 | + properties.setProperty(TRACE_SAMPLING_RULES, RULE_MISSING_SERVICE); |
| 148 | + Sampler sampler = Sampler.Builder.forConfig(properties); |
| 149 | + |
| 150 | + CoreTracer tracer = tracerBuilder().writer(new ListWriter()).build(); |
| 151 | + try { |
| 152 | + DDSpan span = buildSpan(tracer, "service"); |
| 153 | + ((PrioritySampler) sampler).setSamplingPriority(span); |
| 154 | + |
| 155 | + assertEquals(SAMPLER_KEEP, (int) span.getSamplingPriority()); |
| 156 | + assertEquals(1.0, span.getTag(RateByServiceTraceSampler.SAMPLING_AGENT_RATE)); |
| 157 | + } finally { |
| 158 | + tracer.close(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static DDSpan buildSpan(CoreTracer tracer, String serviceName) { |
| 163 | + return (DDSpan) |
| 164 | + tracer |
| 165 | + .buildSpan("datadog", "operation") |
| 166 | + .withServiceName(serviceName) |
| 167 | + .withTag("env", "bar") |
| 168 | + .ignoreActiveSpan() |
| 169 | + .start(); |
| 170 | + } |
| 171 | + |
| 172 | + private static Map<String, Map<String, Number>> rateByService( |
| 173 | + String service, String env, double rate) { |
| 174 | + Map<String, Number> byService = new HashMap<>(); |
| 175 | + byService.put("service:" + service + ",env:" + env, rate); |
| 176 | + Map<String, Map<String, Number>> response = new HashMap<>(); |
| 177 | + response.put("rate_by_service", byService); |
| 178 | + return response; |
| 179 | + } |
| 180 | +} |
0 commit comments