Skip to content

Commit d0d39b8

Browse files
SylvainJugeJonasKunztrask
authored
make netty-* indy compatible (#11559)
Co-authored-by: Jonas Kunz <[email protected]> Co-authored-by: Trask Stalnaker <[email protected]>
1 parent d47289a commit d0d39b8

File tree

22 files changed

+283
-160
lines changed

22 files changed

+283
-160
lines changed

instrumentation/finagle-http-23.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finaglehttp/v23_11/ChannelTransportInstrumentation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ public void transform(TypeTransformer transformer) {
3434
public static class WriteAdvice {
3535

3636
@Advice.OnMethodEnter(suppress = Throwable.class)
37-
public static void methodEnter(@Advice.Local("otelScope") Scope scope) {
37+
public static Scope methodEnter() {
3838
Option<Context> ref = Helpers.CONTEXT_LOCAL.apply();
3939
if (ref.isDefined()) {
40-
scope = ref.get().makeCurrent();
40+
return ref.get().makeCurrent();
4141
}
42+
return null;
4243
}
4344

4445
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
45-
public static void methodExit(
46-
@Advice.Local("otelScope") Scope scope, @Advice.Thrown Throwable thrown) {
46+
public static void methodExit(@Advice.Enter Scope scope, @Advice.Thrown Throwable thrown) {
4747
if (scope != null) {
4848
scope.close();
4949
}

instrumentation/finagle-http-23.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finaglehttp/v23_11/FinagleHttpInstrumentationModule.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import com.google.auto.service.AutoService;
99
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1010
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
11+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1112
import java.util.Arrays;
1213
import java.util.List;
1314

1415
@AutoService(InstrumentationModule.class)
15-
public class FinagleHttpInstrumentationModule extends InstrumentationModule {
16+
public class FinagleHttpInstrumentationModule extends InstrumentationModule
17+
implements ExperimentalInstrumentationModule {
1618

1719
public FinagleHttpInstrumentationModule() {
1820
super("finagle-http", "finagle-http-23.11");
@@ -27,9 +29,17 @@ public List<TypeInstrumentation> typeInstrumentations() {
2729
}
2830

2931
@Override
30-
public boolean isIndyModule() {
31-
// injects helpers to access package-private members
32-
return false;
32+
public String getModuleGroup() {
33+
// relies on netty and needs access to common netty instrumentation classes
34+
return "netty";
35+
}
36+
37+
@Override
38+
public List<String> injectedClassNames() {
39+
// these are injected so that they can access package-private members
40+
return Arrays.asList(
41+
"com.twitter.finagle.ChannelTransportHelpers",
42+
"io.netty.channel.OpenTelemetryChannelInitializerDelegate");
3343
}
3444

3545
@Override

instrumentation/finagle-http-23.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finaglehttp/v23_11/H2StreamChannelInitInstrumentation.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,21 @@ public void transform(TypeTransformer transformer) {
4242
public static class InitServerAdvice {
4343

4444
@Advice.OnMethodExit
45-
public static void handleExit(
46-
@Advice.Return(readOnly = false) ChannelInitializer<Channel> initializer) {
47-
initializer = Helpers.wrapServer(initializer);
45+
@Advice.AssignReturned.ToReturned
46+
public static ChannelInitializer<Channel> handleExit(
47+
@Advice.Return ChannelInitializer<Channel> initializer) {
48+
return Helpers.wrapServer(initializer);
4849
}
4950
}
5051

5152
@SuppressWarnings("unused")
5253
public static class InitClientAdvice {
5354

5455
@Advice.OnMethodExit
55-
public static void handleExit(
56-
@Advice.Return(readOnly = false) ChannelInitializer<Channel> initializer) {
57-
initializer = Helpers.wrapClient(initializer);
56+
@Advice.AssignReturned.ToReturned
57+
public static ChannelInitializer<Channel> handleExit(
58+
@Advice.Return ChannelInitializer<Channel> initializer) {
59+
return Helpers.wrapClient(initializer);
5860
}
5961
}
6062
}

instrumentation/finagle-http-23.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finaglehttp/v23_11/LocalSchedulerActivationInstrumentation.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1515
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1616
import net.bytebuddy.asm.Advice;
17+
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
1718
import net.bytebuddy.description.type.TypeDescription;
1819
import net.bytebuddy.matcher.ElementMatcher;
1920

@@ -35,13 +36,14 @@ public void transform(TypeTransformer transformer) {
3536
public static class WrapRunnableAdvice {
3637

3738
@Advice.OnMethodEnter(suppress = Throwable.class)
38-
public static void wrap(@Advice.Argument(value = 0, readOnly = false) Runnable task) {
39+
@Advice.AssignReturned.ToArguments(@ToArgument(0))
40+
public static Runnable wrap(@Advice.Argument(value = 0) Runnable task) {
3941
if (task == null) {
40-
return;
42+
return null;
4143
}
4244

4345
Context context = Java8BytecodeBridge.currentContext();
44-
task = ContextPropagatingRunnable.propagateContext(task, context);
46+
return ContextPropagatingRunnable.propagateContext(task, context);
4547
}
4648
}
4749
}

instrumentation/finagle-http-23.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finaglehttp/v23_11/PromiseMonitoredInstrumentation.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1313
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1414
import net.bytebuddy.asm.Advice;
15+
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
1516
import net.bytebuddy.description.type.TypeDescription;
1617
import net.bytebuddy.matcher.ElementMatcher;
1718
import scala.Function1;
@@ -34,13 +35,13 @@ public void transform(TypeTransformer transformer) {
3435
public static class WrapFunctionAdvice {
3536

3637
@Advice.OnMethodEnter(suppress = Throwable.class)
37-
public static void wrap(
38-
@Advice.Argument(value = 1, readOnly = false) Function1<?, ?> function1) {
38+
@Advice.AssignReturned.ToArguments(@ToArgument(1))
39+
public static Function1<?, ?> wrap(@Advice.Argument(value = 1) Function1<?, ?> function1) {
3940
if (function1 == null) {
40-
return;
41+
return null;
4142
}
4243

43-
function1 = Function1Wrapper.wrap(function1);
44+
return Function1Wrapper.wrap(function1);
4445
}
4546
}
4647
}

instrumentation/finagle-http-23.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finaglehttp/v23_11/TwitterUtilCoreInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@
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 TwitterUtilCoreInstrumentationModule extends InstrumentationModule {
17+
public class TwitterUtilCoreInstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719

1820
public TwitterUtilCoreInstrumentationModule() {
1921
super("twitter-util-core");
2022
}
2123

24+
@Override
25+
public String getModuleGroup() {
26+
return "netty";
27+
}
28+
2229
@Override
2330
public List<TypeInstrumentation> typeInstrumentations() {
2431
return asList(

instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/NettyChannelInstrumentation.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,55 +56,54 @@ public void transform(TypeTransformer transformer) {
5656
public static class ChannelConnectAdvice {
5757

5858
@Advice.OnMethodEnter(suppress = Throwable.class)
59-
public static void onEnter(
60-
@Advice.This Channel channel,
61-
@Advice.Argument(0) SocketAddress remoteAddress,
62-
@Advice.Local("otelParentContext") Context parentContext,
63-
@Advice.Local("otelRequest") NettyConnectionRequest request,
64-
@Advice.Local("otelTimer") Timer timer) {
65-
66-
parentContext = Java8BytecodeBridge.currentContext();
59+
public static NettyScope onEnter(
60+
@Advice.This Channel channel, @Advice.Argument(0) SocketAddress remoteAddress) {
61+
62+
Context parentContext = Java8BytecodeBridge.currentContext();
6763
Span span = Java8BytecodeBridge.spanFromContext(parentContext);
6864
if (!span.getSpanContext().isValid()) {
69-
return;
65+
return null;
7066
}
7167

7268
VirtualField<Channel, NettyConnectionContext> virtualField =
7369
VirtualField.find(Channel.class, NettyConnectionContext.class);
7470
if (virtualField.get(channel) != null) {
75-
return;
71+
return null;
7672
}
7773
virtualField.set(channel, new NettyConnectionContext(parentContext));
7874

79-
request = NettyConnectionRequest.connect(remoteAddress);
80-
timer = Timer.start();
75+
NettyConnectionRequest request = NettyConnectionRequest.connect(remoteAddress);
76+
Timer timer = Timer.start();
77+
78+
return new NettyScope(parentContext, request, timer);
8179
}
8280

8381
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
8482
public static void onExit(
8583
@Advice.Return ChannelFuture channelFuture,
8684
@Advice.Thrown Throwable error,
87-
@Advice.Local("otelParentContext") Context parentContext,
88-
@Advice.Local("otelRequest") NettyConnectionRequest request,
89-
@Advice.Local("otelTimer") Timer timer) {
85+
@Advice.Enter NettyScope nettyScope) {
9086

91-
if (request == null) {
87+
if (nettyScope == null) {
9288
return;
9389
}
9490

9591
if (error != null) {
96-
if (connectionInstrumenter().shouldStart(parentContext, request)) {
92+
if (connectionInstrumenter()
93+
.shouldStart(nettyScope.getParentContext(), nettyScope.getRequest())) {
9794
InstrumenterUtil.startAndEnd(
9895
connectionInstrumenter(),
99-
parentContext,
100-
request,
96+
nettyScope.getParentContext(),
97+
nettyScope.getRequest(),
10198
null,
10299
error,
103-
timer.startTime(),
104-
timer.now());
100+
nettyScope.getTimer().startTime(),
101+
nettyScope.getTimer().now());
105102
}
106103
} else {
107-
channelFuture.addListener(new ConnectionListener(parentContext, request, timer));
104+
channelFuture.addListener(
105+
new ConnectionListener(
106+
nettyScope.getParentContext(), nettyScope.getRequest(), nettyScope.getTimer()));
108107
}
109108
}
110109
}

instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/NettyChannelPipelineInstrumentation.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,25 @@ public void transform(TypeTransformer transformer) {
5151
public static class ChannelPipelineAdd2ArgsAdvice {
5252

5353
@Advice.OnMethodEnter
54-
public static void checkDepth(
55-
@Advice.This ChannelPipeline pipeline,
56-
@Advice.Argument(1) ChannelHandler handler,
57-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
54+
public static CallDepth checkDepth(
55+
@Advice.This ChannelPipeline pipeline, @Advice.Argument(1) ChannelHandler handler) {
5856
// Pipelines are created once as a factory and then copied multiple times using the same add
5957
// methods as we are hooking. If our handler has already been added we need to remove it so we
6058
// don't end up with duplicates (this throws an exception)
6159
if (pipeline.get(handler.getClass().getName()) != null) {
6260
pipeline.remove(handler.getClass().getName());
6361
}
64-
callDepth = CallDepth.forClass(ChannelPipeline.class);
62+
CallDepth callDepth = CallDepth.forClass(ChannelPipeline.class);
6563
callDepth.getAndIncrement();
64+
return callDepth;
6665
}
6766

6867
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6968
public static void addHandler(
7069
@Advice.This ChannelPipeline pipeline,
7170
@Advice.Argument(1) ChannelHandler handler,
72-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
71+
@Advice.Enter CallDepth callDepth) {
72+
7373
if (callDepth.decrementAndGet() > 0) {
7474
return;
7575
}
@@ -82,29 +82,28 @@ public static void addHandler(
8282
public static class ChannelPipelineAdd3ArgsAdvice {
8383

8484
@Advice.OnMethodEnter
85-
public static void checkDepth(
86-
@Advice.This ChannelPipeline pipeline,
87-
@Advice.Argument(2) ChannelHandler handler,
88-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
85+
public static CallDepth checkDepth(
86+
@Advice.This ChannelPipeline pipeline, @Advice.Argument(2) ChannelHandler handler) {
8987
// Pipelines are created once as a factory and then copied multiple times using the same add
9088
// methods as we are hooking. If our handler has already been added we need to remove it so we
9189
// don't end up with duplicates (this throws an exception)
9290
if (pipeline.get(handler.getClass().getName()) != null) {
9391
pipeline.remove(handler.getClass().getName());
9492
}
95-
callDepth = CallDepth.forClass(ChannelPipeline.class);
93+
CallDepth callDepth = CallDepth.forClass(ChannelPipeline.class);
9694
callDepth.getAndIncrement();
95+
return callDepth;
9796
}
9897

9998
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
10099
public static void addHandler(
101100
@Advice.This ChannelPipeline pipeline,
102101
@Advice.Argument(2) ChannelHandler handler,
103-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
102+
@Advice.Enter CallDepth callDepth) {
103+
104104
if (callDepth.decrementAndGet() > 0) {
105105
return;
106106
}
107-
108107
ChannelPipelineUtil.wrapHandler(pipeline, handler);
109108
}
110109
}

instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/NettyInstrumentationModule.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 NettyInstrumentationModule extends InstrumentationModule {
19+
public class NettyInstrumentationModule extends InstrumentationModule
20+
implements ExperimentalInstrumentationModule {
1921
public NettyInstrumentationModule() {
2022
super("netty", "netty-3.8");
2123
}
@@ -25,6 +27,11 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
2527
return hasClassesNamed("org.jboss.netty.handler.codec.http.HttpMessage");
2628
}
2729

30+
@Override
31+
public String getModuleGroup() {
32+
return "netty";
33+
}
34+
2835
@Override
2936
public List<TypeInstrumentation> typeInstrumentations() {
3037
return asList(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.netty.v3_8;
7+
8+
import io.opentelemetry.context.Context;
9+
import io.opentelemetry.instrumentation.netty.common.internal.NettyConnectionRequest;
10+
import io.opentelemetry.instrumentation.netty.common.internal.Timer;
11+
12+
/** Container used to carry state between enter and exit advices */
13+
public class NettyScope {
14+
15+
private final Context parentContext;
16+
private final NettyConnectionRequest request;
17+
private final Timer timer;
18+
19+
public NettyScope(Context parentContext, NettyConnectionRequest request, Timer timer) {
20+
this.parentContext = parentContext;
21+
this.request = request;
22+
this.timer = timer;
23+
}
24+
25+
public Context getParentContext() {
26+
return parentContext;
27+
}
28+
29+
public NettyConnectionRequest getRequest() {
30+
return request;
31+
}
32+
33+
public Timer getTimer() {
34+
return timer;
35+
}
36+
}

0 commit comments

Comments
 (0)