RemoteControlledSampler implements Sampler { void updateSampler() { SamplingStrategyResponse response; try { response = manager.getSamplingStrategy(serviceName); metrics.samplerRetrieved.inc(1); } catch (SamplingStrategyErrorException e) { metrics.samplerQueryFailure.inc(1); return; } if (response.getOperationSampling() != null) { updatePerOperationSampler(response.getOperationSampling()); } else { updateRateLimitingOrProbabilisticSampler(response); } } private  RemoteControlledSampler(Builder builder); @Override SamplingStatus sample(String operation, long id); @Override boolean equals(Object sampler); @Override void close(); static final String TYPE; }
@Test public void testUpdateToProbabilisticSampler() throws Exception { final double samplingRate = 0.55; SamplingStrategyResponse probabilisticResponse = new SamplingStrategyResponse( new ProbabilisticSamplingStrategy(samplingRate), null, null); when(samplingManager.getSamplingStrategy(SERVICE_NAME)).thenReturn(probabilisticResponse); undertest.updateSampler(); assertEquals(new ProbabilisticSampler(samplingRate), undertest.getSampler()); }
@Test public void testUpdateToRateLimitingSampler() throws Exception { final int tracesPerSecond = 22; SamplingStrategyResponse rateLimitingResponse = new SamplingStrategyResponse(null, new RateLimitingSamplingStrategy(tracesPerSecond), null); when(samplingManager.getSamplingStrategy(SERVICE_NAME)).thenReturn(rateLimitingResponse); undertest.updateSampler(); assertEquals(new RateLimitingSampler(tracesPerSecond), undertest.getSampler()); }
@Test public void testUpdateToPerOperationSamplerReplacesProbabilisticSampler() throws Exception { List<PerOperationSamplingParameters> operationToSampler = new ArrayList<>(); operationToSampler.add(new PerOperationSamplingParameters("operation", new ProbabilisticSamplingStrategy(0.1))); OperationSamplingParameters parameters = new OperationSamplingParameters(0.11, 0.22, operationToSampler); SamplingStrategyResponse response = new SamplingStrategyResponse(null, null, parameters); when(samplingManager.getSamplingStrategy(SERVICE_NAME)).thenReturn(response); undertest.updateSampler(); PerOperationSampler perOperationSampler = new PerOperationSampler(2000, parameters); Sampler actualSampler = undertest.getSampler(); assertEquals(perOperationSampler, actualSampler); }
@Test public void testNullResponse() throws Exception { when(samplingManager.getSamplingStrategy(SERVICE_NAME)).thenReturn(new SamplingStrategyResponse(null, null, null)); undertest.updateSampler(); assertEquals(initialSampler, undertest.getSampler()); }
@Test public void testUnparseableResponse() throws Exception { when(samplingManager.getSamplingStrategy(SERVICE_NAME)).thenThrow(new SamplingStrategyErrorException("test")); undertest.updateSampler(); assertEquals(initialSampler, undertest.getSampler()); }