Skip to content

Commit 196d7c9

Browse files
committed
vertx-sql-client-5.0
1 parent 809f207 commit 196d7c9

File tree

5 files changed

+96
-69
lines changed

5 files changed

+96
-69
lines changed

instrumentation/vertx/vertx-sql-client/vertx-sql-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v5_0/sql/PoolInstrumentation.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.vertx.sqlclient.SqlConnectOptions;
2929
import io.vertx.sqlclient.SqlConnection;
3030
import net.bytebuddy.asm.Advice;
31+
import net.bytebuddy.asm.Advice.AssignReturned;
3132
import net.bytebuddy.description.type.TypeDescription;
3233
import net.bytebuddy.matcher.ElementMatcher;
3334

@@ -61,23 +62,23 @@ public void transform(TypeTransformer transformer) {
6162
@SuppressWarnings("unused")
6263
public static class PoolAdvice {
6364
@Advice.OnMethodEnter(suppress = Throwable.class)
64-
public static void onEnter(
65-
@Advice.Argument(1) SqlConnectOptions sqlConnectOptions,
66-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
65+
public static CallDepth onEnter(@Advice.Argument(1) SqlConnectOptions sqlConnectOptions) {
66+
CallDepth callDepth = null;
6767
callDepth = CallDepth.forClass(Pool.class);
6868
if (callDepth.getAndIncrement() > 0) {
69-
return;
69+
return callDepth;
7070
}
7171

7272
// set connection options to ThreadLocal, they will be read in SqlClientBase constructor
7373
setSqlConnectOptions(sqlConnectOptions);
74+
return callDepth;
7475
}
7576

7677
@Advice.OnMethodExit(suppress = Throwable.class)
7778
public static void onExit(
7879
@Advice.Return Pool pool,
7980
@Advice.Argument(1) SqlConnectOptions sqlConnectOptions,
80-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
81+
@Advice.Enter CallDepth callDepth) {
8182
if (callDepth.decrementAndGet() > 0) {
8283
return;
8384
}
@@ -93,14 +94,14 @@ public static void onExit(
9394

9495
@SuppressWarnings("unused")
9596
public static class GetConnectionAdvice {
97+
@AssignReturned.ToReturned
9698
@Advice.OnMethodExit(suppress = Throwable.class)
97-
public static void onExit(
98-
@Advice.This Pool pool, @Advice.Return(readOnly = false) Future<SqlConnection> future) {
99+
public static Future<SqlConnection> onExit(
100+
@Advice.This Pool pool, @Advice.Return Future<SqlConnection> future) {
99101
// copy connect options stored on pool to new connection
100102
SqlConnectOptions sqlConnectOptions = getPoolSqlConnectOptions(pool);
101103

102-
future = attachConnectOptions(future, sqlConnectOptions);
103-
future = wrapContext(future);
104+
return wrapContext(attachConnectOptions(future, sqlConnectOptions));
104105
}
105106
}
106107
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v5_0/sql/QueryExecutorInstrumentation.java

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.vertx.core.internal.PromiseInternal;
2323
import io.vertx.sqlclient.impl.QueryExecutorUtil;
2424
import io.vertx.sqlclient.internal.PreparedStatement;
25+
import javax.annotation.Nullable;
2526
import net.bytebuddy.asm.Advice;
2627
import net.bytebuddy.description.type.TypeDescription;
2728
import net.bytebuddy.matcher.ElementMatcher;
@@ -54,71 +55,89 @@ public static void onExit(@Advice.This Object queryExecutor) {
5455

5556
@SuppressWarnings("unused")
5657
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+
public static class AdviceScope {
59+
private final CallDepth callDepth;
60+
private final @Nullable VertxSqlClientRequest otelRequest;
61+
private final @Nullable Context context;
62+
private final @Nullable Scope scope;
63+
64+
private AdviceScope(CallDepth callDepth) {
65+
this(callDepth, null, null, null);
66+
}
67+
68+
private AdviceScope(
69+
CallDepth callDepth, VertxSqlClientRequest otelRequest, Context context, Scope scope) {
70+
this.callDepth = callDepth;
71+
this.otelRequest = otelRequest;
72+
this.context = context;
73+
this.scope = scope;
6874
}
6975

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);
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;
8297
}
83-
} else if (argument instanceof PromiseInternal) {
84-
promiseInternal = (PromiseInternal) argument;
8598
}
86-
}
87-
if (sql == null || promiseInternal == null) {
88-
return;
99+
if (sql == null || promiseInternal == null) {
100+
return new AdviceScope(callDepth);
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);
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());
89113
}
90114

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(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
96128
}
129+
}
97130

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);
101135
}
102136

103137
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
104138
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-
}
116-
117-
scope.close();
118-
if (throwable != null) {
119-
instrumenter().end(context, otelRequest, null, throwable);
120-
}
121-
// span will be ended in QueryResultBuilderInstrumentation
139+
@Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) {
140+
adviceScope.end(throwable);
122141
}
123142
}
124143
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v5_0/sql/SqlClientBaseInstrumentation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ public static void onExit(@Advice.This SqlClientBase sqlClientBase) {
5050
@SuppressWarnings("unused")
5151
public static class QueryAdvice {
5252
@Advice.OnMethodEnter(suppress = Throwable.class)
53-
public static void onEnter(
54-
@Advice.This SqlClientBase sqlClientBase,
55-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
53+
public static CallDepth onEnter(@Advice.This SqlClientBase sqlClientBase) {
54+
CallDepth callDepth = null;
5655
callDepth = CallDepth.forClass(SqlClientBase.class);
5756
if (callDepth.getAndIncrement() > 0) {
58-
return;
57+
return callDepth;
5958
}
6059

6160
// set connection options to ThreadLocal, they will be read in QueryExecutor constructor
6261
SqlConnectOptions sqlConnectOptions = getSqlConnectOptions(sqlClientBase);
6362
setSqlConnectOptions(sqlConnectOptions);
63+
return callDepth;
6464
}
6565

6666
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6767
public static void onExit(
68-
@Advice.Thrown Throwable throwable, @Advice.Local("otelCallDepth") CallDepth callDepth) {
68+
@Advice.Thrown Throwable throwable, @Advice.Enter CallDepth callDepth) {
6969
if (callDepth.decrementAndGet() > 0) {
7070
return;
7171
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v5_0/sql/TransactionImplInstrumentation.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1313
import io.vertx.core.Completable;
1414
import net.bytebuddy.asm.Advice;
15+
import net.bytebuddy.asm.Advice.AssignReturned;
1516
import net.bytebuddy.description.type.TypeDescription;
1617
import net.bytebuddy.matcher.ElementMatcher;
1718

@@ -31,9 +32,10 @@ public void transform(TypeTransformer transformer) {
3132

3233
@SuppressWarnings("unused")
3334
public static class WrapHandlerAdvice {
35+
@AssignReturned.ToReturned
3436
@Advice.OnMethodExit(suppress = Throwable.class)
35-
public static void wrapHandler(@Advice.Return(readOnly = false) Completable<?> handler) {
36-
handler = CompletableWrapper.wrap(handler);
37+
public static Completable<?> wrapHandler(@Advice.Return Completable<?> handler) {
38+
return CompletableWrapper.wrap(handler);
3739
}
3840
}
3941
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v5_0/sql/VertxSqlClientInstrumentationModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
4949
new QueryResultBuilderInstrumentation(),
5050
new TransactionImplInstrumentation());
5151
}
52+
53+
@Override
54+
public boolean isIndyReady() {
55+
return true;
56+
}
5257
}

0 commit comments

Comments
 (0)