ProbabilisticSamplingPreExecutionInterceptor extends PreExecutionSpanInterceptor { @Override public void interceptReport(PreExecutionInterceptorContext context) { final SpanContextInformation spanContext = context.getSpanContext(); final BitSet sampleDecisions; final String operationType = spanContext.getOperationType(); if (sampleDecisionsByType.containsKey(operationType)) { sampleDecisions = sampleDecisionsByType.get(operationType); } else if (isRoot(context.getSpanContext().getSpanWrapper())) { sampleDecisions = defaultSampleDecisions; } else { return; } if (sampleDecisions != null && !isSampled(sampleDecisions, spanCounter)) { context.shouldNotReport(getClass()); } }  @Override void init(ConfigurationRegistry configuration); @Override void interceptReport(PreExecutionInterceptorContext context);  }
@Test public void testNeverReportSpan() throws Exception { tracingPlugin.getDefaultRateLimitSpansPercentOption().update(0d, SimpleSource.NAME); interceptor.interceptReport(context); assertFalse(context.isReport()); interceptor.interceptReport(context); assertFalse(context.isReport()); }
@Test public void testAlwaysReportSpan() throws Exception { tracingPlugin.getDefaultRateLimitSpansPercentOption().update(1.0, SimpleSource.NAME); interceptor.interceptReport(context); assertTrue(context.isReport()); }
@Test public void testSample51Percent() throws Exception { tracingPlugin.getDefaultRateLimitSpansPercentOption().update(0.51, SimpleSource.NAME); int reports = 0; for (int i = 0; i < 100; i++) { final PreExecutionInterceptorContext context = new PreExecutionInterceptorContext(spanContext); interceptor.interceptReport(context); if (context.isReport()) { reports++; } } assertThat(reports).isEqualTo(51); }
@Test public void testReportSpanGenericType() throws Exception { when(spanContext.getOperationType()).thenReturn("jdbc"); tracingPlugin.getDefaultRateLimitSpansPercentOption().update(0d, SimpleSource.NAME); tracingPlugin.getRateLimitSpansPerMinutePercentPerTypeOption().update(singletonMap("http", 1d), SimpleSource.NAME); interceptor.interceptReport(context); assertFalse(context.isReport()); }