Skip to content

Commit 1815558

Browse files
JonasKunzlaurit
andauthored
Move Akka and Armeria virtual fields to helpers (#13604)
Co-authored-by: Lauri Tulmin <[email protected]>
1 parent 81f3795 commit 1815558

File tree

13 files changed

+130
-51
lines changed

13 files changed

+130
-51
lines changed

instrumentation/akka/akka-actor-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkaactor/AkkaActorCellInstrumentation.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import akka.dispatch.Envelope;
1212
import akka.dispatch.sysmsg.SystemMessage;
1313
import io.opentelemetry.context.Scope;
14-
import io.opentelemetry.instrumentation.api.util.VirtualField;
15-
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
1614
import io.opentelemetry.javaagent.bootstrap.executors.TaskAdviceHelper;
1715
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1816
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
@@ -42,9 +40,8 @@ public static class InvokeAdvice {
4240

4341
@Advice.OnMethodEnter(suppress = Throwable.class)
4442
public static Scope enter(@Advice.Argument(0) Envelope envelope) {
45-
VirtualField<Envelope, PropagatedContext> virtualField =
46-
VirtualField.find(Envelope.class, PropagatedContext.class);
47-
return TaskAdviceHelper.makePropagatedContextCurrent(virtualField, envelope);
43+
return TaskAdviceHelper.makePropagatedContextCurrent(
44+
VirtualFields.ENVELOPE_PROPAGATED_CONTEXT, envelope);
4845
}
4946

5047
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
@@ -60,9 +57,8 @@ public static class SystemInvokeAdvice {
6057

6158
@Advice.OnMethodEnter(suppress = Throwable.class)
6259
public static Scope enter(@Advice.Argument(0) SystemMessage systemMessage) {
63-
VirtualField<SystemMessage, PropagatedContext> virtualField =
64-
VirtualField.find(SystemMessage.class, PropagatedContext.class);
65-
return TaskAdviceHelper.makePropagatedContextCurrent(virtualField, systemMessage);
60+
return TaskAdviceHelper.makePropagatedContextCurrent(
61+
VirtualFields.SYSTEM_MESSAGE_PROPAGATED_CONTEXT, systemMessage);
6662
}
6763

6864
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)

instrumentation/akka/akka-actor-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkaactor/AkkaDefaultSystemMessageQueueInstrumentation.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import akka.dispatch.sysmsg.SystemMessage;
1414
import io.opentelemetry.context.Context;
15-
import io.opentelemetry.instrumentation.api.util.VirtualField;
1615
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1716
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper;
1817
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
@@ -49,9 +48,8 @@ public static class DispatchSystemAdvice {
4948
public static PropagatedContext enter(@Advice.Argument(1) SystemMessage systemMessage) {
5049
Context context = Java8BytecodeBridge.currentContext();
5150
if (ExecutorAdviceHelper.shouldPropagateContext(context, systemMessage)) {
52-
VirtualField<SystemMessage, PropagatedContext> virtualField =
53-
VirtualField.find(SystemMessage.class, PropagatedContext.class);
54-
return ExecutorAdviceHelper.attachContextToTask(context, virtualField, systemMessage);
51+
return ExecutorAdviceHelper.attachContextToTask(
52+
context, VirtualFields.SYSTEM_MESSAGE_PROPAGATED_CONTEXT, systemMessage);
5553
}
5654
return null;
5755
}
@@ -61,10 +59,11 @@ public static void exit(
6159
@Advice.Argument(1) SystemMessage systemMessage,
6260
@Advice.Enter PropagatedContext propagatedContext,
6361
@Advice.Thrown Throwable throwable) {
64-
VirtualField<SystemMessage, PropagatedContext> virtualField =
65-
VirtualField.find(SystemMessage.class, PropagatedContext.class);
6662
ExecutorAdviceHelper.cleanUpAfterSubmit(
67-
propagatedContext, throwable, virtualField, systemMessage);
63+
propagatedContext,
64+
throwable,
65+
VirtualFields.SYSTEM_MESSAGE_PROPAGATED_CONTEXT,
66+
systemMessage);
6867
}
6968
}
7069
}

