|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.javalin.v5_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 11 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE; |
| 12 | + |
| 13 | +import io.javalin.Javalin; |
| 14 | +import io.opentelemetry.api.trace.SpanKind; |
| 15 | +import io.opentelemetry.instrumentation.test.utils.PortUtils; |
| 16 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 17 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 18 | +import io.opentelemetry.semconv.ClientAttributes; |
| 19 | +import io.opentelemetry.semconv.ErrorAttributes; |
| 20 | +import io.opentelemetry.semconv.HttpAttributes; |
| 21 | +import io.opentelemetry.semconv.NetworkAttributes; |
| 22 | +import io.opentelemetry.semconv.ServerAttributes; |
| 23 | +import io.opentelemetry.semconv.UrlAttributes; |
| 24 | +import io.opentelemetry.semconv.UserAgentAttributes; |
| 25 | +import io.opentelemetry.testing.internal.armeria.client.WebClient; |
| 26 | +import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse; |
| 27 | +import org.junit.jupiter.api.AfterAll; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 31 | + |
| 32 | +class JavalinTest { |
| 33 | + |
| 34 | + @RegisterExtension |
| 35 | + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 36 | + |
| 37 | + private static Javalin app; |
| 38 | + private static int port; |
| 39 | + private static WebClient client; |
| 40 | + |
| 41 | + @BeforeAll |
| 42 | + static void setup() { |
| 43 | + port = PortUtils.findOpenPort(); |
| 44 | + app = TestJavalinJavaApplication.initJavalin(port); |
| 45 | + client = WebClient.of("http://localhost:" + port); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterAll |
| 49 | + static void cleanup() { |
| 50 | + app.stop(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void testSpanNameAndHttpRouteSpanWithPathParamResponseSuccessful() { |
| 55 | + String id = "123"; |
| 56 | + AggregatedHttpResponse response = client.get("/param/" + id).aggregate().join(); |
| 57 | + String content = response.contentUtf8(); |
| 58 | + |
| 59 | + assertThat(content).isEqualTo(id); |
| 60 | + assertThat(response.status().code()).isEqualTo(200); |
| 61 | + testing.waitAndAssertTraces( |
| 62 | + trace -> |
| 63 | + trace.hasSpansSatisfyingExactly( |
| 64 | + span -> |
| 65 | + span.hasName("GET /param/{id}") |
| 66 | + .hasKind(SpanKind.SERVER) |
| 67 | + .hasNoParent() |
| 68 | + .hasAttributesSatisfyingExactly( |
| 69 | + equalTo(UrlAttributes.URL_SCHEME, "http"), |
| 70 | + equalTo(UrlAttributes.URL_PATH, "/param/" + id), |
| 71 | + equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"), |
| 72 | + equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200), |
| 73 | + satisfies( |
| 74 | + UserAgentAttributes.USER_AGENT_ORIGINAL, |
| 75 | + val -> val.isInstanceOf(String.class)), |
| 76 | + equalTo(HTTP_ROUTE, "/param/{id}"), |
| 77 | + equalTo(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), |
| 78 | + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), |
| 79 | + equalTo(ServerAttributes.SERVER_PORT, port), |
| 80 | + equalTo(ClientAttributes.CLIENT_ADDRESS, "127.0.0.1"), |
| 81 | + equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"), |
| 82 | + satisfies( |
| 83 | + NetworkAttributes.NETWORK_PEER_PORT, |
| 84 | + val -> val.isInstanceOf(Long.class))))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testSpanNameAndHttpRouteSpanResponseError() { |
| 89 | + client.get("/error").aggregate().join(); |
| 90 | + |
| 91 | + testing.waitAndAssertTraces( |
| 92 | + trace -> |
| 93 | + trace.hasSpansSatisfyingExactly( |
| 94 | + span -> |
| 95 | + span.hasName("GET /error") |
| 96 | + .hasKind(SpanKind.SERVER) |
| 97 | + .hasNoParent() |
| 98 | + .hasAttributesSatisfyingExactly( |
| 99 | + equalTo(UrlAttributes.URL_SCHEME, "http"), |
| 100 | + equalTo(UrlAttributes.URL_PATH, "/error"), |
| 101 | + equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"), |
| 102 | + equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 500), |
| 103 | + satisfies( |
| 104 | + UserAgentAttributes.USER_AGENT_ORIGINAL, |
| 105 | + val -> val.isInstanceOf(String.class)), |
| 106 | + equalTo(HTTP_ROUTE, "/error"), |
| 107 | + equalTo(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), |
| 108 | + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), |
| 109 | + equalTo(ServerAttributes.SERVER_PORT, port), |
| 110 | + equalTo(ErrorAttributes.ERROR_TYPE, "500"), |
| 111 | + equalTo(ClientAttributes.CLIENT_ADDRESS, "127.0.0.1"), |
| 112 | + equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"), |
| 113 | + satisfies( |
| 114 | + NetworkAttributes.NETWORK_PEER_PORT, |
| 115 | + val -> val.isInstanceOf(Long.class))))); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testSpanNameAndHttpRouteSpanAsyncRouteResponseSuccessful() { |
| 120 | + AggregatedHttpResponse response = client.get("/async").aggregate().join(); |
| 121 | + |
| 122 | + assertThat(response.status().code()).isEqualTo(200); |
| 123 | + testing.waitAndAssertTraces( |
| 124 | + trace -> |
| 125 | + trace.hasSpansSatisfyingExactly( |
| 126 | + span -> |
| 127 | + span.hasName("GET /async") |
| 128 | + .hasKind(SpanKind.SERVER) |
| 129 | + .hasNoParent() |
| 130 | + .hasAttributesSatisfyingExactly( |
| 131 | + equalTo(UrlAttributes.URL_SCHEME, "http"), |
| 132 | + equalTo(UrlAttributes.URL_PATH, "/async"), |
| 133 | + equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"), |
| 134 | + equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200), |
| 135 | + satisfies( |
| 136 | + UserAgentAttributes.USER_AGENT_ORIGINAL, |
| 137 | + val -> val.isInstanceOf(String.class)), |
| 138 | + equalTo(HTTP_ROUTE, "/async"), |
| 139 | + equalTo(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), |
| 140 | + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), |
| 141 | + equalTo(ServerAttributes.SERVER_PORT, port), |
| 142 | + equalTo(ClientAttributes.CLIENT_ADDRESS, "127.0.0.1"), |
| 143 | + equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"), |
| 144 | + satisfies( |
| 145 | + NetworkAttributes.NETWORK_PEER_PORT, |
| 146 | + val -> val.isInstanceOf(Long.class))))); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void testHttpRouteMetricWithPathParamResponseSuccessful() { |
| 151 | + String id = "123"; |
| 152 | + AggregatedHttpResponse response = client.get("/param/" + id).aggregate().join(); |
| 153 | + String content = response.contentUtf8(); |
| 154 | + String instrumentation = "io.opentelemetry.jetty-11.0"; |
| 155 | + |
| 156 | + assertThat(content).isEqualTo(id); |
| 157 | + assertThat(response.status().code()).isEqualTo(200); |
| 158 | + testing.waitAndAssertMetrics( |
| 159 | + instrumentation, |
| 160 | + "http.server.request.duration", |
| 161 | + metrics -> |
| 162 | + metrics.anySatisfy( |
| 163 | + metric -> |
| 164 | + assertThat(metric) |
| 165 | + .hasHistogramSatisfying( |
| 166 | + histogram -> |
| 167 | + histogram.hasPointsSatisfying( |
| 168 | + point -> point.hasAttribute(HTTP_ROUTE, "/param/{id}"))))); |
| 169 | + } |
| 170 | +} |
0 commit comments