Skip to content

Commit 0fce910

Browse files
authored
make struts indy-ready (#14861)
1 parent 96e710a commit 0fce910

File tree

4 files changed

+86
-50
lines changed

4 files changed

+86
-50
lines changed

instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/ActionInvocationInstrumentation.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import io.opentelemetry.context.Context;
1818
import io.opentelemetry.context.Scope;
1919
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
20-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2120
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2221
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
22+
import javax.annotation.Nullable;
2323
import net.bytebuddy.asm.Advice;
2424
import net.bytebuddy.description.type.TypeDescription;
2525
import net.bytebuddy.matcher.ElementMatcher;
@@ -46,39 +46,50 @@ public void transform(TypeTransformer transformer) {
4646
@SuppressWarnings("unused")
4747
public static class InvokeActionOnlyAdvice {
4848

49-
@Advice.OnMethodEnter(suppress = Throwable.class)
50-
public static void onEnter(
51-
@Advice.This ActionInvocation actionInvocation,
52-
@Advice.Local("otelContext") Context context,
53-
@Advice.Local("otelScope") Scope scope) {
54-
Context parentContext = Java8BytecodeBridge.currentContext();
49+
public static class AdviceScope {
50+
private final Context context;
51+
private final Scope scope;
5552

56-
HttpServerRoute.update(
57-
parentContext,
58-
CONTROLLER,
59-
StrutsServerSpanNaming.SERVER_SPAN_NAME,
60-
actionInvocation.getProxy());
53+
public AdviceScope(Context context, Scope scope) {
54+
this.context = context;
55+
this.scope = scope;
56+
}
6157

62-
if (!instrumenter().shouldStart(parentContext, actionInvocation)) {
63-
return;
58+
@Nullable
59+
public static AdviceScope start(ActionInvocation actionInvocation) {
60+
Context parentContext = Context.current();
61+
HttpServerRoute.update(
62+
parentContext,
63+
CONTROLLER,
64+
StrutsServerSpanNaming.SERVER_SPAN_NAME,
65+
actionInvocation.getProxy());
66+
67+
if (!instrumenter().shouldStart(parentContext, actionInvocation)) {
68+
return null;
69+
}
70+
Context context = instrumenter().start(parentContext, actionInvocation);
71+
return new AdviceScope(context, context.makeCurrent());
6472
}
6573

66-
context = instrumenter().start(parentContext, actionInvocation);
67-
scope = context.makeCurrent();
74+
public void end(ActionInvocation actionInvocation, @Nullable Throwable throwable) {
75+
scope.close();
76+
instrumenter().end(context, actionInvocation, null, throwable);
77+
}
78+
}
79+
80+
@Advice.OnMethodEnter(suppress = Throwable.class)
81+
public static AdviceScope onEnter(@Advice.This ActionInvocation actionInvocation) {
82+
return AdviceScope.start(actionInvocation);
6883
}
6984

7085
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
7186
public static void stopSpan(
72-
@Advice.Thrown Throwable throwable,
87+
@Advice.Thrown @Nullable Throwable throwable,
7388
@Advice.This ActionInvocation actionInvocation,
74-
@Advice.Local("otelContext") Context context,
75-
@Advice.Local("otelScope") Scope scope) {
76-
if (scope == null) {
77-
return;
89+
@Advice.Enter @Nullable AdviceScope adviceScope) {
90+
if (adviceScope != null) {
91+
adviceScope.end(actionInvocation, throwable);
7892
}
79-
scope.close();
80-
81-
instrumenter().end(context, actionInvocation, null, throwable);
8293
}
8394
}
8495
}

instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/Struts2InstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import java.util.List;
1415

1516
@AutoService(InstrumentationModule.class)
16-
public class Struts2InstrumentationModule extends InstrumentationModule {
17+
public class Struts2InstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719

1820
public Struts2InstrumentationModule() {
1921
super("struts", "struts-2.3");
@@ -23,4 +25,9 @@ public Struts2InstrumentationModule() {
2325
public List<TypeInstrumentation> typeInstrumentations() {
2426
return singletonList(new ActionInvocationInstrumentation());
2527
}
28+
29+
@Override
30+
public boolean isIndyReady() {
31+
return true;
32+
}
2633
}

instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/ActionInvocationInstrumentation.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
import io.opentelemetry.context.Context;
1717
import io.opentelemetry.context.Scope;
1818
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
19-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2019
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2120
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
21+
import javax.annotation.Nullable;
2222
import net.bytebuddy.asm.Advice;
2323
import net.bytebuddy.description.type.TypeDescription;
2424
import net.bytebuddy.matcher.ElementMatcher;
@@ -46,39 +46,50 @@ public void transform(TypeTransformer transformer) {
4646
@SuppressWarnings("unused")
4747
public static class InvokeActionOnlyAdvice {
4848

49-
@Advice.OnMethodEnter(suppress = Throwable.class)
50-
public static void onEnter(
51-
@Advice.This ActionInvocation actionInvocation,
52-
@Advice.Local("otelContext") Context context,
53-
@Advice.Local("otelScope") Scope scope) {
54-
Context parentContext = Java8BytecodeBridge.currentContext();
49+
public static class AdviceScope {
50+
private final Context context;
51+
private final Scope scope;
5552

56-
HttpServerRoute.update(
57-
parentContext,
58-
CONTROLLER,
59-
StrutsServerSpanNaming.SERVER_SPAN_NAME,
60-
actionInvocation.getProxy());
53+
public AdviceScope(Context context, Scope scope) {
54+
this.context = context;
55+
this.scope = scope;
56+
}
6157

62-
if (!instrumenter().shouldStart(parentContext, actionInvocation)) {
63-
return;
58+
@Nullable
59+
public static AdviceScope start(ActionInvocation actionInvocation) {
60+
Context parentContext = Context.current();
61+
HttpServerRoute.update(
62+
parentContext,
63+
CONTROLLER,
64+
StrutsServerSpanNaming.SERVER_SPAN_NAME,
65+
actionInvocation.getProxy());
66+
67+
if (!instrumenter().shouldStart(parentContext, actionInvocation)) {
68+
return null;
69+
}
70+
Context context = instrumenter().start(parentContext, actionInvocation);
71+
return new AdviceScope(context, context.makeCurrent());
6472
}
6573

66-
context = instrumenter().start(parentContext, actionInvocation);
67-
scope = context.makeCurrent();
74+
public void end(ActionInvocation actionInvocation, @Nullable Throwable throwable) {
75+
scope.close();
76+
instrumenter().end(context, actionInvocation, null, throwable);
77+
}
78+
}
79+
80+
@Advice.OnMethodEnter(suppress = Throwable.class)
81+
public static AdviceScope onEnter(@Advice.This ActionInvocation actionInvocation) {
82+
return AdviceScope.start(actionInvocation);
6883
}
6984

7085
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
7186
public static void stopSpan(
72-
@Advice.Thrown Throwable throwable,
87+
@Advice.Thrown @Nullable Throwable throwable,
7388
@Advice.This ActionInvocation actionInvocation,
74-
@Advice.Local("otelContext") Context context,
75-
@Advice.Local("otelScope") Scope scope) {
76-
if (scope == null) {
77-
return;
89+
@Advice.Enter @Nullable AdviceScope adviceScope) {
90+
if (adviceScope != null) {
91+
adviceScope.end(actionInvocation, throwable);
7892
}
79-
scope.close();
80-
81-
instrumenter().end(context, actionInvocation, null, throwable);
8293
}
8394
}
8495
}

instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/Struts2InstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import com.google.auto.service.AutoService;
1212
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1313
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
14+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1415
import java.util.List;
1516
import net.bytebuddy.matcher.ElementMatcher;
1617

1718
@AutoService(InstrumentationModule.class)
18-
public class Struts2InstrumentationModule extends InstrumentationModule {
19+
public class Struts2InstrumentationModule extends InstrumentationModule
20+
implements ExperimentalInstrumentationModule {
1921

2022
public Struts2InstrumentationModule() {
2123
super("struts", "struts-7.0");
@@ -31,4 +33,9 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
3133
public List<TypeInstrumentation> typeInstrumentations() {
3234
return singletonList(new ActionInvocationInstrumentation());
3335
}
36+
37+
@Override
38+
public boolean isIndyReady() {
39+
return true;
40+
}
3441
}

0 commit comments

Comments
 (0)