instrumentation/akka/akka-actor-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkaactor/AkkaDispatcherInstrumentation.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import akka.dispatch.Envelope;
1212
import io.opentelemetry.context.Context;
13-
import io.opentelemetry.instrumentation.api.util.VirtualField;
1413
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1514
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper;
1615
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
@@ -43,9 +42,8 @@ public static class DispatchEnvelopeAdvice {
4342
public static PropagatedContext enterDispatch(@Advice.Argument(1) Envelope envelope) {
4443
Context context = Java8BytecodeBridge.currentContext();
4544
if (ExecutorAdviceHelper.shouldPropagateContext(context, envelope.message())) {
46-
VirtualField<Envelope, PropagatedContext> virtualField =
47-
VirtualField.find(Envelope.class, PropagatedContext.class);
48-
return ExecutorAdviceHelper.attachContextToTask(context, virtualField, envelope);
45+
return ExecutorAdviceHelper.attachContextToTask(
46+
context, VirtualFields.ENVELOPE_PROPAGATED_CONTEXT, envelope);
4947
}
5048
return null;
5149
}
@@ -55,9 +53,8 @@ public static void exitDispatch(
5553
@Advice.Argument(1) Envelope envelope,
5654
@Advice.Enter PropagatedContext propagatedContext,
5755
@Advice.Thrown Throwable throwable) {
58-
VirtualField<Envelope, PropagatedContext> virtualField =
59-
VirtualField.find(Envelope.class, PropagatedContext.class);
60-
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, envelope);
56+
ExecutorAdviceHelper.cleanUpAfterSubmit(
57+
propagatedContext, throwable, VirtualFields.ENVELOPE_PROPAGATED_CONTEXT, envelope);
6158
}
6259
}
6360
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.akkaactor;
7+
8+
import akka.dispatch.Envelope;
9+
import akka.dispatch.sysmsg.SystemMessage;
10+
import io.opentelemetry.instrumentation.api.util.VirtualField;
11+
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
12+
13+
public class VirtualFields {
14+
15+
private VirtualFields() {}
16+
17+
public static final VirtualField<Envelope, PropagatedContext> ENVELOPE_PROPAGATED_CONTEXT =
18+
VirtualField.find(Envelope.class, PropagatedContext.class);
19+
public static final VirtualField<SystemMessage, PropagatedContext>
20+
SYSTEM_MESSAGE_PROPAGATED_CONTEXT =
21+
VirtualField.find(SystemMessage.class, PropagatedContext.class);
22+
}

instrumentation/akka/akka-actor-fork-join-2.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkaactor/AkkaForkJoinPoolInstrumentation.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import akka.dispatch.forkjoin.ForkJoinTask;
1212
import io.opentelemetry.context.Context;
13-
import io.opentelemetry.instrumentation.api.util.VirtualField;
1413
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1514
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper;
1615
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
@@ -51,9 +50,8 @@ public static class SetAkkaForkJoinStateAdvice {
5150
public static PropagatedContext enterJobSubmit(@Advice.Argument(0) ForkJoinTask<?> task) {
5251
Context context = Java8BytecodeBridge.currentContext();
5352
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
54-
VirtualField<ForkJoinTask<?>, PropagatedContext> virtualField =
55-
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
56-
return ExecutorAdviceHelper.attachContextToTask(context, virtualField, task);
53+
return ExecutorAdviceHelper.attachContextToTask(
54+
context, VirtualFields.FORK_JOIN_TASK_PROPAGATED_CONTEXT, task);
5755
}
5856
return null;
5957
}
@@ -63,9 +61,8 @@ public static void exitJobSubmit(
6361
@Advice.Argument(0) ForkJoinTask<?> task,
6462
@Advice.Enter PropagatedContext propagatedContext,
6563
@Advice.Thrown Throwable throwable) {
66-
VirtualField<ForkJoinTask<?>, PropagatedContext> virtualField =
67-
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
68-
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
64+
ExecutorAdviceHelper.cleanUpAfterSubmit(
65+
propagatedContext, throwable, VirtualFields.FORK_JOIN_TASK_PROPAGATED_CONTEXT, task);
6966
}
7067
}
7168
}

