Skip to content

Commit

Permalink
more stable trace ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed May 18, 2023
1 parent f967945 commit 0806a6a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void sendRequestWithCallback(

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
// optionsBuilder.disableTestRedirects();
// optionsBuilder.disableTestRedirects();
optionsBuilder.markAsLowLevelInstrumentation();
optionsBuilder.setMaxRedirects(52);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public final void waitAndAssertSortedTraces(
waitAndAssertTraces(traceComparator, Arrays.asList(assertions), true);
}

public final void waitAndAssertSortedTraces(
Comparator<List<SpanData>> traceComparator,
Iterable<? extends Consumer<TraceAssert>> assertions) {
waitAndAssertTraces(traceComparator, assertions, true);
}

@SafeVarargs
@SuppressWarnings("varargs")
public final void waitAndAssertTracesWithoutScopeVersionVerification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ public final void waitAndAssertSortedTraces(
testRunner.waitAndAssertSortedTraces(traceComparator, assertions);
}

public final void waitAndAssertSortedTraces(
Comparator<List<SpanData>> traceComparator,
Iterable<? extends Consumer<TraceAssert>> assertions) {
testRunner.waitAndAssertSortedTraces(traceComparator, assertions);
}

@SafeVarargs
@SuppressWarnings("varargs")
public final void waitAndAssertTracesWithoutScopeVersionVerification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.testing.junit.http;

import static io.opentelemetry.instrumentation.testing.util.TelemetryDataUtil.comparingRootSpanAttribute;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP;
Expand Down Expand Up @@ -255,7 +256,8 @@ void basicRequestWith1Redirect() throws Exception {
assertThat(responseCode).isEqualTo(200);

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertTraces(
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
Expand Down Expand Up @@ -294,7 +296,8 @@ void basicRequestWith2Redirects() throws Exception {
assertThat(responseCode).isEqualTo(200);

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertTraces(
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
Expand Down Expand Up @@ -352,7 +355,8 @@ void circularRedirects() {
Throwable clientError = options.getClientSpanErrorMapper().apply(uri, ex);

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertTraces(
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
IntStream.range(0, options.getMaxRedirects())
.mapToObj(i -> makeCircularRedirectAssertForLolLevelTrace(uri, method, i))
.collect(Collectors.toList()));
Expand Down Expand Up @@ -398,7 +402,8 @@ void redirectToSecuredCopiesAuthHeader() throws Exception {
assertThat(responseCode).isEqualTo(200);

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertTraces(
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.trace.data.SpanData;
Expand All @@ -34,6 +35,13 @@ public static Comparator<List<SpanData>> orderByRootSpanName(String... names) {
return Comparator.comparing(span -> list.indexOf(span.get(0).getName()));
}

public static <T extends Comparable<T>> Comparator<List<SpanData>> comparingRootSpanAttribute(
AttributeKey<T> key) {
return Comparator.comparing(
span -> span.get(0).getAttributes().get(key),
Comparator.nullsFirst(Comparator.naturalOrder()));
}

public static List<List<SpanData>> groupTraces(List<SpanData> spans) {
List<List<SpanData>> traces =
new ArrayList<>(
Expand Down

0 comments on commit 0806a6a

Please sign in to comment.