-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sort spans by start time (parents before children as tiebreaker) to a…
…void common causes for flaky tests
- Loading branch information
1 parent
b091536
commit 7e461b0
Showing
6 changed files
with
136 additions
and
17 deletions.
There are no files selected for viewing
6 changes: 4 additions & 2 deletions
6
docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
Comparing source compatibility of against | ||
No changes. | ||
Comparing source compatibility of against | ||
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.TracesAssert (not serializable) | ||
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0 | ||
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.testing.assertj.TracesAssert assertThat(java.util.List) |
31 changes: 31 additions & 0 deletions
31
sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TraceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.testing.assertj; | ||
|
||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import java.util.Comparator; | ||
|
||
public class TraceUtil { | ||
|
||
/** Compare spans by start time, placing parents before their children as a tiebreaker. */ | ||
static final Comparator<SpanData> SPAN_DATA_COMPARATOR = | ||
Comparator.comparing(SpanData::getStartEpochNanos) | ||
.thenComparing( | ||
(span1, span2) -> { | ||
SpanContext parent1 = span1.getParentSpanContext(); | ||
if (parent1.isValid() && parent1.getSpanId().equals(span2.getSpanId())) { | ||
return 1; | ||
} | ||
SpanContext parent2 = span2.getParentSpanContext(); | ||
if (parent2.isValid() && parent2.getSpanId().equals(span1.getSpanId())) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
|
||
private TraceUtil() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.testing.assertj; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.api.trace.SpanId; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.api.trace.TraceFlags; | ||
import io.opentelemetry.api.trace.TraceId; | ||
import io.opentelemetry.api.trace.TraceState; | ||
import io.opentelemetry.sdk.testing.trace.TestSpanData; | ||
import io.opentelemetry.sdk.trace.data.StatusData; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TraceUtilTest { | ||
@Test | ||
void spanDataComparator() { | ||
TestSpanData before = createBasicSpanBuilder().setStartEpochNanos(9).build(); | ||
|
||
String traceId = TraceId.fromLongs(1, 2); | ||
TestSpanData parent = | ||
createBasicSpanBuilder() | ||
.setName("parent") | ||
.setSpanContext( | ||
SpanContext.create( | ||
traceId, SpanId.fromLong(1), TraceFlags.getDefault(), TraceState.getDefault())) | ||
.setStartEpochNanos(10) | ||
.build(); | ||
TestSpanData child = | ||
createBasicSpanBuilder() | ||
.setName("child") | ||
.setSpanContext( | ||
SpanContext.create( | ||
traceId, SpanId.fromLong(2), TraceFlags.getDefault(), TraceState.getDefault())) | ||
.setStartEpochNanos(10) | ||
.setParentSpanContext(parent.getSpanContext()) | ||
.build(); | ||
|
||
TestSpanData sameTime1 = | ||
createBasicSpanBuilder().setName("sameTime1").setStartEpochNanos(11).build(); | ||
TestSpanData sameTime2 = | ||
createBasicSpanBuilder().setName("sameTime2").setStartEpochNanos(11).build(); | ||
|
||
assertSort(Arrays.asList(child, parent, before), before, parent, child); | ||
assertSort(Arrays.asList(parent, child, before), before, parent, child); | ||
|
||
assertSort(Arrays.asList(sameTime1, sameTime2, before), before, sameTime1, sameTime2); | ||
assertSort(Arrays.asList(sameTime2, sameTime1, before), before, sameTime2, sameTime1); | ||
} | ||
|
||
private static void assertSort(List<TestSpanData> spanData, TestSpanData... expected) { | ||
ArrayList<TestSpanData> list = new ArrayList<>(spanData); | ||
list.sort(TraceUtil.SPAN_DATA_COMPARATOR); | ||
assertThat(list).containsExactly(expected); | ||
} | ||
|
||
private static TestSpanData.Builder createBasicSpanBuilder() { | ||
return TestSpanData.builder() | ||
.setHasEnded(true) | ||
.setName("spanName") | ||
.setEndEpochNanos(100) | ||
.setKind(SpanKind.SERVER) | ||
.setStatus(StatusData.ok()) | ||
.setTotalRecordedEvents(0) | ||
.setTotalRecordedLinks(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters