-
Notifications
You must be signed in to change notification settings - Fork 916
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
New agent instrumentation for Apache Sling #9469
Open
rombert
wants to merge
7
commits into
open-telemetry:main
Choose a base branch
from
rombert:issue/sling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0617d47
feat: introduce new, basic, instrumentation for Apache Sling
rombert 8ec8ce9
sling: implement classloader optimisation
rombert f8eef78
sling: cleanup some traces and commented out code
rombert ad6d28e
sling: final cleanups and final refactorings
rombert a93dbf3
sling: don't add unused method arguments to instrumentation
rombert 4bbf95b
sling: spotless fix-up
rombert 315b5da
sling: adapt to API changes
rombert 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
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,12 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
dependencies { | ||
bootstrap(project(":instrumentation:executors:bootstrap")) | ||
implementation(project(":instrumentation:servlet:servlet-3.0:javaagent")) | ||
bootstrap(project(":instrumentation:servlet:servlet-common:bootstrap")) | ||
api(project(":instrumentation:servlet:servlet-common:javaagent")) | ||
compileOnly("javax.servlet:javax.servlet-api:3.1.0") | ||
library("org.apache.sling:org.apache.sling.api:2.0.6") // first non-incubator release | ||
} |
68 changes: 68 additions & 0 deletions
68
...java/io/opentelemetry/javaagent/instrumentation/sling/ServletResolverInstrumentation.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,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.sling; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; | ||
import static io.opentelemetry.javaagent.instrumentation.sling.SlingSingletons.REQUEST_ATTR_RESOLVED_SERVLET_NAME; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import javax.servlet.Servlet; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
|
||
public class ServletResolverInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return implementsInterface(named("org.apache.sling.api.servlets.ServletResolver")); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("resolveServlet")) | ||
.and(takesArguments(1)) | ||
.and(takesArgument(0, named("org.apache.sling.api.SlingHttpServletRequest"))), | ||
this.getClass().getName() + "$ResolveServletAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ResolveServletAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onExit( | ||
@Advice.Argument(0) SlingHttpServletRequest request, | ||
@Advice.Return Servlet servlet, | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
String name = null; | ||
|
||
if (servlet.getServletConfig() != null) { | ||
name = servlet.getServletConfig().getServletName(); | ||
} | ||
if (name == null || name.isEmpty()) { | ||
name = servlet.getServletInfo(); | ||
} | ||
if (name == null || name.isEmpty()) { | ||
name = servlet.getClass().getName(); | ||
} | ||
|
||
request.setAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME, name); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ain/java/io/opentelemetry/javaagent/instrumentation/sling/SlingInstrumentationModule.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,26 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.sling; | ||
|
||
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 SlingInstrumentationModule extends InstrumentationModule { | ||
|
||
public SlingInstrumentationModule() { | ||
super("sling", "sling-1.0"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Arrays.asList( | ||
new ServletResolverInstrumentation(), new SlingSafeMethodsServletInstrumentation()); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...opentelemetry/javaagent/instrumentation/sling/SlingSafeMethodsServletInstrumentation.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,107 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.sling; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; | ||
import static io.opentelemetry.javaagent.instrumentation.sling.SlingSingletons.REQUEST_ATTR_RESOLVED_SERVLET_NAME; | ||
import static io.opentelemetry.javaagent.instrumentation.sling.SlingSingletons.helper; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource; | ||
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import javax.servlet.ServletRequest; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
|
||
public class SlingSafeMethodsServletInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return implementsInterface(named("javax.servlet.Servlet")); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed("org.apache.sling.api.SlingHttpServletRequest"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
|
||
String adviceClassName = this.getClass().getName() + "$ServiceServletAdvice"; | ||
transformer.applyAdviceToMethod( | ||
named("service") | ||
.and(takesArguments(2)) | ||
.and(takesArgument(0, named("javax.servlet.ServletRequest"))) | ||
.and(takesArgument(1, named("javax.servlet.ServletResponse"))), | ||
adviceClassName); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ServiceServletAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(0) ServletRequest request, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
if (!(request instanceof SlingHttpServletRequest)) { | ||
return; | ||
} | ||
|
||
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; | ||
|
||
Context parentContext = Java8BytecodeBridge.currentContext(); | ||
|
||
if (!helper().shouldStart(parentContext, slingRequest)) { | ||
return; | ||
} | ||
|
||
// written by ServletResolverInstrumentation | ||
Object servletName = request.getAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME); | ||
if (!(servletName instanceof String)) { | ||
return; | ||
} | ||
|
||
// TODO - figure out why don't we have matches for all requests and find a better way to | ||
// filter | ||
context = helper().start(parentContext, slingRequest); | ||
scope = context.makeCurrent(); | ||
|
||
// ensure that the top-level route is Sling-specific | ||
HttpServerRoute.update(context, HttpServerRouteSource.CONTROLLER, (String) servletName); | ||
|
||
// cleanup and ensure we don't have reuse the resolved Servlet name by accident for other | ||
// requests | ||
request.removeAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME); | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onExit( | ||
@Advice.Argument(0) ServletRequest request, | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
if (scope == null) { | ||
return; | ||
} | ||
scope.close(); | ||
|
||
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; | ||
helper().end(context, slingRequest, null, throwable); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...agent/src/main/java/io/opentelemetry/javaagent/instrumentation/sling/SlingSingletons.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,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.sling; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
|
||
public final class SlingSingletons { | ||
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.sling-1.0"; | ||
|
||
static final String REQUEST_ATTR_RESOLVED_SERVLET_NAME = | ||
INSTRUMENTATION_NAME + ".resolvedServletName"; | ||
|
||
private static final SpanNameExtractor<SlingHttpServletRequest> SPAN_NAME_EXTRACTOR = | ||
s -> (String) s.getAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME); | ||
private static final Instrumenter<SlingHttpServletRequest, Void> INSTRUMENTER = | ||
Instrumenter.<SlingHttpServletRequest, Void>builder( | ||
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, SPAN_NAME_EXTRACTOR) | ||
.buildInstrumenter(); | ||
|
||
public static Instrumenter<SlingHttpServletRequest, Void> helper() { | ||
return INSTRUMENTER; | ||
} | ||
|
||
private SlingSingletons() {} | ||
} |
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
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.
I could see this as potentially surprising if it shows up as the "name", but I think it's fine. Hopefully more informative than just the class name when the name is missing from the config.
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.
I could've sworn that I found this pattern in the repository, but I can't find it anymore.