1818
1919import static com .google .common .truth .Truth .assertThat ;
2020
21+ import io .opencensus .trace .NoopSpan ;
2122import io .opencensus .trace .Sampler ;
2223import io .opencensus .trace .Span ;
2324import io .opencensus .trace .SpanContext ;
2425import io .opencensus .trace .SpanId ;
2526import io .opencensus .trace .TraceId ;
2627import io .opencensus .trace .TraceOptions ;
28+ import java .util .Arrays ;
2729import java .util .Collections ;
30+ import java .util .EnumSet ;
31+ import java .util .List ;
2832import java .util .Random ;
2933import org .junit .Test ;
3034import org .junit .runner .RunWith ;
@@ -43,6 +47,8 @@ public class SamplersTest {
4347 SpanContext .create (traceId , parentSpanId , TraceOptions .builder ().setIsSampled (true ).build ());
4448 private final SpanContext notSampledSpanContext =
4549 SpanContext .create (traceId , parentSpanId , TraceOptions .DEFAULT );
50+ private final Span sampledSpan =
51+ new NoopSpan (sampledSpanContext , EnumSet .of (Span .Options .RECORD_EVENTS ));
4652
4753 @ Test
4854 public void alwaysSampleSampler_AlwaysReturnTrue () {
@@ -118,7 +124,7 @@ public void probabilitySampler_outOfRangeLowProbability() {
118124
119125 // Applies the given sampler to NUM_SAMPLE_TRIES random traceId/spanId pairs.
120126 private static void assertSamplerSamplesWithProbability (
121- Sampler sampler , SpanContext parent , double probability ) {
127+ Sampler sampler , SpanContext parent , List < Span > parentLinks , double probability ) {
122128 Random random = new Random (1234 );
123129 int count = 0 ; // Count of spans with sampling enabled
124130 for (int i = 0 ; i < NUM_SAMPLE_TRIES ; i ++) {
@@ -128,7 +134,7 @@ private static void assertSamplerSamplesWithProbability(
128134 TraceId .generateRandomId (random ),
129135 SpanId .generateRandomId (random ),
130136 SPAN_NAME ,
131- Collections .< Span > emptyList () )) {
137+ parentLinks )) {
132138 count ++;
133139 }
134140 }
@@ -139,17 +145,133 @@ private static void assertSamplerSamplesWithProbability(
139145 }
140146
141147 @ Test
142- public void probabilitySampler_differentProbabilities () {
148+ public void probabilitySampler_DifferentProbabilities_NotSampledParent () {
143149 final Sampler neverSample = Samplers .probabilitySampler (0.0 );
144- assertSamplerSamplesWithProbability (neverSample , sampledSpanContext , 0.0 );
150+ assertSamplerSamplesWithProbability (
151+ neverSample , notSampledSpanContext , Collections .<Span >emptyList (), 0.0 );
145152 final Sampler alwaysSample = Samplers .probabilitySampler (1.0 );
146- assertSamplerSamplesWithProbability (alwaysSample , sampledSpanContext , 1.0 );
153+ assertSamplerSamplesWithProbability (
154+ alwaysSample , notSampledSpanContext , Collections .<Span >emptyList (), 1.0 );
147155 final Sampler fiftyPercentSample = Samplers .probabilitySampler (0.5 );
148- assertSamplerSamplesWithProbability (fiftyPercentSample , sampledSpanContext , 0.5 );
156+ assertSamplerSamplesWithProbability (
157+ fiftyPercentSample , notSampledSpanContext , Collections .<Span >emptyList (), 0.5 );
149158 final Sampler twentyPercentSample = Samplers .probabilitySampler (0.2 );
150- assertSamplerSamplesWithProbability (twentyPercentSample , sampledSpanContext , 0.2 );
159+ assertSamplerSamplesWithProbability (
160+ twentyPercentSample , notSampledSpanContext , Collections .<Span >emptyList (), 0.2 );
151161 final Sampler twoThirdsSample = Samplers .probabilitySampler (2.0 / 3.0 );
152- assertSamplerSamplesWithProbability (twoThirdsSample , sampledSpanContext , 2.0 / 3.0 );
162+ assertSamplerSamplesWithProbability (
163+ twoThirdsSample , notSampledSpanContext , Collections .<Span >emptyList (), 2.0 / 3.0 );
164+ }
165+
166+ @ Test
167+ public void probabilitySampler_DifferentProbabilities_SampledParent () {
168+ final Sampler neverSample = Samplers .probabilitySampler (0.0 );
169+ assertSamplerSamplesWithProbability (
170+ neverSample , sampledSpanContext , Collections .<Span >emptyList (), 1.0 );
171+ final Sampler alwaysSample = Samplers .probabilitySampler (1.0 );
172+ assertSamplerSamplesWithProbability (
173+ alwaysSample , sampledSpanContext , Collections .<Span >emptyList (), 1.0 );
174+ final Sampler fiftyPercentSample = Samplers .probabilitySampler (0.5 );
175+ assertSamplerSamplesWithProbability (
176+ fiftyPercentSample , sampledSpanContext , Collections .<Span >emptyList (), 1.0 );
177+ final Sampler twentyPercentSample = Samplers .probabilitySampler (0.2 );
178+ assertSamplerSamplesWithProbability (
179+ twentyPercentSample , sampledSpanContext , Collections .<Span >emptyList (), 1.0 );
180+ final Sampler twoThirdsSample = Samplers .probabilitySampler (2.0 / 3.0 );
181+ assertSamplerSamplesWithProbability (
182+ twoThirdsSample , sampledSpanContext , Collections .<Span >emptyList (), 1.0 );
183+ }
184+
185+ @ Test
186+ public void probabilitySampler_DifferentProbabilities_SampledParentLink () {
187+ final Sampler neverSample = Samplers .probabilitySampler (0.0 );
188+ assertSamplerSamplesWithProbability (
189+ neverSample , notSampledSpanContext , Arrays .asList (sampledSpan ), 1.0 );
190+ final Sampler alwaysSample = Samplers .probabilitySampler (1.0 );
191+ assertSamplerSamplesWithProbability (
192+ alwaysSample , notSampledSpanContext , Arrays .asList (sampledSpan ), 1.0 );
193+ final Sampler fiftyPercentSample = Samplers .probabilitySampler (0.5 );
194+ assertSamplerSamplesWithProbability (
195+ fiftyPercentSample , notSampledSpanContext , Arrays .asList (sampledSpan ), 1.0 );
196+ final Sampler twentyPercentSample = Samplers .probabilitySampler (0.2 );
197+ assertSamplerSamplesWithProbability (
198+ twentyPercentSample , notSampledSpanContext , Arrays .asList (sampledSpan ), 1.0 );
199+ final Sampler twoThirdsSample = Samplers .probabilitySampler (2.0 / 3.0 );
200+ assertSamplerSamplesWithProbability (
201+ twoThirdsSample , notSampledSpanContext , Arrays .asList (sampledSpan ), 1.0 );
202+ }
203+
204+ @ Test
205+ public void probabilitySampler_SampleBasedOnTraceId () {
206+ final Sampler defaultProbability = Samplers .probabilitySampler (0.0001 );
207+ // This traceId will not be sampled by the ProbabilitySampler because the first 8 bytes as long
208+ // is not less than probability * Long.MAX_VALUE;
209+ TraceId notSampledtraceId =
210+ TraceId .fromBytes (
211+ new byte [] {
212+ (byte ) 0x8F ,
213+ (byte ) 0xFF ,
214+ (byte ) 0xFF ,
215+ (byte ) 0xFF ,
216+ (byte ) 0xFF ,
217+ (byte ) 0xFF ,
218+ (byte ) 0xFF ,
219+ (byte ) 0xFF ,
220+ 0 ,
221+ 0 ,
222+ 0 ,
223+ 0 ,
224+ 0 ,
225+ 0 ,
226+ 0 ,
227+ 0
228+ });
229+ assertThat (
230+ defaultProbability .shouldSample (
231+ null ,
232+ false ,
233+ notSampledtraceId ,
234+ SpanId .generateRandomId (random ),
235+ SPAN_NAME ,
236+ Collections .<Span >emptyList ()))
237+ .isFalse ();
238+ // This traceId will be sampled by the ProbabilitySampler because the first 8 bytes as long
239+ // is less than probability * Long.MAX_VALUE;
240+ TraceId sampledtraceId =
241+ TraceId .fromBytes (
242+ new byte [] {
243+ (byte ) 0x00 ,
244+ (byte ) 0x00 ,
245+ (byte ) 0xFF ,
246+ (byte ) 0xFF ,
247+ (byte ) 0xFF ,
248+ (byte ) 0xFF ,
249+ (byte ) 0xFF ,
250+ (byte ) 0xFF ,
251+ 0 ,
252+ 0 ,
253+ 0 ,
254+ 0 ,
255+ 0 ,
256+ 0 ,
257+ 0 ,
258+ 0
259+ });
260+ assertThat (
261+ defaultProbability .shouldSample (
262+ null ,
263+ false ,
264+ sampledtraceId ,
265+ SpanId .generateRandomId (random ),
266+ SPAN_NAME ,
267+ Collections .<Span >emptyList ()))
268+ .isTrue ();
269+ }
270+
271+ @ Test
272+ public void probabilitySampler_getDescription () {
273+ assertThat ((Samplers .probabilitySampler (0.5 )).getDescription ())
274+ .isEqualTo ("ProbabilitySampler{0.500000}" );
153275 }
154276
155277 @ Test
0 commit comments