SofaTracerPercentageBasedSampler implements Sampler { @Override public SamplingStatus sample(SofaTracerSpan sofaTracerSpan) { SamplingStatus samplingStatus = new SamplingStatus(); Map<String, Object> tags = new HashMap<String, Object>(); tags.put(SofaTracerConstant.SAMPLER_TYPE_TAG_KEY, TYPE); tags.put(SofaTracerConstant.SAMPLER_PARAM_TAG_KEY, configuration.getPercentage()); tags = Collections.unmodifiableMap(tags); samplingStatus.setTags(tags); if (this.configuration.getPercentage() == 0) { samplingStatus.setSampled(false); return samplingStatus; } else if (this.configuration.getPercentage() == 100) { samplingStatus.setSampled(true); return samplingStatus; } boolean result = this.sampleDecisions.get((int) (this.counter.getAndIncrement() % 100)); samplingStatus.setSampled(result); return samplingStatus; } SofaTracerPercentageBasedSampler(SamplerProperties configuration); @Override SamplingStatus sample(SofaTracerSpan sofaTracerSpan); @Override String getType(); @Override void close(); static BitSet randomBitSet(int size, int cardinality, Random rnd); static final String TYPE; }
@Test public void sample() { samplerProperties.setPercentage(0); SamplingStatus sampleStatusFalse = sofaTracerPercentageBasedSampler.sample(sofaTracerSpan); Assert.assertTrue(!sampleStatusFalse.isSampled()); samplerProperties.setPercentage(100); SamplingStatus sampleStatusTrue = sofaTracerPercentageBasedSampler.sample(sofaTracerSpan); Assert.assertTrue(sampleStatusTrue.isSampled()); }