Skip to content

Commit

Permalink
add app cleanup, assert events, rewrite test to use waitAndAssertTraces
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydeluca committed Jan 7, 2025
1 parent 0490ecd commit af41bc7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import static io.opentelemetry.semconv.UrlAttributes.URL_SCHEME;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -37,6 +39,11 @@ static void setup() throws Exception {
app = new RatpackFunctionalTest(RatpackApp.class);
}

@AfterAll
static void cleanup() {
app.close();
}

@Test
void testAddSpanOnHandlers() {
assertThat(app.getHttpClient().get("foo").getBody().getText()).isEqualTo("hi-foo");
Expand Down Expand Up @@ -96,8 +103,18 @@ void testPropagateTraceToHttpCalls() {

@Test
void testIgnoreHandlersBeforeOpenTelemetryServerHandler() {
assertThat(app.getHttpClient().get("ignore").getBody().getText()).isEqualTo("ignored");
assertThat(testing.spans().stream().filter(span -> "GET /ignore".equals(span.getName())))
.isEmpty();
testing.runWithSpan(
"parent",
() ->
assertThat(app.getHttpClient().get("ignore").getBody().getText()).isEqualTo("ignored"));

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("parent")
.hasNoParent()
.hasKind(SpanKind.INTERNAL)
.hasAttributes(Attributes.empty())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ void testPropagateTraceWithInstrumentedAsyncOperations() throws Exception {
span.hasName("a-span")
.hasParent(trace.getSpan(0))
.hasAttributes(Attributes.empty())
.hasTotalRecordedEvents(1)));
.hasEventsSatisfyingExactly(
event -> event.hasName("an-event").hasAttributes(Attributes.empty()))));
}

@Test
Expand Down Expand Up @@ -194,7 +195,8 @@ void testPropagateTraceWithInstrumentedAsyncConcurrentOperations() throws Except
span.hasName("a-span")
.hasParent(trace.getSpan(0))
.hasAttributes(Attributes.empty())
.hasTotalRecordedEvents(1)),
.hasEventsSatisfyingExactly(
event -> event.hasName("an-event").hasAttributes(Attributes.empty()))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
Expand All @@ -215,6 +217,7 @@ void testPropagateTraceWithInstrumentedAsyncConcurrentOperations() throws Except
span.hasName("another-span")
.hasParent(trace.getSpan(0))
.hasAttributes(Attributes.empty())
.hasTotalRecordedEvents(1)));
.hasEventsSatisfyingExactly(
event -> event.hasName("an-event").hasAttributes(Attributes.empty()))));
}
}

0 comments on commit af41bc7

Please sign in to comment.