|
19 | 19 | import io.opentelemetry.context.Scope; |
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
21 | 21 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 22 | +import javax.annotation.Nullable; |
22 | 23 | import net.bytebuddy.asm.Advice; |
23 | 24 | import net.bytebuddy.description.type.TypeDescription; |
24 | 25 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -61,76 +62,83 @@ public void transform(TypeTransformer transformer) { |
61 | 62 | this.getClass().getName() + "$QueryAdvice"); |
62 | 63 | } |
63 | 64 |
|
64 | | - @SuppressWarnings("unused") |
65 | | - public static class SimpleAdvice { |
| 65 | + public static class AdviceScope { |
| 66 | + public GeodeRequest request; |
| 67 | + public Context context; |
| 68 | + public Scope scope; |
66 | 69 |
|
67 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
68 | | - public static void onEnter( |
69 | | - @Advice.This Region<?, ?> region, |
70 | | - @Advice.Origin("#m") String methodName, |
71 | | - @Advice.Local("otelRequest") GeodeRequest request, |
72 | | - @Advice.Local("otelContext") Context context, |
73 | | - @Advice.Local("otelScope") Scope scope) { |
| 70 | + public AdviceScope(GeodeRequest request, Context context, Scope scope) { |
| 71 | + this.request = request; |
| 72 | + this.context = context; |
| 73 | + this.scope = scope; |
| 74 | + } |
74 | 75 |
|
| 76 | + @Nullable |
| 77 | + public static AdviceScope start( |
| 78 | + Region<?, ?> region, String methodName, @Nullable String query) { |
75 | 79 | Context parentContext = currentContext(); |
76 | | - request = GeodeRequest.create(region, methodName, null); |
| 80 | + GeodeRequest request = GeodeRequest.create(region, methodName, query); |
77 | 81 | if (!instrumenter().shouldStart(parentContext, request)) { |
78 | | - return; |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + Context context = instrumenter().start(parentContext, request); |
| 86 | + return new AdviceScope(request, context, context.makeCurrent()); |
| 87 | + } |
| 88 | + |
| 89 | + public void end(@Nullable Throwable throwable) { |
| 90 | + if (scope != null) { |
| 91 | + scope.close(); |
79 | 92 | } |
| 93 | + instrumenter().end(context, request, null, throwable); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @SuppressWarnings("unused") |
| 98 | + public static class SimpleAdvice { |
80 | 99 |
|
81 | | - context = instrumenter().start(parentContext, request); |
82 | | - scope = context.makeCurrent(); |
| 100 | + public static class AdviceLocals { |
| 101 | + public GeodeRequest request; |
| 102 | + public Context context; |
| 103 | + public Scope scope; |
| 104 | + } |
| 105 | + |
| 106 | + @Nullable |
| 107 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 108 | + public static AdviceScope onEnter( |
| 109 | + @Advice.This Region<?, ?> region, @Advice.Origin("#m") String methodName) { |
| 110 | + return AdviceScope.start(region, methodName, null); |
83 | 111 | } |
84 | 112 |
|
85 | 113 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
86 | 114 | public static void stopSpan( |
87 | | - @Advice.Thrown Throwable throwable, |
88 | | - @Advice.Local("otelRequest") GeodeRequest request, |
89 | | - @Advice.Local("otelContext") Context context, |
90 | | - @Advice.Local("otelScope") Scope scope) { |
91 | | - if (scope == null) { |
92 | | - return; |
| 115 | + @Advice.Thrown @Nullable Throwable throwable, |
| 116 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 117 | + if (adviceScope != null) { |
| 118 | + adviceScope.end(throwable); |
93 | 119 | } |
94 | | - |
95 | | - scope.close(); |
96 | | - instrumenter().end(context, request, null, throwable); |
97 | 120 | } |
98 | 121 | } |
99 | 122 |
|
100 | 123 | @SuppressWarnings("unused") |
101 | 124 | public static class QueryAdvice { |
102 | 125 |
|
| 126 | + @Nullable |
103 | 127 | @Advice.OnMethodEnter(suppress = Throwable.class) |
104 | | - public static void onEnter( |
| 128 | + public static AdviceScope onEnter( |
105 | 129 | @Advice.This Region<?, ?> region, |
106 | 130 | @Advice.Origin("#m") String methodName, |
107 | | - @Advice.Argument(0) String query, |
108 | | - @Advice.Local("otelRequest") GeodeRequest request, |
109 | | - @Advice.Local("otelContext") Context context, |
110 | | - @Advice.Local("otelScope") Scope scope) { |
111 | | - |
112 | | - Context parentContext = currentContext(); |
113 | | - request = GeodeRequest.create(region, methodName, query); |
114 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
115 | | - return; |
116 | | - } |
117 | | - |
118 | | - context = instrumenter().start(parentContext, request); |
119 | | - scope = context.makeCurrent(); |
| 131 | + @Advice.Argument(0) String query) { |
| 132 | + return AdviceScope.start(region, methodName, query); |
120 | 133 | } |
121 | 134 |
|
122 | 135 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
123 | 136 | public static void stopSpan( |
124 | | - @Advice.Thrown Throwable throwable, |
125 | | - @Advice.Local("otelRequest") GeodeRequest request, |
126 | | - @Advice.Local("otelContext") Context context, |
127 | | - @Advice.Local("otelScope") Scope scope) { |
128 | | - if (scope == null) { |
129 | | - return; |
| 137 | + @Advice.Thrown @Nullable Throwable throwable, |
| 138 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 139 | + if (adviceScope != null) { |
| 140 | + adviceScope.end(throwable); |
130 | 141 | } |
131 | | - |
132 | | - scope.close(); |
133 | | - instrumenter().end(context, request, null, throwable); |
134 | 142 | } |
135 | 143 | } |
136 | 144 | } |
0 commit comments