|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.vertx.reactive.server; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION; |
| 9 | + |
| 10 | +import io.opentelemetry.instrumentation.api.internal.HttpConstants; |
| 11 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest; |
| 12 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; |
| 13 | +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; |
| 14 | +import io.vertx.core.DeploymentOptions; |
| 15 | +import io.vertx.core.Promise; |
| 16 | +import io.vertx.core.Vertx; |
| 17 | +import io.vertx.core.VertxOptions; |
| 18 | +import io.vertx.core.json.JsonObject; |
| 19 | +import io.vertx.reactivex.core.AbstractVerticle; |
| 20 | +import io.vertx.reactivex.ext.web.Router; |
| 21 | +import io.vertx.reactivex.ext.web.RoutingContext; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +abstract class AbstractVertxRxHttpServerTest extends AbstractHttpServerTest<Vertx> { |
| 26 | + static final String CONFIG_HTTP_SERVER_PORT = "http.server.port"; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected Vertx setupServer() throws Exception { |
| 30 | + Vertx server = |
| 31 | + Vertx.vertx( |
| 32 | + new VertxOptions() |
| 33 | + // Useful for debugging: |
| 34 | + // .setBlockedThreadCheckInterval(Integer.MAX_VALUE) |
| 35 | + ); |
| 36 | + CompletableFuture<Void> future = new CompletableFuture<>(); |
| 37 | + server.deployVerticle( |
| 38 | + verticle().getName(), |
| 39 | + new DeploymentOptions() |
| 40 | + .setConfig(new JsonObject().put(CONFIG_HTTP_SERVER_PORT, port)) |
| 41 | + .setInstances(3), |
| 42 | + result -> { |
| 43 | + if (!result.succeeded()) { |
| 44 | + throw new IllegalStateException("Cannot deploy server Verticle", result.cause()); |
| 45 | + } |
| 46 | + future.complete(null); |
| 47 | + }); |
| 48 | + |
| 49 | + future.get(30, TimeUnit.SECONDS); |
| 50 | + return server; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void stopServer(Vertx vertx) { |
| 55 | + vertx.close(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void configure(HttpServerTestOptions options) { |
| 60 | + super.configure(options); |
| 61 | + |
| 62 | + options.setTestPathParam(true); |
| 63 | + // server spans are ended inside the controller spans |
| 64 | + options.setVerifyServerSpanEndTime(false); |
| 65 | + options.setExpectedHttpRoute( |
| 66 | + (endpoint, method) -> { |
| 67 | + if (HttpConstants._OTHER.equals(method)) { |
| 68 | + return getContextPath() + endpoint.getPath(); |
| 69 | + } |
| 70 | + return expectedHttpRoute(endpoint, method); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + protected Class<? extends AbstractVerticle> verticle() { |
| 75 | + return VertxReactiveWebServer.class; |
| 76 | + } |
| 77 | + |
| 78 | + public static class VertxReactiveWebServer extends AbstractVertxRxVerticle { |
| 79 | + @Override |
| 80 | + void handle(RoutingContext ctx, ServerEndpoint endpoint, Runnable action) { |
| 81 | + controller(endpoint, action::run); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void start(Promise<Void> startFuture) { |
| 86 | + int port = config().getInteger(CONFIG_HTTP_SERVER_PORT); |
| 87 | + Router router = Router.router(vertx); |
| 88 | + |
| 89 | + configure(router); |
| 90 | + router |
| 91 | + .route(EXCEPTION.getPath()) |
| 92 | + .handler( |
| 93 | + ctx -> |
| 94 | + handle( |
| 95 | + ctx, |
| 96 | + EXCEPTION, |
| 97 | + () -> { |
| 98 | + throw new IllegalStateException(EXCEPTION.getBody()); |
| 99 | + })); |
| 100 | + |
| 101 | + vertx |
| 102 | + .createHttpServer() |
| 103 | + .requestHandler(router) |
| 104 | + .listen(port, httpServerAsyncResult -> startFuture.complete()); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments