GuaranteedThroughputSampler implements Sampler { @Override public synchronized SamplingStatus sample(String operation, long id) { SamplingStatus probabilisticSamplingStatus = probabilisticSampler.sample(operation, id); SamplingStatus lowerBoundSamplingStatus = lowerBoundSampler.sample(operation, id); if (probabilisticSamplingStatus.isSampled()) { return probabilisticSamplingStatus; } return SamplingStatus.of(lowerBoundSamplingStatus.isSampled(), tags); } GuaranteedThroughputSampler(double samplingRate, double lowerBound); synchronized boolean update(double samplingRate, double lowerBound); @Override synchronized SamplingStatus sample(String operation, long id); @Override synchronized void close(); static final String TYPE; }
@Test public void testRateLimitingLowerBound() throws Exception { undertest = new GuaranteedThroughputSampler(0.0001, 1.0); SamplingStatus samplingStatus = undertest.sample("test", Long.MAX_VALUE); Assert.assertTrue(samplingStatus.isSampled()); Map<String, Object> tags = samplingStatus.getTags(); assertEquals(tags.get(Constants.SAMPLER_TYPE_TAG_KEY), GuaranteedThroughputSampler.TYPE); assertEquals(tags.get(Constants.SAMPLER_PARAM_TAG_KEY), 0.0001); }
@Test public void testProbabilityTagsOverrideRateLimitingTags() throws Exception { undertest = new GuaranteedThroughputSampler(0.999, 1.0); SamplingStatus samplingStatus = undertest.sample("test", 0L); Assert.assertTrue(samplingStatus.isSampled()); Map<String, Object> tags = samplingStatus.getTags(); assertEquals(tags.get(Constants.SAMPLER_TYPE_TAG_KEY), ProbabilisticSampler.TYPE); assertEquals(tags.get(Constants.SAMPLER_PARAM_TAG_KEY), 0.999); }