use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class CentralizedRule method sample.
@Override
public SamplingResponse sample(Instant now) {
SamplingResponse res = new SamplingResponse(name);
double rn = rand.next();
lock.writeLock().lock();
try {
return doSample(now, res, rn);
} finally {
lock.writeLock().unlock();
}
}
use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class CentralizedRuleTest method testPositiveBernoulliSample.
@Test
public void testPositiveBernoulliSample() {
Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
SamplingRule input = createInput("r1", 300, 10, 0.0);
CentralizedRule rule = new CentralizedRule(input, rand);
Mockito.when(rand.next()).thenReturn(0.01);
SamplingTargetDocument target = createTarget(0, 0.05, 1500000010);
rule.update(target, clock.instant());
// Sample using bernoulli sampling
SamplingResponse response = rule.sample(clock.instant());
Assert.assertTrue(response.isSampled());
Assert.assertEquals("r1", response.getRuleName().get());
Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
Assert.assertEquals(1, s.getSampled());
Assert.assertEquals(1, s.getRequests());
Assert.assertEquals(0, s.getBorrowed());
}
use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class CentralizedRuleTest method testExpiredReservoirNegativeBernoulliSample.
@Test
public void testExpiredReservoirNegativeBernoulliSample() {
Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
SamplingRule input = createInput("r1", 300, 0, 0.2);
CentralizedRule rule = new CentralizedRule(input, rand);
SamplingTargetDocument target = createTarget(0, 0.2, 1499999999);
rule.update(target, clock.instant());
Mockito.when(rand.next()).thenReturn(0.4);
SamplingResponse response = rule.sample(clock.instant());
Assert.assertFalse(response.isSampled());
Assert.assertEquals("r1", response.getRuleName().get());
Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
Assert.assertEquals(0, s.getSampled());
Assert.assertEquals(1, s.getRequests());
Assert.assertEquals(0, s.getBorrowed());
}
use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class CentralizedRuleTest method testPositiveSampleTake.
@Test
public void testPositiveSampleTake() {
Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
SamplingRule input = createInput("r1", 300, 10, 0.0);
CentralizedRule rule = new CentralizedRule(input, new RandImpl());
SamplingTargetDocument target = createTarget(2, 0.0, 1500000010);
rule.update(target, clock.instant());
SamplingResponse response = rule.sample(clock.instant());
Assert.assertTrue(response.isSampled());
Assert.assertEquals("r1", response.getRuleName().get());
Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
Assert.assertEquals(1, s.getSampled());
Assert.assertEquals(1, s.getRequests());
Assert.assertEquals(0, s.getBorrowed());
}
use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class AWSXRayRecorderTest method testBeginSegmentWithSamplingDoesSample.
@Test
public void testBeginSegmentWithSamplingDoesSample() {
SamplingResponse response = new SamplingResponse(true, "rule");
when(mockSamplingStrategy.shouldTrace(any())).thenReturn(response);
AWSXRay.getGlobalRecorder().setSamplingStrategy(mockSamplingStrategy);
Segment segment = AWSXRay.beginSegmentWithSampling("test");
assertThat(segment.isSampled()).isTrue();
assertThat(segment.getAws().get("xray")).isInstanceOfSatisfying(Map.class, xray -> assertThat(xray.get("rule_name")).isEqualTo("rule"));
}
Aggregations