-
Notifications
You must be signed in to change notification settings - Fork 867
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
Restlet 2.0 instrumentation #4535
Merged
+973
−0
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
instrumentation/restlet/restlet-2.0/javaagent/build.gradle.kts
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,42 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("org.restlet") | ||
module.set("org.restlet.jse") | ||
versions.set("[2.0.0,)") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven("https://maven.restlet.talend.com/") | ||
mavenLocal() | ||
} | ||
|
||
dependencies { | ||
api(project(":instrumentation:restlet:restlet-2.0:library")) | ||
|
||
library("org.restlet.jse:org.restlet:2.0.2") | ||
|
||
implementation(project(":instrumentation:restlet:restlet-2.0:library")) | ||
|
||
testImplementation(project(":instrumentation:restlet:restlet-2.0:testing")) | ||
testImplementation("org.restlet.jse:org.restlet.ext.jetty:2.0.2") | ||
} | ||
|
||
// restlet registers the first engine that is present on classpath, so we need to enforce the appropriate version | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (findProperty("testLatestDeps") as Boolean) { | ||
configurations.configureEach { | ||
resolutionStrategy { | ||
eachDependency { | ||
if (requested.group == "org.restlet.jse") { | ||
useVersion("2.+") | ||
} | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletInstrumentationModule.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,25 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.restlet.v2_0; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class RestletInstrumentationModule extends InstrumentationModule { | ||
|
||
public RestletInstrumentationModule() { | ||
super("restlet", "restlet-2.0"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Arrays.asList(new ServerInstrumentation(), new RouteInstrumentation()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletSingletons.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,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.restlet.v2_0; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.restlet.v2_0.RestletTracing; | ||
import org.restlet.Request; | ||
import org.restlet.Response; | ||
|
||
public final class RestletSingletons { | ||
|
||
private static final Instrumenter<Request, Response> INSTRUMENTER = | ||
RestletTracing.create(GlobalOpenTelemetry.get()).getServerInstrumenter(); | ||
|
||
public static Instrumenter<Request, Response> instrumenter() { | ||
return INSTRUMENTER; | ||
} | ||
|
||
private RestletSingletons() {} | ||
} |
52 changes: 52 additions & 0 deletions
52
...in/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RouteInstrumentation.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,52 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.restlet.v2_0; | ||
|
||
import static io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming.Source.CONTROLLER; | ||
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming; | ||
import io.opentelemetry.instrumentation.restlet.v2_0.internal.RestletServerSpanNaming; | ||
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.restlet.Request; | ||
import org.restlet.routing.TemplateRoute; | ||
|
||
public class RouteInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.restlet.routing.TemplateRoute").or(named("org.restlet.routing.Route")); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("beforeHandle")) | ||
.and(takesArgument(0, named("org.restlet.Request"))) | ||
.and(takesArgument(1, named("org.restlet.Response"))), | ||
this.getClass().getName() + "$RouteBeforeHandleAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RouteBeforeHandleAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void getRouteInfo( | ||
@Advice.This TemplateRoute route, @Advice.Argument(0) Request request) { | ||
String pattern = route.getTemplate().getPattern(); | ||
|
||
ServerSpanNaming.updateServerSpanName( | ||
currentContext(), CONTROLLER, RestletServerSpanNaming.SERVER_SPAN_NAME, pattern); | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...n/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/ServerInstrumentation.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,94 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.restlet.v2_0; | ||
|
||
import static io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming.Source.CONTROLLER; | ||
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext; | ||
import static io.opentelemetry.javaagent.instrumentation.restlet.v2_0.RestletSingletons.instrumenter; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming; | ||
import io.opentelemetry.instrumentation.restlet.v2_0.internal.RestletServerSpanNaming; | ||
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.restlet.Request; | ||
import org.restlet.Response; | ||
import org.restlet.data.Status; | ||
|
||
public class ServerInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.restlet.Server"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("handle")) | ||
.and(takesArgument(0, named("org.restlet.Request"))) | ||
.and(takesArgument(1, named("org.restlet.Response"))), | ||
this.getClass().getName() + "$ServerHandleAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ServerHandleAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void beginRequest( | ||
@Advice.Argument(0) Request request, | ||
@Advice.Argument(1) Response response, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
Context parentContext = currentContext(); | ||
|
||
if (!instrumenter().shouldStart(parentContext, request)) { | ||
return; | ||
} | ||
|
||
context = instrumenter().start(parentContext, request); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void finishRequest( | ||
@Advice.Argument(0) Request request, | ||
@Advice.Argument(1) Response response, | ||
@Advice.Thrown Throwable exception, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
if (scope == null) { | ||
return; | ||
} | ||
|
||
scope.close(); | ||
|
||
if (Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) { | ||
ServerSpanNaming.updateServerSpanName( | ||
context, CONTROLLER, RestletServerSpanNaming.SERVER_SPAN_NAME, "/*"); | ||
} | ||
|
||
if (exception != null) { | ||
instrumenter().end(context, request, response, exception); | ||
return; | ||
} | ||
|
||
// Restlet suppresses exceptions and sets the throwable in status | ||
Throwable statusThrowable = response.getStatus().getThrowable(); | ||
|
||
instrumenter().end(context, request, response, statusThrowable); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...t/groovy/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletServerTest.groovy
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,13 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.restlet.v2_0 | ||
|
||
import io.opentelemetry.instrumentation.restlet.v2_0.AbstractRestletServerTest | ||
import io.opentelemetry.instrumentation.test.AgentTestTrait | ||
|
||
class RestletServerTest extends AbstractRestletServerTest implements AgentTestTrait { | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
instrumentation/restlet/restlet-2.0/library/build.gradle.kts
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,29 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven("https://maven.restlet.talend.com/") | ||
mavenLocal() | ||
} | ||
|
||
dependencies { | ||
|
||
library("org.restlet.jse:org.restlet:2.0.2") | ||
|
||
testImplementation(project(":instrumentation:restlet:restlet-2.0:testing")) | ||
testImplementation("org.restlet.jse:org.restlet.ext.jetty:2.0.2") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (same question here) |
||
} | ||
// restlet registers the first engine that is present on classpath, so we need to enforce the appropriate version | ||
if (findProperty("testLatestDeps") as Boolean) { | ||
configurations.configureEach { | ||
resolutionStrategy { | ||
eachDependency { | ||
if (requested.group == "org.restlet.jse") { | ||
useVersion("2.+") | ||
} | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ary/src/main/java/io/opentelemetry/instrumentation/restlet/v2_0/RestletHeadersGetter.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,29 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.restlet.v2_0; | ||
|
||
import io.opentelemetry.context.propagation.TextMapGetter; | ||
import org.restlet.Message; | ||
import org.restlet.Request; | ||
import org.restlet.util.Series; | ||
|
||
final class RestletHeadersGetter implements TextMapGetter<Request> { | ||
|
||
@Override | ||
public Iterable<String> keys(Request carrier) { | ||
return getHeaders(carrier).getNames(); | ||
} | ||
|
||
@Override | ||
public String get(Request carrier, String key) { | ||
Series<?> headers = getHeaders(carrier); | ||
return headers.getFirstValue(key, /* ignoreCase = */ true); | ||
} | ||
|
||
static Series<?> getHeaders(Message carrier) { | ||
return (Series<?>) carrier.getAttributes().get("org.restlet.http.headers"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this work, and avoid needing the
resolutionStrategy
below?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the
resolutionStrategy
below still needed? I thinklatestDepTestLibrary
is supposed to handle thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it seems that even with
testLibrary
the wrong engine gets picked up and theresolutionStrategy
is still needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thanks for confirming 👍