|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.activejhttp; |
| 7 | + |
| 8 | +import static io.activej.http.HttpMethod.GET; |
| 9 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS; |
| 10 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR; |
| 11 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION; |
| 12 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.NOT_FOUND; |
| 13 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM; |
| 14 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; |
| 15 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS; |
| 16 | +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; |
| 17 | + |
| 18 | +import io.activej.eventloop.Eventloop; |
| 19 | +import io.activej.http.AsyncServlet; |
| 20 | +import io.activej.http.HttpHeaderValue; |
| 21 | +import io.activej.http.HttpHeaders; |
| 22 | +import io.activej.http.HttpResponse; |
| 23 | +import io.activej.http.HttpServer; |
| 24 | +import io.activej.http.RoutingServlet; |
| 25 | +import io.activej.promise.Promise; |
| 26 | +import io.activej.reactor.Reactor; |
| 27 | +import io.opentelemetry.instrumentation.api.internal.HttpConstants; |
| 28 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 29 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest; |
| 30 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; |
| 31 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; |
| 32 | +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; |
| 33 | +import io.opentelemetry.testing.internal.armeria.internal.shaded.guava.collect.ImmutableSet; |
| 34 | +import org.junit.ClassRule; |
| 35 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 36 | + |
| 37 | +public class ActivejHttpServerTest extends AbstractHttpServerTest<HttpServer> { |
| 38 | + |
| 39 | + @ClassRule public static final EventloopRule eventloopRule = new EventloopRule(); |
| 40 | + |
| 41 | + @RegisterExtension |
| 42 | + static final InstrumentationExtension testing = HttpServerInstrumentationExtension.forAgent(); |
| 43 | + |
| 44 | + private static final Eventloop eventloop = Reactor.getCurrentReactor(); |
| 45 | + private Thread thread; |
| 46 | + |
| 47 | + @Override |
| 48 | + protected HttpServer setupServer() throws Exception { |
| 49 | + |
| 50 | + AsyncServlet captureHttpHeadersAsyncServlet = |
| 51 | + request -> { |
| 52 | + HttpResponse httpResponse = |
| 53 | + HttpResponse.builder() |
| 54 | + .withBody(CAPTURE_HEADERS.getBody()) |
| 55 | + .withCode(CAPTURE_HEADERS.getStatus()) |
| 56 | + .withHeader(HttpHeaders.of(TEST_RESPONSE_HEADER), HttpHeaderValue.of("test")) |
| 57 | + .build(); |
| 58 | + controller(CAPTURE_HEADERS, () -> httpResponse); |
| 59 | + return httpResponse.toPromise(); |
| 60 | + }; |
| 61 | + |
| 62 | + RoutingServlet routingServlet = |
| 63 | + RoutingServlet.builder(eventloop) |
| 64 | + .with(GET, SUCCESS.getPath(), request -> prepareResponse(SUCCESS)) |
| 65 | + .with(GET, QUERY_PARAM.getPath(), request -> prepareResponse(QUERY_PARAM)) |
| 66 | + .with(GET, ERROR.getPath(), request -> prepareResponse(ERROR)) |
| 67 | + .with(GET, NOT_FOUND.getPath(), request -> prepareResponse(NOT_FOUND)) |
| 68 | + .with(GET, EXCEPTION.getPath(), request -> prepareResponse(EXCEPTION)) |
| 69 | + .with(GET, REDIRECT.getPath(), request -> prepareResponse(REDIRECT)) |
| 70 | + .with(GET, CAPTURE_HEADERS.getPath(), captureHttpHeadersAsyncServlet) |
| 71 | + .build(); |
| 72 | + |
| 73 | + HttpServer server = HttpServer.builder(eventloop, routingServlet).withListenPort(port).build(); |
| 74 | + |
| 75 | + server.listen(); |
| 76 | + thread = new Thread(eventloop); |
| 77 | + thread.start(); |
| 78 | + return server; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void stopServer(HttpServer server) throws Exception { |
| 83 | + server.closeFuture().get(); |
| 84 | + thread.join(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void assertHighConcurrency(int count) { |
| 89 | + // |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void configure(HttpServerTestOptions options) { |
| 94 | + options.setTestHttpPipelining(false); |
| 95 | + options.setTestRedirect(false); |
| 96 | + options.setTestException(false); |
| 97 | + options.disableTestNonStandardHttpMethod(); |
| 98 | + options.setExpectedServerSpanNameMapper( |
| 99 | + (uri, method, route) -> { |
| 100 | + if (HttpConstants._OTHER.equals(method)) { |
| 101 | + method = "HTTP"; |
| 102 | + } |
| 103 | + return method; |
| 104 | + }); |
| 105 | + options.setHttpAttributes(endpoint -> ImmutableSet.of(NETWORK_PEER_PORT)); |
| 106 | + } |
| 107 | + |
| 108 | + Promise<HttpResponse> prepareResponse(ServerEndpoint endpoint) { |
| 109 | + HttpResponse httpResponse = |
| 110 | + HttpResponse.builder().withBody(endpoint.getBody()).withCode(endpoint.getStatus()).build(); |
| 111 | + controller(endpoint, () -> httpResponse); |
| 112 | + return httpResponse.toPromise(); |
| 113 | + } |
| 114 | +} |
0 commit comments