Skip to content

Commit

Permalink
no need to pass around testing
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Aug 26, 2024
1 parent 0694534 commit 6f5e60b
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
Expand All @@ -33,12 +32,6 @@

public abstract class AbstractServlet3Test<SERVER, CONTEXT> extends AbstractHttpServerTest<SERVER> {

protected final InstrumentationExtension testing;

public AbstractServlet3Test(InstrumentationExtension testing) {
this.testing = testing;
}

public static final ServerEndpoint HTML_PRINT_WRITER =
new ServerEndpoint(
"HTML_PRINT_WRITER",
Expand Down Expand Up @@ -171,17 +164,18 @@ void snippetInjectionWithServletOutputStream() {
ExperimentalSnippetHolder.setSnippet("");

String expectedRoute = expectedHttpRoute(HTML_SERVLET_OUTPUT_STREAM, "GET");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET" + (expectedRoute != null ? " " + expectedRoute : ""))
.hasKind(SpanKind.SERVER)
.hasNoParent(),
span ->
span.hasName("controller")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))));
testing()
.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET" + (expectedRoute != null ? " " + expectedRoute : ""))
.hasKind(SpanKind.SERVER)
.hasNoParent(),
span ->
span.hasName("controller")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))));
}

@Test
Expand Down Expand Up @@ -210,16 +204,17 @@ void snippetInjectionWithPrintWriter() {
ExperimentalSnippetHolder.setSnippet("");

String expectedRoute = expectedHttpRoute(HTML_PRINT_WRITER, "GET");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET" + (expectedRoute != null ? " " + expectedRoute : ""))
.hasKind(SpanKind.SERVER)
.hasNoParent(),
span ->
span.hasName("controller")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))));
testing()
.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET" + (expectedRoute != null ? " " + expectedRoute : ""))
.hasKind(SpanKind.SERVER)
.hasNoParent(),
span ->
span.hasName("controller")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ public abstract class JettyServlet3Test
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public JettyServlet3Test() {
super(testing);
}

static final boolean IS_BEFORE_94 = isBefore94();

public static boolean isBefore94() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public class JettyServletHandlerTest extends AbstractServlet3Test<Server, Servle
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public JettyServletHandlerTest() {
super(testing);
}

@Override
protected void configure(HttpServerTestOptions options) {
super.configure(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class TomcatServlet3AsyncTest extends TomcatServlet3Test {
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public TomcatServlet3AsyncTest() {
super(testing);
}

@Override
public Class<? extends Servlet> servlet() {
return TestServlet3.Async.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class TomcatServlet3FakeAsyncTest extends TomcatServlet3Test {
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public TomcatServlet3FakeAsyncTest() {
super(testing);
}

@Override
public Class<? extends Servlet> servlet() {
return TestServlet3.FakeAsync.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class TomcatServlet3SyncTest extends TomcatServlet3Test {
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public TomcatServlet3SyncTest() {
super(testing);
}

@Override
public Class<? extends Servlet> servlet() {
return TestServlet3.Sync.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import io.opentelemetry.instrumentation.testing.util.TelemetryDataUtil;
Expand Down Expand Up @@ -61,10 +60,6 @@ public abstract class TomcatServlet3Test extends AbstractServlet3Test<Tomcat, Co
false);
private final TestAccessLogValve accessLogValue = new TestAccessLogValve();

public TomcatServlet3Test(InstrumentationExtension testing) {
super(testing);
}

@Override
protected void configure(HttpServerTestOptions options) {
super.configure(options);
Expand Down Expand Up @@ -130,7 +125,7 @@ protected Tomcat setupServer() throws Exception {
@BeforeEach
void setUp() {
accessLogValue.getLoggedIds().clear();
testing.clearData();
testing().clearAllExportedData();
}

@Override
Expand Down Expand Up @@ -183,10 +178,11 @@ void access_log_has_ids_for__count_requests(int count) {
trace.hasSpansSatisfyingExactly(
span -> assertServerSpan(span, "GET", ACCESS_LOG_SUCCESS, SUCCESS.getStatus()),
span -> assertControllerSpan(span, null));
testing.waitAndAssertTraces(
IntStream.range(0, count).mapToObj(i -> check).collect(Collectors.toList()));
testing()
.waitAndAssertTraces(
IntStream.range(0, count).mapToObj(i -> check).collect(Collectors.toList()));

List<List<SpanData>> traces = TelemetryDataUtil.groupTraces(testing.spans());
List<List<SpanData>> traces = TelemetryDataUtil.groupTraces(testing().getExportedSpans());

for (int i = 0; i < count; i++) {
assertThat(loggedTraces).contains(traces.get(i).get(0).getTraceId());
Expand All @@ -209,7 +205,7 @@ void accessLogHasIdsForErrorRequest() {
spanCount++;
}

List<SpanData> spanData = TelemetryDataUtil.groupTraces(testing.spans()).get(0);
List<SpanData> spanData = TelemetryDataUtil.groupTraces(testing().getExportedSpans()).get(0);
List<SpanDataAssert> spans =
spanData.stream().map(OpenTelemetryAssertions::assertThat).collect(Collectors.toList());
assertThat(spans).hasSize(spanCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@

package io.opentelemetry.javaagent.instrumentation.servlet.v3_0.tomcat.dispatch;

import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.tomcat.TomcatServlet3Test;

abstract class TomcatDispatchTest extends TomcatServlet3Test {

public TomcatDispatchTest(InstrumentationExtension testing) {
super(testing);
}

@Override
protected void configure(HttpServerTestOptions options) {
super.configure(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class TomcatServlet3DispatchAsyncTest extends TomcatDispatchTest {
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public TomcatServlet3DispatchAsyncTest() {
super(testing);
}

@Override
protected void configure(HttpServerTestOptions options) {
super.configure(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class TomcatServlet3DispatchImmediateTest extends TomcatDispatchTest {
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public TomcatServlet3DispatchImmediateTest() {
super(testing);
}

@Override
public Class<? extends Servlet> servlet() {
return TestServlet3.Sync.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class TomcatServlet3ForwardTest extends TomcatDispatchTest {
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public TomcatServlet3ForwardTest() {
super(testing);
}

@Override
public Class<? extends Servlet> servlet() {
return TestServlet3.Sync.class; // dispatch to sync servlet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class TomcatServlet3IncludeTest extends TomcatDispatchTest {
protected static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

public TomcatServlet3IncludeTest() {
super(testing);
}

@Override
public Class<? extends Servlet> servlet() {
return TestServlet3.Sync.class; // dispatch to sync servlet
Expand Down

0 comments on commit 6f5e60b

Please sign in to comment.