PerOperationSampler implements Sampler { @Override public synchronized SamplingStatus sample(String operation, long id) { GuaranteedThroughputSampler sampler = operationNameToSampler.get(operation); if (sampler != null) { return sampler.sample(operation, id); } if (operationNameToSampler.size() < maxOperations) { sampler = new GuaranteedThroughputSampler(defaultSampler.getSamplingRate(), lowerBound); operationNameToSampler.put(operation, sampler); return sampler.sample(operation, id); } return defaultSampler.sample(operation, id); } PerOperationSampler(int maxOperations, OperationSamplingParameters strategies); synchronized boolean update(final OperationSamplingParameters strategies); @Override synchronized SamplingStatus sample(String operation, long id); @Override synchronized void close();  }
@Test public void testFallbackToDefaultProbabilisticSampler() { undertest = new PerOperationSampler(0, operationToSamplers, defaultProbabilisticSampler, DEFAULT_LOWER_BOUND_TRACES_PER_SECOND); SamplingStatus samplingStatus = undertest.sample(OPERATION, TRACE_ID); assertTrue(samplingStatus.isSampled()); verify(defaultProbabilisticSampler).sample(OPERATION, TRACE_ID); }
@Test public void testCreateGuaranteedSamplerOnUnseenOperation() { String newOperation = "new OPERATION"; undertest.sample(newOperation, TRACE_ID); assertEquals(new GuaranteedThroughputSampler(DEFAULT_SAMPLING_PROBABILITY, DEFAULT_LOWER_BOUND_TRACES_PER_SECOND), operationToSamplers.get(newOperation)); }
@Test public void testPerOperationSamplerWithKnownOperation() { GuaranteedThroughputSampler sampler = mock(GuaranteedThroughputSampler.class); operationToSamplers.put(OPERATION, sampler); when(sampler.sample(OPERATION, TRACE_ID)).thenReturn(SamplingStatus.of(true, new HashMap<String, Object>())); SamplingStatus samplingStatus = undertest.sample(OPERATION, TRACE_ID); assertTrue(samplingStatus.isSampled()); verify(sampler).sample(OPERATION, TRACE_ID); verifyNoMoreInteractions(defaultProbabilisticSampler); }