instrumentation/akka/akka-actor-fork-join-2.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkaactor/AkkaForkJoinTaskInstrumentation.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import akka.dispatch.forkjoin.ForkJoinPool;
1616
import akka.dispatch.forkjoin.ForkJoinTask;
1717
import io.opentelemetry.context.Scope;
18-
import io.opentelemetry.instrumentation.api.util.VirtualField;
19-
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
2018
import io.opentelemetry.javaagent.bootstrap.executors.TaskAdviceHelper;
2119
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2220
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
@@ -62,14 +60,13 @@ public static class ForkJoinTaskAdvice {
6260
*/
6361
@Advice.OnMethodEnter(suppress = Throwable.class)
6462
public static Scope enter(@Advice.This ForkJoinTask<?> thiz) {
65-
VirtualField<ForkJoinTask<?>, PropagatedContext> virtualField =
66-
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
67-
Scope scope = TaskAdviceHelper.makePropagatedContextCurrent(virtualField, thiz);
63+
Scope scope =
64+
TaskAdviceHelper.makePropagatedContextCurrent(
65+
VirtualFields.FORK_JOIN_TASK_PROPAGATED_CONTEXT, thiz);
6866
if (thiz instanceof Runnable) {
69-
VirtualField<Runnable, PropagatedContext> runnableVirtualField =
70-
VirtualField.find(Runnable.class, PropagatedContext.class);
7167
Scope newScope =
72-
TaskAdviceHelper.makePropagatedContextCurrent(runnableVirtualField, (Runnable) thiz);
68+
TaskAdviceHelper.makePropagatedContextCurrent(
69+
VirtualFields.RUNNABLE_PROPAGATED_CONTEXT, (Runnable) thiz);
7370
if (null != newScope) {
7471
if (null != scope) {
7572
newScope.close();
@@ -79,10 +76,9 @@ public static Scope enter(@Advice.This ForkJoinTask<?> thiz) {
7976
}
8077
}
8178
if (thiz instanceof Callable) {
82-
VirtualField<Callable<?>, PropagatedContext> callableVirtualField =
83-
VirtualField.find(Callable.class, PropagatedContext.class);
8479
Scope newScope =
85-
TaskAdviceHelper.makePropagatedContextCurrent(callableVirtualField, (Callable<?>) thiz);
80+
TaskAdviceHelper.makePropagatedContextCurrent(
81+
VirtualFields.CALLABLE_PROPAGATED_CONTEXT, (Callable<?>) thiz);
8682
if (null != newScope) {
8783
if (null != scope) {
8884
newScope.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.akkaactor;
7+
8+
import akka.dispatch.forkjoin.ForkJoinTask;
9+
import io.opentelemetry.instrumentation.api.util.VirtualField;
10+
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
11+
import java.util.concurrent.Callable;
12+
13+
public class VirtualFields {
14+
15+
private VirtualFields() {}
16+
17+
public static final VirtualField<ForkJoinTask<?>, PropagatedContext>
18+
FORK_JOIN_TASK_PROPAGATED_CONTEXT =
19+
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
20+
public static final VirtualField<Runnable, PropagatedContext> RUNNABLE_PROPAGATED_CONTEXT =
21+
VirtualField.find(Runnable.class, PropagatedContext.class);
22+
public static final VirtualField<Callable<?>, PropagatedContext> CALLABLE_PROPAGATED_CONTEXT =
23+
VirtualField.find(Callable.class, PropagatedContext.class);
24+
}

instrumentation/akka/akka-http-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkahttp/server/route/PathMatcherInstrumentation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import akka.http.scaladsl.model.Uri;
1313
import akka.http.scaladsl.server.PathMatcher;
14-
import io.opentelemetry.instrumentation.api.util.VirtualField;
1514
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1615
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1716
import net.bytebuddy.asm.Advice;
@@ -41,7 +40,7 @@ public static void onEnter(
4140
@Advice.Argument(0) Uri.Path prefix, @Advice.Return PathMatcher<?> result) {
4241
// store the path being matched inside a VirtualField on the given matcher, so it can be used
4342
// for constructing the route
44-
VirtualField.find(PathMatcher.class, String.class).set(result, prefix.toString());
43+
PathMatcherUtil.setMatched(result, prefix.toString());
4544
}
4645
}
4746
}

instrumentation/akka/akka-http-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkahttp/server/route/PathMatcherStaticInstrumentation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import akka.http.scaladsl.model.Uri;
1313
import akka.http.scaladsl.server.PathMatcher;
1414
import akka.http.scaladsl.server.PathMatchers;
15-
import io.opentelemetry.instrumentation.api.util.VirtualField;
1615
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1716
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1817
import net.bytebuddy.asm.Advice;
@@ -48,7 +47,7 @@ public static void onExit(
4847
}
4948
// if present use the matched path that was remembered in PathMatcherInstrumentation,
5049
// otherwise just use a *
51-
String prefix = VirtualField.find(PathMatcher.class, String.class).get(pathMatcher);
50+
String prefix = PathMatcherUtil.getMatched(pathMatcher);
5251
if (prefix == null) {
5352
if (PathMatchers.Slash$.class == pathMatcher.getClass()) {
5453
prefix = "/";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.akkahttp.server.route;
7+
8+
import akka.http.scaladsl.server.PathMatcher;
9+
import io.opentelemetry.instrumentation.api.util.VirtualField;
10+
11+
public class PathMatcherUtil {
12+
13+
private static final VirtualField<PathMatcher<?>, String> PATH_MATCHER_ROUTE =
14+
VirtualField.find(PathMatcher.class, String.class);
15+
16+
public static void setMatched(PathMatcher<?> matcher, String route) {
17+
PATH_MATCHER_ROUTE.set(matcher, route);
18+
}
19+
20+
public static String getMatched(PathMatcher<?> matcher) {
21+
return PATH_MATCHER_ROUTE.get(matcher);
22+
}
23+
24+
private PathMatcherUtil() {}
25+
}

0 commit comments

Comments
 (0)