Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions instrumentation/vertx/vertx-web-3.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ dependencies {
testInstrumentation(project(":instrumentation:netty:netty-4.0:javaagent"))
testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
testInstrumentation(project(":instrumentation:jdbc:javaagent"))
testInstrumentation(project(":instrumentation:vertx:vertx-http-client:vertx-http-client-4.0:javaagent"))
}

testing {
suites {
register<JvmTestSuite>("version3Test") {
targets.all {
testTask.configure {
systemProperty("testLatestDeps", otelProps.testLatestDeps)
}
}
dependencies {
implementation(project(":instrumentation:vertx:vertx-web-3.0:testing"))

Expand All @@ -45,6 +51,25 @@ testing {
}
}

register<JvmTestSuite>("version45Test") {
sources {
java.srcDir("src/version41Test/java")
}
dependencies {
implementation(project(":instrumentation:vertx:vertx-web-3.0:testing"))
implementation("io.vertx:vertx-web:4.5.3")
implementation("io.vertx:vertx-codegen:4.5.3")
}
targets.all {
testTask.configure {
filter {
includeTestsMatching("*.multiHandlerRoute")
includeTestsMatching("*.subRouterRouteIncludesMountPath")
}
}
}
}

register<JvmTestSuite>("version5Test") {
dependencies {
implementation(project(":instrumentation:vertx:vertx-web-3.0:testing"))
Expand All @@ -63,6 +88,14 @@ tasks {
options.release.set(11)
}
val testJavaVersion = otelProps.testJavaVersion ?: JavaVersion.current()
named("compileVersion45TestJava", JavaCompile::class).configure {
options.release.set(21)
}
if (!testJavaVersion.isCompatibleWith(JavaVersion.VERSION_21)) {
named("version45Test", Test::class).configure {
enabled = false
}
}
if (!testJavaVersion.isCompatibleWith(JavaVersion.VERSION_11)) {
named("version5Test", Test::class).configure {
enabled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class NextAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
public static void next(@Advice.This RoutingContext routingContext) {
// calling next tells router to move to the next matching route
// calling next tells router to move to the next handler or matching route
// restore remembered route to remove currently matched route from it
String previousRoute = RoutingContextUtil.getRoute(routingContext);
if (previousRoute != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class RoutingContextUtil {
VirtualField.find(RoutingContext.class, String.class);

static void setRoute(RoutingContext routingContext, @Nullable String route) {
ROUTE_FIELD.set(routingContext, route);
// Distinguish an empty parent route from a context whose route has not been saved yet.
ROUTE_FIELD.set(routingContext, route == null ? "" : route);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

package io.opentelemetry.javaagent.instrumentation.vertx.web.v3_0;

import static io.opentelemetry.instrumentation.testing.util.TestLatestDeps.testLatestDeps;

import io.vertx.core.AbstractVerticle;

class VertxHttpServerTest extends AbstractVertxHttpServerTest {
@Override
protected boolean supportsMultiHandlerRoutes() {
return testLatestDeps();
Comment thread
trask marked this conversation as resolved.
Comment thread
trask marked this conversation as resolved.
}

@Override
protected Class<? extends AbstractVerticle> verticle() {
return VertxWebServer.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@

package io.opentelemetry.javaagent.instrumentation.vertx.web.v3_0;

import static io.opentelemetry.instrumentation.testing.util.TestLatestDeps.testLatestDeps;

import io.vertx.core.Future;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Router;

public class VertxWebServer extends AbstractVertxWebServer {

@Override
void addMultiHandlerRoutes(Router router) {
if (testLatestDeps()) {
super.addMultiHandlerRoutes(router);
}
}

@Override
void end(HttpServerResponse response) {
response.end();
Expand All @@ -28,6 +37,7 @@ public void start(Future<Void> startFuture) {
router.mountSubRouter("/child", buildRouter());

Router mainRouter = Router.router(vertx);
addMultiHandlerRoutes(mainRouter);
mainRouter.mountSubRouter("/vertx-app", router);

vertx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.javaagent.instrumentation.vertx.web.v3_0;

import static java.util.concurrent.TimeUnit.SECONDS;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;

Expand All @@ -17,6 +19,6 @@ protected Class<? extends AbstractVerticle> verticle() {

@Override
protected void stopServer(Vertx server) throws Exception {
server.close();
server.close().toCompletionStage().toCompletableFuture().get(30, SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void start(Promise<Void> startPromise) {
router.route("/child/*").subRouter(buildRouter());

Router mainRouter = Router.router(vertx);
addMultiHandlerRoutes(mainRouter);
mainRouter.route("/vertx-app/*").subRouter(router);

vertx
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.vertx.web.v3_0;

import io.vertx.core.DeploymentOptions;
import io.vertx.core.ThreadingModel;

class VertxVirtualThreadHttpServerTest extends Vertx41HttpServerTest {
@Override
protected DeploymentOptions deploymentOptions() {
Comment thread
trask marked this conversation as resolved.
Outdated
return super.deploymentOptions().setThreadingModel(ThreadingModel.VIRTUAL_THREAD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void start(Promise<Void> startPromise) {
router.route("/child/*").subRouter(buildRouter());

Router mainRouter = Router.router(vertx);
addMultiHandlerRoutes(mainRouter);
mainRouter.route("/vertx-app/*").subRouter(router);

vertx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@

package io.opentelemetry.javaagent.instrumentation.vertx.web.v3_0;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
Expand All @@ -26,6 +31,8 @@
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

abstract class AbstractVertxHttpServerTest extends AbstractHttpServerTest<Vertx> {

Expand Down Expand Up @@ -70,6 +77,49 @@ void subRouterRouteIncludesMountPath() {
assertTheTraces(1, null, null, null, method, SUB_ROUTER_PATH_PARAM);
}

@ParameterizedTest
@CsvSource({
"/standard, /standard, /standard",
"/virtual, /virtual, /virtual",
"/repeat/repeat, /repeat/repeat, GET /repeat/repeat",
"/precedence/123, /precedence/:id, GET /precedence/:id",
"/vertx-app/standard, /vertx-app/standard, /vertx-app/standard",
"/vertx-app/child/standard, /vertx-app/child/standard, /vertx-app/child/standard",
"/vertx-app/child/repeat/repeat, /vertx-app/child/repeat/repeat, GET /vertx-app/child/repeat/repeat"
Comment thread
trask marked this conversation as resolved.
})
void multiHandlerRoute(String path, String expectedRoute, String expectedName) {
assumeTrue(
supportsMultiHandlerRoutes(), "Vert.x 3.0 replaces rather than appends route handlers");
AggregatedHttpResponse response =
client
.get(address.resolve(path).toString().replace("http://", "h1c://"))
.aggregate()
.join();

assertThat(response.status().code()).isEqualTo(200);
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName(expectedName)
.hasKind(SpanKind.SERVER)
.hasNoParent()
.hasSpanId(response.contentUtf8())
.hasAttributesSatisfying(
Comment thread
trask marked this conversation as resolved.
Outdated
equalTo(HTTP_ROUTE, expectedRoute),
equalTo(stringKey("http.target"), path),
equalTo(stringKey("first-handler-span-id"), response.contentUtf8()),
equalTo(stringKey("custom-prop"), "from-handler"))));
}

protected boolean supportsMultiHandlerRoutes() {
return true;
}

protected DeploymentOptions deploymentOptions() {
return new DeploymentOptions();
}

@Override
protected Vertx setupServer() throws ExecutionException, InterruptedException, TimeoutException {
Vertx server =
Expand All @@ -82,7 +132,7 @@ protected Vertx setupServer() throws ExecutionException, InterruptedException, T

server.deployVerticle(
verticle().getName(),
new DeploymentOptions()
deploymentOptions()
// casting port to Object because the put override that takes Integer is removed in
// later versions of vertx-core
.setConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;

import io.opentelemetry.api.trace.Span;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerResponse;
Expand All @@ -30,8 +31,34 @@ public abstract class AbstractVertxWebServer extends AbstractVerticle {

abstract void end(HttpServerResponse response);

void addMultiHandlerRoutes(Router router) {
router.route("/precedence/*").handler(RoutingContext::next);
for (String path :
new String[] {"/standard", "/virtual", "/repeat/repeat", "/precedence/:id"}) {
router
.get(path)
.handler(
Comment thread
trask marked this conversation as resolved.
ctx -> {
Span span = Span.current();
if (path.equals("/standard") || path.equals("/virtual")) {
span.updateName(ctx.request().path());
}
span.setAttribute("http.target", ctx.request().path())
.setAttribute("first-handler-span-id", span.getSpanContext().getSpanId());
ctx.next();
})
.handler(
ctx -> {
Span span = Span.current();
span.setAttribute("custom-prop", "from-handler");
end(ctx.response(), span.getSpanContext().getSpanId());
});
}
}

Router buildRouter() {
Router router = Router.router(vertx);
addMultiHandlerRoutes(router);

router.route().handler(RoutingContext::next);
// verify that calling RoutingContext::next doesn't affect http.route
Expand Down
Loading