Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instrumentation for quarkus-resteasy-reactive #8487

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id("otel.java-conventions")
}

dependencies {
implementation(project(":testing-common"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.quarkus.resteasy.reactive;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.testing.internal.armeria.client.ClientFactory;
import io.opentelemetry.testing.internal.armeria.client.WebClient;
import io.opentelemetry.testing.internal.armeria.client.logging.LoggingClient;
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpRequest;
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpMethod;
import java.time.Duration;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public abstract class AbstractQuarkusJaxRsTest {

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

private static WebClient client;
private static int port;

@BeforeAll
static void setUp() {
client =
WebClient.builder()
.responseTimeout(Duration.ofMinutes(1))
.writeTimeout(Duration.ofMinutes(1))
.factory(ClientFactory.builder().connectTimeout(Duration.ofMinutes(1)).build())
.decorator(LoggingClient.newDecorator())
.build();
port = Integer.parseInt(System.getProperty("quarkus.http.test-port"));
}

private static AggregatedHttpResponse request(String path) {
AggregatedHttpRequest request =
AggregatedHttpRequest.of(HttpMethod.GET, "h1c://localhost:" + port + path);
return client.execute(request).aggregate().join();
}

@Test
void testPathOnMethod() {
AggregatedHttpResponse response = request("/test");
assertThat(response.status().code()).isEqualTo(200);
assertThat(response.contentUtf8()).isEqualTo("success");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("GET /test").hasKind(SpanKind.SERVER)));
}

@Test
void testPathOnClass() {
AggregatedHttpResponse response = request("/hello");
assertThat(response.status().code()).isEqualTo(200);
assertThat(response.contentUtf8()).isEqualTo("hello");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("GET /hello").hasKind(SpanKind.SERVER)));
}

@Test
void testPathParameter() {
AggregatedHttpResponse response = request("/hello/greeting/test");
assertThat(response.status().code()).isEqualTo(200);
assertThat(response.contentUtf8()).isEqualTo("hello test");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("GET /hello/greeting/{name}").hasKind(SpanKind.SERVER)));
}

@Test
void testSubResourceLocator() {
AggregatedHttpResponse response = request("/test-sub-resource-locator/call/sub");
assertThat(response.status().code()).isEqualTo(200);
assertThat(response.contentUtf8()).isEqualTo("success");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET /test-sub-resource-locator/call/sub")
.hasKind(SpanKind.SERVER)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("io.quarkus")
module.set("quarkus-resteasy-reactive")
versions.set("(,)")
}
}

dependencies {
compileOnly("io.quarkus:quarkus-resteasy-reactive:1.11.0.Final")
implementation(project(":instrumentation:jaxrs:jaxrs-common:javaagent"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.quarkus.resteasy.reactive;

import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;

public class InvocationHandlerInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("org.jboss.resteasy.reactive.server.handlers.InvocationHandler");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
named("handle"), InvocationHandlerInstrumentation.class.getName() + "$HandleAdvice");
}

@SuppressWarnings("unused")
public static class HandleAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) ResteasyReactiveRequestContext requestContext,
@Advice.Local("otelScope") Scope scope) {
ResteasyReactiveSpanName.INSTANCE.updateServerSpanName(requestContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.quarkus.resteasy.reactive;

import static java.util.Collections.singletonList;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class QuarkusResteasyReactiveInstrumentationModule extends InstrumentationModule {

public QuarkusResteasyReactiveInstrumentationModule() {
super("quarkus", "jaxrs", "quarkus-resteasy-reactive", "quarkus-resteasy-reactive-3.0");
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new InvocationHandlerInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.quarkus.resteasy.reactive;

import static io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsPathUtil.normalizePath;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteGetter;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteHolder;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteSource;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import javax.annotation.Nullable;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.mapping.RuntimeResource;
import org.jboss.resteasy.reactive.server.mapping.URITemplate;

public class ResteasyReactiveSpanName implements HttpRouteGetter<String> {
// remember previous path to handle sub path locators
private static final VirtualField<ResteasyReactiveRequestContext, String> pathField =
VirtualField.find(ResteasyReactiveRequestContext.class, String.class);

public static final ResteasyReactiveSpanName INSTANCE = new ResteasyReactiveSpanName();

public void updateServerSpanName(ResteasyReactiveRequestContext requestContext) {
Context context = Context.current();
String jaxRsName = calculateJaxRsName(requestContext);
HttpRouteHolder.updateHttpRoute(context, HttpRouteSource.NESTED_CONTROLLER, this, jaxRsName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpRouteHolder has a variant where it simply accepts a String route; you can use it and skip implementing the "fake" HttpRouteGetter

pathField.set(requestContext, jaxRsName);
}

private static String calculateJaxRsName(ResteasyReactiveRequestContext requestContext) {
RuntimeResource target = requestContext.getTarget();
URITemplate classPath = target.getClassPath();
URITemplate path = target.getPath();
String name = normalize(classPath) + normalize(path);
if (name.isEmpty()) {
return null;
}
String existingPath = pathField.get(requestContext);
return existingPath == null || existingPath.isEmpty() ? name : existingPath + name;
}

@Override
@Nullable
public String get(Context context, String jaxRsName) {
return jaxRsName;
}

private static String normalize(URITemplate uriTemplate) {
if (uriTemplate == null) {
return "";
}

return normalizePath(uriTemplate.template);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id("otel.javaagent-testing")

id("io.quarkus") version "2.16.7.Final"
}

otelJava {
minJavaVersionSupported.set(JavaVersion.VERSION_11)
}

dependencies {
implementation(enforcedPlatform("io.quarkus.platform:quarkus-bom:2.16.7.Final"))
implementation("io.quarkus:quarkus-resteasy-reactive")

testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
testInstrumentation(project(":instrumentation:quarkus-resteasy-reactive:javaagent"))

testImplementation(project(":instrumentation:quarkus-resteasy-reactive:common-testing"))
testImplementation("io.quarkus:quarkus-junit5")
}

tasks.named("compileJava").configure {
dependsOn(tasks.named("compileQuarkusGeneratedSourcesJava"))
}
tasks.named("sourcesJar").configure {
dependsOn(tasks.named("compileQuarkusGeneratedSourcesJava"))
}
tasks.named("checkstyleTest").configure {
dependsOn(tasks.named("compileQuarkusGeneratedSourcesJava"))
}
tasks.named("compileTestJava").configure {
dependsOn(tasks.named("compileQuarkusTestGeneratedSourcesJava"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.quarkus.resteasy.reactive.v2_0;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/hello")
public class GreetingResource {

@GET
@Path("/greeting/{name}")
public String greeting(String name) {
return "hello " + name;
}

@GET
public String hello() {
return "hello";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.quarkus.resteasy.reactive.v2_0;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("test-sub-resource-locator")
public class SubResourceLocatorTestResource {

@Path("call")
public Object call() {
return new SubResource();
}

public static class SubResource {
@Path("sub")
@GET
public String call() {
return "success";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.quarkus.resteasy.reactive.v2_0;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("")
public class TestRootResource {

@GET
@Path("test")
public String test() {
return "success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.http.test-port=0
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.quarkus.resteasy.reactive.v2_0;

import io.opentelemetry.instrumentation.quarkus.resteasy.reactive.AbstractQuarkusJaxRsTest;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
class QuarkusJaxRsTest extends AbstractQuarkusJaxRsTest {}
Loading