-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add javalin instrumentation (#11587)
- Loading branch information
1 parent
51cb6f0
commit 8537d21
Showing
7 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("io.javalin") | ||
module.set("javalin") | ||
versions.set("[5.0.0,)") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
otelJava { | ||
minJavaVersionSupported.set(JavaVersion.VERSION_11) | ||
} | ||
|
||
dependencies { | ||
library("io.javalin:javalin:5.0.0") | ||
|
||
testInstrumentation(project(":instrumentation:jetty:jetty-11.0:javaagent")) | ||
} |
48 changes: 48 additions & 0 deletions
48
.../java/io/opentelemetry/javaagent/instrumentation/javalin/v5_0/JavalinInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.javalin.v5_0; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType; | ||
import static net.bytebuddy.matcher.ElementMatchers.isInterface; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.javalin.http.Context; | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource; | ||
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; | ||
|
||
public class JavalinInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return hasSuperType(named("io.javalin.http.Handler")).and(not(isInterface())); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("handle").and(takesArgument(0, named("io.javalin.http.Context"))), | ||
this.getClass().getName() + "$HandlerAdapterAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class HandlerAdapterAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onAfterExecute(@Advice.Argument(0) Context ctx, @Advice.Thrown Throwable t) { | ||
HttpServerRoute.update( | ||
io.opentelemetry.context.Context.current(), | ||
HttpServerRouteSource.CONTROLLER, | ||
ctx.endpointHandlerPath()); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...io/opentelemetry/javaagent/instrumentation/javalin/v5_0/JavalinInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.javalin.v5_0; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
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; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@SuppressWarnings("unused") | ||
@AutoService(InstrumentationModule.class) | ||
public class JavalinInstrumentationModule extends InstrumentationModule { | ||
|
||
public JavalinInstrumentationModule() { | ||
super("javalin", "javalin-5"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new JavalinInstrumentation()); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("io.javalin.http.Handler"); | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
...nt/src/test/java/io/opentelemetry/javaagent/instrumentation/javalin/v5_0/JavalinTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.javalin.v5_0; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; | ||
import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE; | ||
|
||
import io.javalin.Javalin; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.instrumentation.test.utils.PortUtils; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.semconv.ClientAttributes; | ||
import io.opentelemetry.semconv.ErrorAttributes; | ||
import io.opentelemetry.semconv.HttpAttributes; | ||
import io.opentelemetry.semconv.NetworkAttributes; | ||
import io.opentelemetry.semconv.ServerAttributes; | ||
import io.opentelemetry.semconv.UrlAttributes; | ||
import io.opentelemetry.semconv.UserAgentAttributes; | ||
import io.opentelemetry.testing.internal.armeria.client.WebClient; | ||
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class JavalinTest { | ||
|
||
@RegisterExtension | ||
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
private static Javalin app; | ||
private static int port; | ||
private static WebClient client; | ||
|
||
@BeforeAll | ||
static void setup() { | ||
port = PortUtils.findOpenPort(); | ||
app = TestJavalinJavaApplication.initJavalin(port); | ||
client = WebClient.of("http://localhost:" + port); | ||
} | ||
|
||
@AfterAll | ||
static void cleanup() { | ||
app.stop(); | ||
} | ||
|
||
@Test | ||
void testSpanNameAndHttpRouteSpanWithPathParamResponseSuccessful() { | ||
String id = "123"; | ||
AggregatedHttpResponse response = client.get("/param/" + id).aggregate().join(); | ||
String content = response.contentUtf8(); | ||
|
||
assertThat(content).isEqualTo(id); | ||
assertThat(response.status().code()).isEqualTo(200); | ||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> | ||
span.hasName("GET /param/{id}") | ||
.hasKind(SpanKind.SERVER) | ||
.hasNoParent() | ||
.hasAttributesSatisfyingExactly( | ||
equalTo(UrlAttributes.URL_SCHEME, "http"), | ||
equalTo(UrlAttributes.URL_PATH, "/param/" + id), | ||
equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"), | ||
equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200), | ||
satisfies( | ||
UserAgentAttributes.USER_AGENT_ORIGINAL, | ||
val -> val.isInstanceOf(String.class)), | ||
equalTo(HTTP_ROUTE, "/param/{id}"), | ||
equalTo(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), | ||
equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), | ||
equalTo(ServerAttributes.SERVER_PORT, port), | ||
equalTo(ClientAttributes.CLIENT_ADDRESS, "127.0.0.1"), | ||
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"), | ||
satisfies( | ||
NetworkAttributes.NETWORK_PEER_PORT, | ||
val -> val.isInstanceOf(Long.class))))); | ||
} | ||
|
||
@Test | ||
void testSpanNameAndHttpRouteSpanResponseError() { | ||
client.get("/error").aggregate().join(); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> | ||
span.hasName("GET /error") | ||
.hasKind(SpanKind.SERVER) | ||
.hasNoParent() | ||
.hasAttributesSatisfyingExactly( | ||
equalTo(UrlAttributes.URL_SCHEME, "http"), | ||
equalTo(UrlAttributes.URL_PATH, "/error"), | ||
equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"), | ||
equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 500), | ||
satisfies( | ||
UserAgentAttributes.USER_AGENT_ORIGINAL, | ||
val -> val.isInstanceOf(String.class)), | ||
equalTo(HTTP_ROUTE, "/error"), | ||
equalTo(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), | ||
equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), | ||
equalTo(ServerAttributes.SERVER_PORT, port), | ||
equalTo(ErrorAttributes.ERROR_TYPE, "500"), | ||
equalTo(ClientAttributes.CLIENT_ADDRESS, "127.0.0.1"), | ||
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"), | ||
satisfies( | ||
NetworkAttributes.NETWORK_PEER_PORT, | ||
val -> val.isInstanceOf(Long.class))))); | ||
} | ||
|
||
@Test | ||
public void testSpanNameAndHttpRouteSpanAsyncRouteResponseSuccessful() { | ||
AggregatedHttpResponse response = client.get("/async").aggregate().join(); | ||
|
||
assertThat(response.status().code()).isEqualTo(200); | ||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> | ||
span.hasName("GET /async") | ||
.hasKind(SpanKind.SERVER) | ||
.hasNoParent() | ||
.hasAttributesSatisfyingExactly( | ||
equalTo(UrlAttributes.URL_SCHEME, "http"), | ||
equalTo(UrlAttributes.URL_PATH, "/async"), | ||
equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"), | ||
equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200), | ||
satisfies( | ||
UserAgentAttributes.USER_AGENT_ORIGINAL, | ||
val -> val.isInstanceOf(String.class)), | ||
equalTo(HTTP_ROUTE, "/async"), | ||
equalTo(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), | ||
equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"), | ||
equalTo(ServerAttributes.SERVER_PORT, port), | ||
equalTo(ClientAttributes.CLIENT_ADDRESS, "127.0.0.1"), | ||
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"), | ||
satisfies( | ||
NetworkAttributes.NETWORK_PEER_PORT, | ||
val -> val.isInstanceOf(Long.class))))); | ||
} | ||
|
||
@Test | ||
void testHttpRouteMetricWithPathParamResponseSuccessful() { | ||
String id = "123"; | ||
AggregatedHttpResponse response = client.get("/param/" + id).aggregate().join(); | ||
String content = response.contentUtf8(); | ||
String instrumentation = "io.opentelemetry.jetty-11.0"; | ||
|
||
assertThat(content).isEqualTo(id); | ||
assertThat(response.status().code()).isEqualTo(200); | ||
testing.waitAndAssertMetrics( | ||
instrumentation, | ||
"http.server.request.duration", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metric -> | ||
assertThat(metric) | ||
.hasHistogramSatisfying( | ||
histogram -> | ||
histogram.hasPointsSatisfying( | ||
point -> point.hasAttribute(HTTP_ROUTE, "/param/{id}"))))); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...a/io/opentelemetry/javaagent/instrumentation/javalin/v5_0/TestJavalinJavaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.javalin.v5_0; | ||
|
||
import io.javalin.Javalin; | ||
|
||
public class TestJavalinJavaApplication { | ||
|
||
private TestJavalinJavaApplication() {} | ||
|
||
public static Javalin initJavalin(int port) { | ||
Javalin app = Javalin.create().start(port); | ||
app.get( | ||
"/param/{id}", | ||
ctx -> { | ||
String paramId = ctx.pathParam("id"); | ||
ctx.result(paramId); | ||
}); | ||
app.get( | ||
"/error", | ||
ctx -> { | ||
throw new RuntimeException("boom"); | ||
}); | ||
app.get("/async", ctx -> ctx.async(() -> ctx.result("ok"))); | ||
return app; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters