|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.ratpack.server; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 10 | +import static io.opentelemetry.semconv.ClientAttributes.CLIENT_ADDRESS; |
| 11 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD; |
| 12 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE; |
| 13 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE; |
| 14 | +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS; |
| 15 | +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; |
| 16 | +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION; |
| 17 | +import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; |
| 18 | +import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; |
| 19 | +import static io.opentelemetry.semconv.UrlAttributes.URL_PATH; |
| 20 | +import static io.opentelemetry.semconv.UrlAttributes.URL_QUERY; |
| 21 | +import static io.opentelemetry.semconv.UrlAttributes.URL_SCHEME; |
| 22 | +import static io.opentelemetry.semconv.UserAgentAttributes.USER_AGENT_ORIGINAL; |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +import io.opentelemetry.api.common.Attributes; |
| 26 | +import io.opentelemetry.api.trace.SpanKind; |
| 27 | +import io.opentelemetry.instrumentation.test.utils.PortUtils; |
| 28 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 29 | +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; |
| 30 | +import io.opentelemetry.testing.internal.armeria.client.WebClient; |
| 31 | +import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse; |
| 32 | +import java.net.InetAddress; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | +import java.util.function.Consumer; |
| 36 | +import java.util.stream.Stream; |
| 37 | +import org.junit.jupiter.api.AfterAll; |
| 38 | +import org.junit.jupiter.api.BeforeAll; |
| 39 | +import org.junit.jupiter.api.TestInstance; |
| 40 | +import org.junit.jupiter.params.ParameterizedTest; |
| 41 | +import org.junit.jupiter.params.provider.Arguments; |
| 42 | +import org.junit.jupiter.params.provider.MethodSource; |
| 43 | +import ratpack.func.Action; |
| 44 | +import ratpack.handling.Chain; |
| 45 | +import ratpack.path.PathBinding; |
| 46 | +import ratpack.server.RatpackServer; |
| 47 | +import ratpack.server.RatpackServerSpec; |
| 48 | + |
| 49 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 50 | +public abstract class AbstractRatpackRoutesTest { |
| 51 | + |
| 52 | + private static RatpackServer app; |
| 53 | + private static WebClient client; |
| 54 | + |
| 55 | + protected abstract InstrumentationExtension testing(); |
| 56 | + |
| 57 | + protected abstract void configure(RatpackServerSpec serverSpec) throws Exception; |
| 58 | + |
| 59 | + protected abstract boolean hasHandlerSpan(); |
| 60 | + |
| 61 | + @BeforeAll |
| 62 | + void setUp() throws Exception { |
| 63 | + app = |
| 64 | + RatpackServer.start( |
| 65 | + ratpackServerSpec -> { |
| 66 | + ratpackServerSpec.serverConfig( |
| 67 | + serverConfigBuilder -> { |
| 68 | + serverConfigBuilder.port(PortUtils.findOpenPort()); |
| 69 | + serverConfigBuilder.address(InetAddress.getByName("localhost")); |
| 70 | + }); |
| 71 | + |
| 72 | + Action<? super Chain> action = |
| 73 | + chain -> |
| 74 | + chain.all( |
| 75 | + context -> |
| 76 | + context.render(context.get(PathBinding.class).getDescription())); |
| 77 | + ratpackServerSpec.handlers( |
| 78 | + chain -> { |
| 79 | + chain.prefix("a", action); |
| 80 | + chain.prefix("b/::\\d+", action); |
| 81 | + chain.prefix("c/:val?", action); |
| 82 | + chain.prefix("d/:val", action); |
| 83 | + chain.prefix("e/:val?:\\d+", action); |
| 84 | + chain.prefix("f/:val:\\d+", action); |
| 85 | + }); |
| 86 | + |
| 87 | + configure(ratpackServerSpec); |
| 88 | + }); |
| 89 | + |
| 90 | + // Force HTTP/1 with h1c to prevent tracing of upgrade request. |
| 91 | + client = WebClient.of("h1c://localhost:" + app.getBindPort()); |
| 92 | + } |
| 93 | + |
| 94 | + @AfterAll |
| 95 | + void cleanUp() throws Exception { |
| 96 | + app.stop(); |
| 97 | + } |
| 98 | + |
| 99 | + private static Stream<Arguments> provideBindingArguments() { |
| 100 | + return Stream.of( |
| 101 | + Arguments.of("a", "a"), |
| 102 | + Arguments.of("b/123", "b/::\\d+"), |
| 103 | + Arguments.of("c", "c/:val?"), |
| 104 | + Arguments.of("c/123", "c/:val?"), |
| 105 | + Arguments.of("c/foo", "c/:val?"), |
| 106 | + Arguments.of("d/123", "d/:val"), |
| 107 | + Arguments.of("d/foo", "d/:val"), |
| 108 | + Arguments.of("e", "e/:val?:\\d+"), |
| 109 | + Arguments.of("e/123", "e/:val?:\\d+"), |
| 110 | + Arguments.of("e/foo", "e/:val?:\\d+"), |
| 111 | + Arguments.of("f/123", "f/:val:\\d+")); |
| 112 | + } |
| 113 | + |
| 114 | + @ParameterizedTest |
| 115 | + @MethodSource("provideBindingArguments") |
| 116 | + void bindingsForPath(String path, String route) { |
| 117 | + AggregatedHttpResponse response = client.get(path).aggregate().join(); |
| 118 | + |
| 119 | + assertThat(response.status().code()).isEqualTo(200); |
| 120 | + assertThat(response.contentUtf8()).isEqualTo(route); |
| 121 | + |
| 122 | + testing() |
| 123 | + .waitAndAssertTraces( |
| 124 | + trace -> { |
| 125 | + List<Consumer<SpanDataAssert>> assertions = new ArrayList<>(); |
| 126 | + assertions.add( |
| 127 | + span -> |
| 128 | + span.hasName("GET /" + route) |
| 129 | + .hasKind(SpanKind.SERVER) |
| 130 | + .hasNoParent() |
| 131 | + .hasAttributesSatisfyingExactly( |
| 132 | + equalTo(NETWORK_PROTOCOL_VERSION, "1.1"), |
| 133 | + equalTo(SERVER_ADDRESS, "localhost"), |
| 134 | + equalTo(SERVER_PORT, app.getBindPort()), |
| 135 | + equalTo(CLIENT_ADDRESS, hasHandlerSpan() ? "127.0.0.1" : null), |
| 136 | + equalTo(NETWORK_PEER_ADDRESS, hasHandlerSpan() ? "127.0.0.1" : null), |
| 137 | + satisfies( |
| 138 | + NETWORK_PEER_PORT, |
| 139 | + port -> |
| 140 | + port.satisfiesAnyOf( |
| 141 | + val -> assertThat(val).isInstanceOf(Long.class), |
| 142 | + val -> assertThat(val).isNull())), |
| 143 | + satisfies( |
| 144 | + NETWORK_PEER_PORT, |
| 145 | + val -> { |
| 146 | + if (hasHandlerSpan()) { |
| 147 | + val.isInstanceOf(Long.class); |
| 148 | + } else { |
| 149 | + val.isNull(); |
| 150 | + } |
| 151 | + }), |
| 152 | + equalTo(HTTP_REQUEST_METHOD, "GET"), |
| 153 | + equalTo(HTTP_RESPONSE_STATUS_CODE, 200), |
| 154 | + satisfies(USER_AGENT_ORIGINAL, val -> val.isInstanceOf(String.class)), |
| 155 | + equalTo(URL_SCHEME, "http"), |
| 156 | + equalTo(URL_PATH, "/" + path), |
| 157 | + satisfies(URL_QUERY, val -> val.isNullOrEmpty()), |
| 158 | + equalTo(HTTP_ROUTE, "/" + route))); |
| 159 | + if (hasHandlerSpan()) { |
| 160 | + assertions.add( |
| 161 | + span -> |
| 162 | + span.hasName("/" + route) |
| 163 | + .hasKind(SpanKind.INTERNAL) |
| 164 | + .hasParent(trace.getSpan(0)) |
| 165 | + .hasAttributes(Attributes.empty())); |
| 166 | + } |
| 167 | + |
| 168 | + trace.hasSpansSatisfyingExactly(assertions); |
| 169 | + }); |
| 170 | + } |
| 171 | +} |
0 commit comments