-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Struts 7.0 (fixes CI muzzle failures) (#12935)
Co-authored-by: Lauri Tulmin <[email protected]>
- Loading branch information
Showing
24 changed files
with
582 additions
and
21 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
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
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
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
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
File renamed without changes.
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
37 changes: 37 additions & 0 deletions
37
instrumentation/struts/struts-7.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,37 @@ | ||
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")) | ||
testInstrumentation(project(":instrumentation:struts:struts-2.3:javaagent")) | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true") | ||
} |
84 changes: 84 additions & 0 deletions
84
.../opentelemetry/javaagent/instrumentation/struts/v7_0/ActionInvocationInstrumentation.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,84 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.struts.v7_0; | ||
|
||
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.struts.v7_0.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
.../io/opentelemetry/javaagent/instrumentation/struts/v7_0/Struts2InstrumentationModule.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.struts.v7_0; | ||
|
||
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
...va/io/opentelemetry/javaagent/instrumentation/struts/v7_0/StrutsCodeAttributesGetter.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,22 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.struts.v7_0; | ||
|
||
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
...n/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/StrutsServerSpanNaming.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,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.struts.v7_0; | ||
|
||
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
...rc/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/StrutsSingletons.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,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.struts.v7_0; | ||
|
||
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() {} | ||
} |
Oops, something went wrong.