Skip to content
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
Expand Up @@ -6,7 +6,8 @@ muzzle {
pass {
group.set("org.apache.struts")
module.set("struts2-core")
versions.set("[2.3.1,)")
versions.set("[2.1.0,7)")
assertInverse.set(true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;

import com.opensymphony.xwork2.ActionInvocation;
import io.opentelemetry.context.Context;
Expand All @@ -28,7 +29,8 @@ public class ActionInvocationInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<ClassLoader> classLoaderOptimization() {
return hasClassesNamed("com.opensymphony.xwork2.ActionInvocation");
return hasClassesNamed("com.opensymphony.xwork2.ActionInvocation")
.and(not(hasClassesNamed("org.apache.struts2.ActionInvocation")));
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks

}

@Override
Expand Down
36 changes: 36 additions & 0 deletions instrumentation/struts/struts-7.0/javaagent/build.gradle.kts
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")
}
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);
}
}
}
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());
}
}
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();
}
}
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() {}
}
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() {}
}
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;
}
}
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");
}
}
Loading
Loading