ProbabilisticSampler implements Sampler { @Override public SamplingStatus sample(String operation, long id) { if (id > 0) { return SamplingStatus.of(id <= this.positiveSamplingBoundary, tags); } else { return SamplingStatus.of(id >= this.negativeSamplingBoundary, tags); } } ProbabilisticSampler(double samplingRate); @Override SamplingStatus sample(String operation, long id); @Override boolean equals(Object other); @Override void close(); static final double DEFAULT_SAMPLING_PROBABILITY; static final String TYPE; }
@Test public void testSamplingBoundariesPositive() { double samplingRate = 0.5; long halfwayBoundary = 0x3fffffffffffffffL; Sampler sampler = new ProbabilisticSampler(samplingRate); assertTrue(sampler.sample("", halfwayBoundary).isSampled()); assertFalse(sampler.sample("", halfwayBoundary + 2).isSampled()); }
@Test public void testSamplingBoundariesNegative() { double samplingRate = 0.5; long halfwayBoundary = -0x4000000000000000L; Sampler sampler = new ProbabilisticSampler(samplingRate); assertTrue(sampler.sample("", halfwayBoundary).isSampled()); assertFalse(sampler.sample("", halfwayBoundary - 1).isSampled()); }
@Test public void testTags() { ProbabilisticSampler sampler = new ProbabilisticSampler(0.1); Map<String, Object> tags = sampler.sample("vadacurry", 20L).getTags(); assertEquals("probabilistic", tags.get("sampler.type")); assertEquals(0.1, tags.get("sampler.param")); }