|
58 | 58 | import io.opentelemetry.sdk.trace.data.SpanData; |
59 | 59 | import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
60 | 60 | import java.util.List; |
61 | | -import java.util.Optional; |
62 | 61 | import org.junit.jupiter.api.AfterEach; |
63 | 62 | import org.junit.jupiter.api.BeforeEach; |
64 | 63 | import org.junit.jupiter.api.Test; |
@@ -254,21 +253,27 @@ void testTracing_retry_grpc() throws Exception { |
254 | 253 | assertThat(spans).hasSize(attempts); // Expect exactly one span for the successful retry |
255 | 254 |
|
256 | 255 | // This single span represents the successful retry, which has resend_count=1 |
257 | | - for (int resendCount = 1; resendCount < attempts; resendCount++) { |
258 | | - Optional<SpanData> found = |
259 | | - spans.stream() |
260 | | - .filter( |
261 | | - span -> |
262 | | - span.getAttributes() |
263 | | - .asMap() |
264 | | - .getOrDefault( |
265 | | - AttributeKey.longKey( |
266 | | - ObservabilityAttributes.GRPC_RESEND_COUNT_ATTRIBUTE), |
267 | | - -1L) |
268 | | - .equals(1L)) |
269 | | - .findFirst(); |
270 | | - assertThat(found).isPresent(); |
271 | | - } |
| 256 | + // The first attempt has no resend_count. The subsequent retries will have a resend_count, |
| 257 | + // starting from 1. |
| 258 | + List<Long> resendCounts = |
| 259 | + spans.stream() |
| 260 | + .map( |
| 261 | + span -> |
| 262 | + (Long) |
| 263 | + span.getAttributes() |
| 264 | + .asMap() |
| 265 | + .get( |
| 266 | + AttributeKey.longKey( |
| 267 | + ObservabilityAttributes.GRPC_RESEND_COUNT_ATTRIBUTE))) |
| 268 | + .filter(java.util.Objects::nonNull) |
| 269 | + .sorted() |
| 270 | + .collect(java.util.stream.Collectors.toList()); |
| 271 | + |
| 272 | + List<Long> expectedCounts = |
| 273 | + java.util.stream.LongStream.range(1, attempts) |
| 274 | + .boxed() |
| 275 | + .collect(java.util.stream.Collectors.toList()); |
| 276 | + assertThat(resendCounts).containsExactlyElementsIn(expectedCounts).inOrder(); |
272 | 277 | } |
273 | 278 |
|
274 | 279 | @Test |
@@ -323,20 +328,26 @@ void testTracing_retry_httpjson() throws Exception { |
323 | 328 | assertThat(spans).hasSize(attempts); // Expect exactly one span for the successful retry |
324 | 329 |
|
325 | 330 | // This single span represents the successful retry, which has resend_count=1 |
326 | | - for (int resendCount = 1; resendCount < attempts; resendCount++) { |
327 | | - Optional<SpanData> found = |
328 | | - spans.stream() |
329 | | - .filter( |
330 | | - span -> |
331 | | - span.getAttributes() |
332 | | - .asMap() |
333 | | - .getOrDefault( |
334 | | - AttributeKey.longKey( |
335 | | - ObservabilityAttributes.HTTP_RESEND_COUNT_ATTRIBUTE), |
336 | | - -1L) |
337 | | - .equals(1L)) |
338 | | - .findFirst(); |
339 | | - assertThat(found).isPresent(); |
340 | | - } |
| 331 | + // The first attempt has no resend_count. The subsequent retries will have a resend_count, |
| 332 | + // starting from 1. |
| 333 | + List<Long> resendCounts = |
| 334 | + spans.stream() |
| 335 | + .map( |
| 336 | + span -> |
| 337 | + (Long) |
| 338 | + span.getAttributes() |
| 339 | + .asMap() |
| 340 | + .get( |
| 341 | + AttributeKey.longKey( |
| 342 | + ObservabilityAttributes.HTTP_RESEND_COUNT_ATTRIBUTE))) |
| 343 | + .filter(java.util.Objects::nonNull) |
| 344 | + .sorted() |
| 345 | + .collect(java.util.stream.Collectors.toList()); |
| 346 | + |
| 347 | + List<Long> expectedCounts = |
| 348 | + java.util.stream.LongStream.range(1, attempts) |
| 349 | + .boxed() |
| 350 | + .collect(java.util.stream.Collectors.toList()); |
| 351 | + assertThat(resendCounts).containsExactlyElementsIn(expectedCounts).inOrder(); |
341 | 352 | } |
342 | 353 | } |
0 commit comments