-
Notifications
You must be signed in to change notification settings - Fork 1k
Support Struts 7.0 (fixes CI muzzle failures) #12935
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions
36
instrumentation/struts/struts-7.0/javaagent/build.gradle.kts
This file contains hidden or 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,36 @@ | ||
| plugins { | ||
| id("otel.javaagent-instrumentation") | ||
| } | ||
|
|
||
| muzzle { | ||
| pass { | ||
| group.set("org.apache.struts") | ||
| module.set("struts2-core") | ||
| versions.set("[7.0.0,)") | ||
| assertInverse.set(true) | ||
| } | ||
| } | ||
|
|
||
| // struts 7 requires java 17 | ||
| otelJava { | ||
| minJavaVersionSupported.set(JavaVersion.VERSION_17) | ||
| } | ||
|
|
||
| dependencies { | ||
| bootstrap(project(":instrumentation:servlet:servlet-common:bootstrap")) | ||
|
|
||
| library("org.apache.struts:struts2-core:7.0.0") | ||
|
|
||
| testImplementation(project(":testing-common")) | ||
| testImplementation("org.eclipse.jetty:jetty-server:11.0.0") | ||
| testImplementation("org.eclipse.jetty:jetty-servlet:11.0.0") | ||
| testImplementation("jakarta.servlet:jakarta.servlet-api:5.0.0") | ||
| testImplementation("jakarta.servlet.jsp:jakarta.servlet.jsp-api:3.0.0") | ||
|
|
||
| testInstrumentation(project(":instrumentation:servlet:servlet-5.0:javaagent")) | ||
| testInstrumentation(project(":instrumentation:jetty:jetty-11.0:javaagent")) | ||
| } | ||
|
|
||
| tasks.withType<Test>().configureEach { | ||
| jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true") | ||
| } |
84 changes: 84 additions & 0 deletions
84
...a/io/opentelemetry/javaagent/instrumentation/struts2/ActionInvocationInstrumentation.java
This file contains hidden or 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,84 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.struts2; | ||
|
|
||
| import static io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource.CONTROLLER; | ||
| import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
| import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; | ||
| import static io.opentelemetry.javaagent.instrumentation.struts2.StrutsSingletons.instrumenter; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; | ||
| import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; | ||
| 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.apache.struts2.ActionInvocation; | ||
|
|
||
| public class ActionInvocationInstrumentation implements TypeInstrumentation { | ||
|
|
||
| @Override | ||
| public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
| return hasClassesNamed("org.apache.struts2.ActionInvocation"); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return implementsInterface(named("org.apache.struts2.ActionInvocation")); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| transformer.applyAdviceToMethod( | ||
| isMethod().and(isPublic()).and(named("invokeActionOnly")), | ||
| this.getClass().getName() + "$InvokeActionOnlyAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class InvokeActionOnlyAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter( | ||
| @Advice.This ActionInvocation actionInvocation, | ||
| @Advice.Local("otelContext") Context context, | ||
| @Advice.Local("otelScope") Scope scope) { | ||
| Context parentContext = Java8BytecodeBridge.currentContext(); | ||
|
|
||
| HttpServerRoute.update( | ||
| parentContext, | ||
| CONTROLLER, | ||
| StrutsServerSpanNaming.SERVER_SPAN_NAME, | ||
| actionInvocation.getProxy()); | ||
|
|
||
| if (!instrumenter().shouldStart(parentContext, actionInvocation)) { | ||
| return; | ||
| } | ||
|
|
||
| context = instrumenter().start(parentContext, actionInvocation); | ||
| scope = context.makeCurrent(); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void stopSpan( | ||
| @Advice.Thrown Throwable throwable, | ||
| @Advice.This ActionInvocation actionInvocation, | ||
| @Advice.Local("otelContext") Context context, | ||
| @Advice.Local("otelScope") Scope scope) { | ||
| if (scope == null) { | ||
| return; | ||
| } | ||
| scope.close(); | ||
|
|
||
| instrumenter().end(context, actionInvocation, null, throwable); | ||
| } | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...java/io/opentelemetry/javaagent/instrumentation/struts2/Struts2InstrumentationModule.java
This file contains hidden or 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.struts2; | ||
|
|
||
| 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 Struts2InstrumentationModule extends InstrumentationModule { | ||
|
|
||
| public Struts2InstrumentationModule() { | ||
| super("struts", "struts-7.0"); | ||
| } | ||
|
|
||
| @Override | ||
| public List<TypeInstrumentation> typeInstrumentations() { | ||
| return singletonList(new ActionInvocationInstrumentation()); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...n/java/io/opentelemetry/javaagent/instrumentation/struts2/StrutsCodeAttributesGetter.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.struts2; | ||
|
|
||
| import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; | ||
| import org.apache.struts2.ActionInvocation; | ||
|
|
||
| public class StrutsCodeAttributesGetter implements CodeAttributesGetter<ActionInvocation> { | ||
|
|
||
| @Override | ||
| public Class<?> getCodeClass(ActionInvocation actionInvocation) { | ||
| return actionInvocation.getAction().getClass(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getMethodName(ActionInvocation actionInvocation) { | ||
| return actionInvocation.getProxy().getMethod(); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
.../main/java/io/opentelemetry/javaagent/instrumentation/struts2/StrutsServerSpanNaming.java
This file contains hidden or 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,37 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.struts2; | ||
|
|
||
| import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteGetter; | ||
| import io.opentelemetry.javaagent.bootstrap.servlet.ServletContextPath; | ||
| import org.apache.struts2.ActionProxy; | ||
|
|
||
| public class StrutsServerSpanNaming { | ||
|
|
||
| public static final HttpServerRouteGetter<ActionProxy> SERVER_SPAN_NAME = | ||
| (context, actionProxy) -> { | ||
| // We take name from the config, because it contains the path pattern from the | ||
| // configuration. | ||
| String result = actionProxy.getConfig().getName(); | ||
|
|
||
| String actionNamespace = actionProxy.getNamespace(); | ||
| if (actionNamespace != null && !actionNamespace.isEmpty()) { | ||
| if (actionNamespace.endsWith("/") || result.startsWith("/")) { | ||
| result = actionNamespace + result; | ||
| } else { | ||
| result = actionNamespace + "/" + result; | ||
| } | ||
| } | ||
|
|
||
| if (!result.startsWith("/")) { | ||
| result = "/" + result; | ||
| } | ||
|
|
||
| return ServletContextPath.prepend(context, result); | ||
| }; | ||
|
|
||
| private StrutsServerSpanNaming() {} | ||
| } |
38 changes: 38 additions & 0 deletions
38
...nt/src/main/java/io/opentelemetry/javaagent/instrumentation/struts2/StrutsSingletons.java
This file contains hidden or 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,38 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.struts2; | ||
|
|
||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeSpanNameExtractor; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
| import io.opentelemetry.javaagent.bootstrap.internal.ExperimentalConfig; | ||
| import org.apache.struts2.ActionInvocation; | ||
|
|
||
| public class StrutsSingletons { | ||
| private static final String INSTRUMENTATION_NAME = "io.opentelemetry.struts-7.0"; | ||
|
|
||
| private static final Instrumenter<ActionInvocation, Void> INSTRUMENTER; | ||
|
|
||
| static { | ||
| StrutsCodeAttributesGetter codeAttributesGetter = new StrutsCodeAttributesGetter(); | ||
|
|
||
| INSTRUMENTER = | ||
| Instrumenter.<ActionInvocation, Void>builder( | ||
| GlobalOpenTelemetry.get(), | ||
| INSTRUMENTATION_NAME, | ||
| CodeSpanNameExtractor.create(codeAttributesGetter)) | ||
| .addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter)) | ||
| .setEnabled(ExperimentalConfig.get().controllerTelemetryEnabled()) | ||
| .buildInstrumenter(); | ||
| } | ||
|
|
||
| public static Instrumenter<ActionInvocation, Void> instrumenter() { | ||
| return INSTRUMENTER; | ||
| } | ||
|
|
||
| private StrutsSingletons() {} | ||
| } |
92 changes: 92 additions & 0 deletions
92
...gent/src/test/java/io/opentelemetry/javaagent/instrumentation/struts2/GreetingAction.java
This file contains hidden or 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,92 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.struts2; | ||
|
|
||
| import static io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest.controller; | ||
|
|
||
| import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.apache.struts2.ActionSupport; | ||
| import org.apache.struts2.ServletActionContext; | ||
| import org.apache.struts2.interceptor.parameter.StrutsParameter; | ||
|
|
||
| public class GreetingAction extends ActionSupport { | ||
|
|
||
| String responseBody = "default"; | ||
|
|
||
| public String success() { | ||
| responseBody = controller(ServerEndpoint.SUCCESS, ServerEndpoint.SUCCESS::getBody); | ||
|
|
||
| return "greeting"; | ||
| } | ||
|
|
||
| public String redirect() { | ||
| responseBody = controller(ServerEndpoint.REDIRECT, ServerEndpoint.REDIRECT::getBody); | ||
| return "redirect"; | ||
| } | ||
|
|
||
| public String query_param() { | ||
| responseBody = controller(ServerEndpoint.QUERY_PARAM, ServerEndpoint.QUERY_PARAM::getBody); | ||
| return "greeting"; | ||
| } | ||
|
|
||
| public String error() { | ||
| controller(ServerEndpoint.ERROR, ServerEndpoint.ERROR::getBody); | ||
| return "error"; | ||
| } | ||
|
|
||
| public String exception() { | ||
| controller( | ||
| ServerEndpoint.EXCEPTION, | ||
| () -> { | ||
| throw new IllegalStateException(ServerEndpoint.EXCEPTION.getBody()); | ||
| }); | ||
| throw new AssertionError(); // should not reach here | ||
| } | ||
|
|
||
| public String path_param() { | ||
| controller( | ||
| ServerEndpoint.PATH_PARAM, | ||
| () -> | ||
| "this does nothing, as responseBody is set in setId, but we need this controller span nevertheless"); | ||
| return "greeting"; | ||
| } | ||
|
|
||
| public String indexed_child() { | ||
| responseBody = | ||
| controller( | ||
| ServerEndpoint.INDEXED_CHILD, | ||
| () -> { | ||
| ServerEndpoint.INDEXED_CHILD.collectSpanAttributes( | ||
| (name) -> ServletActionContext.getRequest().getParameter(name)); | ||
| return ServerEndpoint.INDEXED_CHILD.getBody(); | ||
| }); | ||
| return "greeting"; | ||
| } | ||
|
|
||
| public String capture_headers() { | ||
| HttpServletRequest request = ServletActionContext.getRequest(); | ||
| HttpServletResponse response = ServletActionContext.getResponse(); | ||
| response.setHeader("X-Test-Response", request.getHeader("X-Test-Request")); | ||
| responseBody = | ||
| controller(ServerEndpoint.CAPTURE_HEADERS, ServerEndpoint.CAPTURE_HEADERS::getBody); | ||
| return "greeting"; | ||
| } | ||
|
|
||
| public String dispatch_servlet() { | ||
| return "greetingServlet"; | ||
| } | ||
|
|
||
| @StrutsParameter | ||
| public void setId(String id) { | ||
| responseBody = id; | ||
| } | ||
|
|
||
| public String getResponseBody() { | ||
| return responseBody; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
...ent/src/test/java/io/opentelemetry/javaagent/instrumentation/struts2/GreetingServlet.java
This file contains hidden or 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,19 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.struts2; | ||
|
|
||
| import jakarta.servlet.http.HttpServlet; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
|
|
||
| public class GreetingServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
| resp.getWriter().write("greeting"); | ||
| } | ||
| } |
Oops, something went wrong.
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'm not sure why this extra condition is necessary. Since the interface is in different package for struts7 it should clash with the instrumentation for older versions. I'll push to the pr and remove this check if it is needed then please add it back. I'll also move struts 2.3 and 7 instrumentation classes to different packages.
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.
Thanks