|
| 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.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD; |
| 11 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE; |
| 12 | +import static io.opentelemetry.semconv.UrlAttributes.URL_PATH; |
| 13 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +import io.activej.eventloop.Eventloop; |
| 19 | +import io.activej.http.AsyncServlet; |
| 20 | +import io.activej.http.HttpError; |
| 21 | +import io.activej.http.HttpHeaders; |
| 22 | +import io.activej.http.HttpRequest; |
| 23 | +import io.activej.http.HttpResponse; |
| 24 | +import io.activej.http.RoutingServlet; |
| 25 | +import io.activej.http.UrlParser; |
| 26 | +import io.activej.promise.Promise; |
| 27 | +import io.activej.reactor.Reactor; |
| 28 | +import io.opentelemetry.api.trace.SpanKind; |
| 29 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 30 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 31 | +import org.junit.ClassRule; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 34 | + |
| 35 | +public class ActivejRoutingServletTest { |
| 36 | + |
| 37 | + @ClassRule public static final EventloopRule eventloopRule = new EventloopRule(); |
| 38 | + |
| 39 | + @RegisterExtension |
| 40 | + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 41 | + |
| 42 | + private static final Eventloop eventloop = Reactor.getCurrentReactor(); |
| 43 | + |
| 44 | + @Test |
| 45 | + void testRequestSuccessFlow() throws Exception { |
| 46 | + AsyncServlet asyncServlet = |
| 47 | + request -> HttpResponse.ofCode(200).withBody("".getBytes(UTF_8)).toPromise(); |
| 48 | + |
| 49 | + RoutingServlet routingServlet = |
| 50 | + RoutingServlet.builder(eventloop).with(GET, "/success", asyncServlet).build(); |
| 51 | + |
| 52 | + String url = "http://some-test.com/success"; |
| 53 | + HttpRequest httpRequest = HttpRequest.get(url).build(); |
| 54 | + check(routingServlet.serve(httpRequest), "", 200); |
| 55 | + |
| 56 | + UrlParser urlParser = UrlParser.of(url); |
| 57 | + |
| 58 | + testing.waitAndAssertTraces( |
| 59 | + trace -> |
| 60 | + trace.hasSpansSatisfyingExactly( |
| 61 | + span -> |
| 62 | + span.hasName("GET /success") |
| 63 | + .hasNoParent() |
| 64 | + .hasKind(SpanKind.SERVER) |
| 65 | + .hasAttributesSatisfying( |
| 66 | + equalTo(URL_PATH, urlParser.getPath()), |
| 67 | + equalTo(HTTP_REQUEST_METHOD, "GET"), |
| 68 | + equalTo(HTTP_RESPONSE_STATUS_CODE, 200)))); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testRequestNotFoundFlow() throws Exception { |
| 73 | + |
| 74 | + AsyncServlet asyncServlet = request -> HttpResponse.notFound404().withBody("").toPromise(); |
| 75 | + |
| 76 | + RoutingServlet routingServlet = |
| 77 | + RoutingServlet.builder(eventloop).with(GET, "/notfound", asyncServlet).build(); |
| 78 | + |
| 79 | + String url = "http://some-test.com/notfound"; |
| 80 | + HttpRequest httpRequest = HttpRequest.get(url).build(); |
| 81 | + check(routingServlet.serve(httpRequest), "", 404); |
| 82 | + |
| 83 | + UrlParser urlParser = UrlParser.of(url); |
| 84 | + |
| 85 | + testing.waitAndAssertTraces( |
| 86 | + trace -> |
| 87 | + trace.hasSpansSatisfyingExactly( |
| 88 | + span -> |
| 89 | + span.hasName("GET /notfound") |
| 90 | + .hasNoParent() |
| 91 | + .hasKind(SpanKind.SERVER) |
| 92 | + .hasAttributesSatisfying( |
| 93 | + equalTo(URL_PATH, urlParser.getPath()), |
| 94 | + equalTo(HTTP_REQUEST_METHOD, "GET"), |
| 95 | + equalTo(HTTP_RESPONSE_STATUS_CODE, 404)))); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testRequestErrorFlow() throws Exception { |
| 100 | + |
| 101 | + AsyncServlet asyncServlet = request -> Promise.ofException(HttpError.ofCode(500)); |
| 102 | + |
| 103 | + RoutingServlet routingServlet = |
| 104 | + RoutingServlet.builder(eventloop).with(GET, "/error", asyncServlet).build(); |
| 105 | + |
| 106 | + String url = "http://some-test.com/error"; |
| 107 | + HttpRequest httpRequest = HttpRequest.get(url).build(); |
| 108 | + check(routingServlet.serve(httpRequest), "HTTP code 500", 500); |
| 109 | + |
| 110 | + UrlParser urlParser = UrlParser.of(url); |
| 111 | + |
| 112 | + testing.waitAndAssertTraces( |
| 113 | + trace -> |
| 114 | + trace.hasSpansSatisfyingExactly( |
| 115 | + span -> |
| 116 | + span.hasName("GET /error") |
| 117 | + .hasNoParent() |
| 118 | + .hasKind(SpanKind.SERVER) |
| 119 | + .hasAttributesSatisfying( |
| 120 | + equalTo(URL_PATH, urlParser.getPath()), |
| 121 | + equalTo(HTTP_REQUEST_METHOD, "GET"), |
| 122 | + equalTo(HTTP_RESPONSE_STATUS_CODE, 500)))); |
| 123 | + } |
| 124 | + |
| 125 | + private static void check(Promise<HttpResponse> promise, String expectedBody, int expectedCode) { |
| 126 | + assertTrue(promise.isComplete()); |
| 127 | + if (promise.isResult() && !promise.isException()) { |
| 128 | + HttpResponse result = promise.getResult(); |
| 129 | + assertEquals(expectedBody, result.getBody().asString(UTF_8)); |
| 130 | + assertEquals(expectedCode, result.getCode()); |
| 131 | + } else { |
| 132 | + assertEquals(expectedCode, ((HttpError) promise.getException()).getCode()); |
| 133 | + } |
| 134 | + assertThat(promise.getResult().getHeader(HttpHeaders.of("traceparent"))) |
| 135 | + .isNotNull() |
| 136 | + .isNotBlank(); |
| 137 | + } |
| 138 | +} |
0 commit comments