@@ -61,6 +61,11 @@ public class SampledSpanStoreImplTest {
6161 TraceId .generateRandomId (random ),
6262 SpanId .generateRandomId (random ),
6363 TraceOptions .builder ().setIsSampled ().build ());
64+ private final SpanContext notSampledSpanContext =
65+ SpanContext .create (
66+ TraceId .generateRandomId (random ),
67+ SpanId .generateRandomId (random ),
68+ TraceOptions .DEFAULT );
6469 private final SpanId parentSpanId = SpanId .generateRandomId (random );
6570 private final EnumSet <Options > recordSpanOptions = EnumSet .of (Options .RECORD_EVENTS );
6671 private final TestClock testClock = TestClock .create (Timestamp .create (12345 , 54321 ));
@@ -83,7 +88,7 @@ public void setUp() {
8388 sampleStore .registerSpanNamesForCollection (Arrays .asList (REGISTERED_SPAN_NAME ));
8489 }
8590
86- SpanImpl createSpan (String spanName ) {
91+ SpanImpl createSampledSpan (String spanName ) {
8792 SpanImpl span =
8893 SpanImpl .startSpan (
8994 sampledSpanContext ,
@@ -98,9 +103,25 @@ SpanImpl createSpan(String spanName) {
98103 return span ;
99104 }
100105
106+ SpanImpl createNotSampledSpan (String spanName ) {
107+ SpanImpl span =
108+ SpanImpl .startSpan (
109+ notSampledSpanContext ,
110+ recordSpanOptions ,
111+ spanName ,
112+ parentSpanId ,
113+ false ,
114+ TraceParams .DEFAULT ,
115+ startEndHandler ,
116+ null ,
117+ testClock );
118+ return span ;
119+ }
120+
101121 private void addSpanNameToAllLatencyBuckets (String spanName ) {
102122 for (LatencyBucketBoundaries boundaries : LatencyBucketBoundaries .values ()) {
103- Span span = createSpan (spanName );
123+ Span sampledSpan = createSampledSpan (spanName );
124+ Span notSampledSpan = createNotSampledSpan (spanName );
104125 if (boundaries .getLatencyLowerNs () < NUM_NANOS_PER_SECOND ) {
105126 testClock .advanceTime (Duration .create (0 , (int ) boundaries .getLatencyLowerNs ()));
106127 } else {
@@ -109,16 +130,19 @@ private void addSpanNameToAllLatencyBuckets(String spanName) {
109130 boundaries .getLatencyLowerNs () / NUM_NANOS_PER_SECOND ,
110131 (int ) (boundaries .getLatencyLowerNs () % NUM_NANOS_PER_SECOND )));
111132 }
112- span .end ();
133+ sampledSpan .end ();
134+ notSampledSpan .end ();
113135 }
114136 }
115137
116138 private void addSpanNameToAllErrorBuckets (String spanName ) {
117139 for (CanonicalCode code : CanonicalCode .values ()) {
118140 if (code != CanonicalCode .OK ) {
119- Span span = createSpan (spanName );
141+ Span sampledSpan = createSampledSpan (spanName );
142+ Span notSampledSpan = createNotSampledSpan (spanName );
120143 testClock .advanceTime (Duration .create (0 , 1000 ));
121- span .end (EndSpanOptions .builder ().setStatus (code .toStatus ()).build ());
144+ sampledSpan .end (EndSpanOptions .builder ().setStatus (code .toStatus ()).build ());
145+ notSampledSpan .end (EndSpanOptions .builder ().setStatus (code .toStatus ()).build ());
122146 }
123147 }
124148 }
@@ -133,7 +157,7 @@ public void addSpansWithRegisteredNamesInAllLatencyBuckets() {
133157 perSpanNameSummary .get (REGISTERED_SPAN_NAME ).getNumbersOfLatencySampledSpans ();
134158 assertThat (latencyBucketsSummaries .size ()).isEqualTo (LatencyBucketBoundaries .values ().length );
135159 for (Map .Entry <LatencyBucketBoundaries , Integer > it : latencyBucketsSummaries .entrySet ()) {
136- assertThat (it .getValue ()).isEqualTo (1 );
160+ assertThat (it .getValue ()).isEqualTo (2 );
137161 }
138162 }
139163
@@ -173,7 +197,7 @@ public void addSpansWithRegisteredNamesInAllErrorBuckets() {
173197 perSpanNameSummary .get (REGISTERED_SPAN_NAME ).getNumbersOfErrorSampledSpans ();
174198 assertThat (errorBucketsSummaries .size ()).isEqualTo (CanonicalCode .values ().length - 1 );
175199 for (Map .Entry <CanonicalCode , Integer > it : errorBucketsSummaries .entrySet ()) {
176- assertThat (it .getValue ()).isEqualTo (1 );
200+ assertThat (it .getValue ()).isEqualTo (2 );
177201 }
178202 }
179203
@@ -188,7 +212,7 @@ public void addSpansWithoutRegisteredNamesInAllErrorBuckets() {
188212
189213 @ Test
190214 public void getErrorSampledSpans () {
191- SpanImpl span = createSpan (REGISTERED_SPAN_NAME );
215+ SpanImpl span = createSampledSpan (REGISTERED_SPAN_NAME );
192216 testClock .advanceTime (Duration .create (0 , 1000 ));
193217 span .end (EndSpanOptions .builder ().setStatus (Status .CANCELLED ).build ());
194218 Collection <SpanData > samples =
@@ -200,12 +224,12 @@ public void getErrorSampledSpans() {
200224
201225 @ Test
202226 public void getErrorSampledSpans_MaxSpansToReturn () {
203- SpanImpl span1 = createSpan (REGISTERED_SPAN_NAME );
227+ SpanImpl span1 = createSampledSpan (REGISTERED_SPAN_NAME );
204228 testClock .advanceTime (Duration .create (0 , 1000 ));
205229 span1 .end (EndSpanOptions .builder ().setStatus (Status .CANCELLED ).build ());
206230 // Advance time to allow other spans to be sampled.
207231 testClock .advanceTime (Duration .create (5 , 0 ));
208- SpanImpl span2 = createSpan (REGISTERED_SPAN_NAME );
232+ SpanImpl span2 = createSampledSpan (REGISTERED_SPAN_NAME );
209233 testClock .advanceTime (Duration .create (0 , 1000 ));
210234 span2 .end (EndSpanOptions .builder ().setStatus (Status .CANCELLED ).build ());
211235 Collection <SpanData > samples =
@@ -218,10 +242,10 @@ public void getErrorSampledSpans_MaxSpansToReturn() {
218242
219243 @ Test
220244 public void getErrorSampledSpans_NullCode () {
221- SpanImpl span1 = createSpan (REGISTERED_SPAN_NAME );
245+ SpanImpl span1 = createSampledSpan (REGISTERED_SPAN_NAME );
222246 testClock .advanceTime (Duration .create (0 , 1000 ));
223247 span1 .end (EndSpanOptions .builder ().setStatus (Status .CANCELLED ).build ());
224- SpanImpl span2 = createSpan (REGISTERED_SPAN_NAME );
248+ SpanImpl span2 = createSampledSpan (REGISTERED_SPAN_NAME );
225249 testClock .advanceTime (Duration .create (0 , 1000 ));
226250 span2 .end (EndSpanOptions .builder ().setStatus (Status .UNKNOWN ).build ());
227251 Collection <SpanData > samples =
@@ -232,10 +256,10 @@ public void getErrorSampledSpans_NullCode() {
232256
233257 @ Test
234258 public void getErrorSampledSpans_NullCode_MaxSpansToReturn () {
235- SpanImpl span1 = createSpan (REGISTERED_SPAN_NAME );
259+ SpanImpl span1 = createSampledSpan (REGISTERED_SPAN_NAME );
236260 testClock .advanceTime (Duration .create (0 , 1000 ));
237261 span1 .end (EndSpanOptions .builder ().setStatus (Status .CANCELLED ).build ());
238- SpanImpl span2 = createSpan (REGISTERED_SPAN_NAME );
262+ SpanImpl span2 = createSampledSpan (REGISTERED_SPAN_NAME );
239263 testClock .advanceTime (Duration .create (0 , 1000 ));
240264 span2 .end (EndSpanOptions .builder ().setStatus (Status .UNKNOWN ).build ());
241265 Collection <SpanData > samples =
@@ -246,7 +270,7 @@ public void getErrorSampledSpans_NullCode_MaxSpansToReturn() {
246270
247271 @ Test
248272 public void getLatencySampledSpans () {
249- SpanImpl span = createSpan (REGISTERED_SPAN_NAME );
273+ SpanImpl span = createSampledSpan (REGISTERED_SPAN_NAME );
250274 testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (20 )));
251275 span .end ();
252276 Collection <SpanData > samples =
@@ -262,7 +286,7 @@ public void getLatencySampledSpans() {
262286
263287 @ Test
264288 public void getLatencySampledSpans_ExclusiveUpperBound () {
265- SpanImpl span = createSpan (REGISTERED_SPAN_NAME );
289+ SpanImpl span = createSampledSpan (REGISTERED_SPAN_NAME );
266290 testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (20 )));
267291 span .end ();
268292 Collection <SpanData > samples =
@@ -277,7 +301,7 @@ public void getLatencySampledSpans_ExclusiveUpperBound() {
277301
278302 @ Test
279303 public void getLatencySampledSpans_InclusiveLowerBound () {
280- SpanImpl span = createSpan (REGISTERED_SPAN_NAME );
304+ SpanImpl span = createSampledSpan (REGISTERED_SPAN_NAME );
281305 testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (20 )));
282306 span .end ();
283307 Collection <SpanData > samples =
@@ -293,12 +317,12 @@ public void getLatencySampledSpans_InclusiveLowerBound() {
293317
294318 @ Test
295319 public void getLatencySampledSpans_QueryBetweenMultipleBuckets () {
296- SpanImpl span1 = createSpan (REGISTERED_SPAN_NAME );
320+ SpanImpl span1 = createSampledSpan (REGISTERED_SPAN_NAME );
297321 testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (20 )));
298322 span1 .end ();
299323 // Advance time to allow other spans to be sampled.
300324 testClock .advanceTime (Duration .create (5 , 0 ));
301- SpanImpl span2 = createSpan (REGISTERED_SPAN_NAME );
325+ SpanImpl span2 = createSampledSpan (REGISTERED_SPAN_NAME );
302326 testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (200 )));
303327 span2 .end ();
304328 Collection <SpanData > samples =
@@ -313,12 +337,12 @@ public void getLatencySampledSpans_QueryBetweenMultipleBuckets() {
313337
314338 @ Test
315339 public void getLatencySampledSpans_MaxSpansToReturn () {
316- SpanImpl span1 = createSpan (REGISTERED_SPAN_NAME );
340+ SpanImpl span1 = createSampledSpan (REGISTERED_SPAN_NAME );
317341 testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (20 )));
318342 span1 .end ();
319343 // Advance time to allow other spans to be sampled.
320344 testClock .advanceTime (Duration .create (5 , 0 ));
321- SpanImpl span2 = createSpan (REGISTERED_SPAN_NAME );
345+ SpanImpl span2 = createSampledSpan (REGISTERED_SPAN_NAME );
322346 testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (200 )));
323347 span2 .end ();
324348 Collection <SpanData > samples =
@@ -331,4 +355,20 @@ public void getLatencySampledSpans_MaxSpansToReturn() {
331355 assertThat (samples .size ()).isEqualTo (1 );
332356 assertThat (samples .contains (span1 .toSpanData ())).isTrue ();
333357 }
358+
359+ @ Test
360+ public void ignoreNegativeSpanLatency () {
361+ SpanImpl span = createSampledSpan (REGISTERED_SPAN_NAME );
362+ testClock .advanceTime (Duration .create (0 , (int ) TimeUnit .MICROSECONDS .toNanos (-20 )));
363+ span .end ();
364+ Collection <SpanData > samples =
365+ sampleStore .getLatencySampledSpans (
366+ LatencyFilter .create (
367+ REGISTERED_SPAN_NAME ,
368+ 0 ,
369+ Long .MAX_VALUE ,
370+ 0 ));
371+ assertThat (samples .size ()).isEqualTo (0 );
372+ }
373+
334374}
0 commit comments