|
22 | 22 | import io.vertx.core.impl.future.PromiseInternal; |
23 | 23 | import io.vertx.sqlclient.impl.PreparedStatement; |
24 | 24 | import io.vertx.sqlclient.impl.QueryExecutorUtil; |
| 25 | +import javax.annotation.Nullable; |
25 | 26 | import net.bytebuddy.asm.Advice; |
26 | 27 | import net.bytebuddy.description.type.TypeDescription; |
27 | 28 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -54,71 +55,93 @@ public static void onExit(@Advice.This Object queryExecutor) { |
54 | 55 |
|
55 | 56 | @SuppressWarnings("unused") |
56 | 57 | public static class QueryAdvice { |
57 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
58 | | - public static void onEnter( |
59 | | - @Advice.This Object queryExecutor, |
60 | | - @Advice.AllArguments Object[] arguments, |
61 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
62 | | - @Advice.Local("otelRequest") VertxSqlClientRequest otelRequest, |
63 | | - @Advice.Local("otelContext") Context context, |
64 | | - @Advice.Local("otelScope") Scope scope) { |
65 | | - callDepth = CallDepth.forClass(queryExecutor.getClass()); |
66 | | - if (callDepth.getAndIncrement() > 0) { |
67 | | - return; |
| 58 | + |
| 59 | + public static class AdviceScope { |
| 60 | + private final CallDepth callDepth; |
| 61 | + @Nullable private final VertxSqlClientRequest otelRequest; |
| 62 | + @Nullable private final Context context; |
| 63 | + @Nullable private final Scope scope; |
| 64 | + |
| 65 | + private AdviceScope( |
| 66 | + CallDepth callDepth, |
| 67 | + @Nullable VertxSqlClientRequest otelRequest, |
| 68 | + @Nullable Context context, |
| 69 | + @Nullable Scope scope) { |
| 70 | + this.callDepth = callDepth; |
| 71 | + this.otelRequest = otelRequest; |
| 72 | + this.context = context; |
| 73 | + this.scope = scope; |
68 | 74 | } |
69 | 75 |
|
70 | | - // The parameter we need are in different positions, we are not going to have separate |
71 | | - // advices for all of them. The method gets the statement either as String or |
72 | | - // PreparedStatement, use the first argument that is either of these. PromiseInternal is |
73 | | - // always at the end of the argument list. |
74 | | - String sql = null; |
75 | | - PromiseInternal<?> promiseInternal = null; |
76 | | - for (Object argument : arguments) { |
77 | | - if (sql == null) { |
78 | | - if (argument instanceof String) { |
79 | | - sql = (String) argument; |
80 | | - } else if (argument instanceof PreparedStatement) { |
81 | | - sql = ((PreparedStatement) argument).sql(); |
| 76 | + public static AdviceScope start(Object queryExecutor, Object[] arguments) { |
| 77 | + CallDepth callDepth = CallDepth.forClass(queryExecutor.getClass()); |
| 78 | + if (callDepth.getAndIncrement() > 0) { |
| 79 | + return new AdviceScope(callDepth, null, null, null); |
| 80 | + } |
| 81 | + |
| 82 | + // The parameter we need are in different positions, we are not going to have separate |
| 83 | + // advices for all of them. The method gets the statement either as String or |
| 84 | + // PreparedStatement, use the first argument that is either of these. PromiseInternal is |
| 85 | + // always at the end of the argument list. |
| 86 | + String sql = null; |
| 87 | + PromiseInternal<?> promiseInternal = null; |
| 88 | + for (Object argument : arguments) { |
| 89 | + if (sql == null) { |
| 90 | + if (argument instanceof String) { |
| 91 | + sql = (String) argument; |
| 92 | + } else if (argument instanceof PreparedStatement) { |
| 93 | + sql = ((PreparedStatement) argument).sql(); |
| 94 | + } |
| 95 | + } else if (argument instanceof PromiseInternal) { |
| 96 | + promiseInternal = (PromiseInternal<?>) argument; |
82 | 97 | } |
83 | | - } else if (argument instanceof PromiseInternal) { |
84 | | - promiseInternal = (PromiseInternal) argument; |
85 | 98 | } |
86 | | - } |
87 | | - if (sql == null || promiseInternal == null) { |
88 | | - return; |
| 99 | + if (sql == null || promiseInternal == null) { |
| 100 | + return new AdviceScope(callDepth, null, null, null); |
| 101 | + } |
| 102 | + |
| 103 | + VertxSqlClientRequest otelRequest = |
| 104 | + new VertxSqlClientRequest(sql, QueryExecutorUtil.getConnectOptions(queryExecutor)); |
| 105 | + Context parentContext = currentContext(); |
| 106 | + if (!instrumenter().shouldStart(parentContext, otelRequest)) { |
| 107 | + return new AdviceScope(callDepth, otelRequest, null, null); |
| 108 | + } |
| 109 | + |
| 110 | + Context context = instrumenter().start(parentContext, otelRequest); |
| 111 | + VertxSqlClientUtil.attachRequest(promiseInternal, otelRequest, context, parentContext); |
| 112 | + return new AdviceScope(callDepth, otelRequest, context, context.makeCurrent()); |
89 | 113 | } |
90 | 114 |
|
91 | | - otelRequest = |
92 | | - new VertxSqlClientRequest(sql, QueryExecutorUtil.getConnectOptions(queryExecutor)); |
93 | | - Context parentContext = currentContext(); |
94 | | - if (!instrumenter().shouldStart(parentContext, otelRequest)) { |
95 | | - return; |
| 115 | + public void end(@Nullable Throwable throwable) { |
| 116 | + if (callDepth.decrementAndGet() > 0) { |
| 117 | + return; |
| 118 | + } |
| 119 | + if (scope == null || context == null || otelRequest == null) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + scope.close(); |
| 124 | + if (throwable != null) { |
| 125 | + instrumenter().end(context, otelRequest, null, throwable); |
| 126 | + } |
| 127 | + // span will be ended in QueryResultBuilderInstrumentation |
96 | 128 | } |
| 129 | + } |
97 | 130 |
|
98 | | - context = instrumenter().start(parentContext, otelRequest); |
99 | | - scope = context.makeCurrent(); |
100 | | - VertxSqlClientUtil.attachRequest(promiseInternal, otelRequest, context, parentContext); |
| 131 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 132 | + public static AdviceScope onEnter( |
| 133 | + @Advice.This Object queryExecutor, @Advice.AllArguments Object[] arguments) { |
| 134 | + return AdviceScope.start(queryExecutor, arguments); |
101 | 135 | } |
102 | 136 |
|
103 | 137 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
104 | 138 | public static void onExit( |
105 | | - @Advice.Thrown Throwable throwable, |
106 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
107 | | - @Advice.Local("otelRequest") VertxSqlClientRequest otelRequest, |
108 | | - @Advice.Local("otelContext") Context context, |
109 | | - @Advice.Local("otelScope") Scope scope) { |
110 | | - if (callDepth.decrementAndGet() > 0) { |
111 | | - return; |
112 | | - } |
113 | | - if (scope == null) { |
114 | | - return; |
115 | | - } |
| 139 | + @Advice.Thrown @Nullable Throwable throwable, |
| 140 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
116 | 141 |
|
117 | | - scope.close(); |
118 | | - if (throwable != null) { |
119 | | - instrumenter().end(context, otelRequest, null, throwable); |
| 142 | + if (adviceScope != null) { |
| 143 | + adviceScope.end(throwable); |
120 | 144 | } |
121 | | - // span will be ended in QueryResultBuilderInstrumentation |
122 | 145 | } |
123 | 146 | } |
124 | 147 | } |
0 commit comments