Skip to content

Commit 2ed8b96

Browse files
committed
vertx-sql-client-4.0
1 parent 42bdc90 commit 2ed8b96

File tree

5 files changed

+98
-70
lines changed

5 files changed

+98
-70
lines changed

instrumentation/vertx/vertx-sql-client/vertx-sql-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/sql/PoolInstrumentation.java

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

@@ -60,23 +61,22 @@ public void transform(TypeTransformer transformer) {
6061
@SuppressWarnings("unused")
6162
public static class PoolAdvice {
6263
@Advice.OnMethodEnter(suppress = Throwable.class)
63-
public static void onEnter(
64-
@Advice.Argument(1) SqlConnectOptions sqlConnectOptions,
65-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
66-
callDepth = CallDepth.forClass(Pool.class);
64+
public static CallDepth onEnter(@Advice.Argument(1) SqlConnectOptions sqlConnectOptions) {
65+
CallDepth callDepth = CallDepth.forClass(Pool.class);
6766
if (callDepth.getAndIncrement() > 0) {
68-
return;
67+
return callDepth;
6968
}
7069

7170
// set connection options to ThreadLocal, they will be read in SqlClientBase constructor
7271
setSqlConnectOptions(sqlConnectOptions);
72+
return callDepth;
7373
}
7474

7575
@Advice.OnMethodExit(suppress = Throwable.class)
7676
public static void onExit(
7777
@Advice.Return Pool pool,
7878
@Advice.Argument(1) SqlConnectOptions sqlConnectOptions,
79-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
79+
@Advice.Enter CallDepth callDepth) {
8080
if (callDepth.decrementAndGet() > 0) {
8181
return;
8282
}
@@ -88,14 +88,13 @@ public static void onExit(
8888

8989
@SuppressWarnings("unused")
9090
public static class GetConnectionAdvice {
91+
@AssignReturned.ToReturned
9192
@Advice.OnMethodExit(suppress = Throwable.class)
92-
public static void onExit(
93-
@Advice.This Pool pool, @Advice.Return(readOnly = false) Future<SqlConnection> future) {
93+
public static Future<SqlConnection> onExit(
94+
@Advice.This Pool pool, @Advice.Return Future<SqlConnection> future) {
9495
// copy connect options stored on pool to new connection
9596
SqlConnectOptions sqlConnectOptions = getPoolSqlConnectOptions(pool);
96-
97-
future = attachConnectOptions(future, sqlConnectOptions);
98-
future = wrapContext(future);
97+
return wrapContext(attachConnectOptions(future, sqlConnectOptions));
9998
}
10099
}
101100
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/sql/QueryExecutorInstrumentation.java

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.vertx.core.impl.future.PromiseInternal;
2323
import io.vertx.sqlclient.impl.PreparedStatement;
2424
import io.vertx.sqlclient.impl.QueryExecutorUtil;
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,93 @@ 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+
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;
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, 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;
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, 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());
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(@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
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-
}
139+
@Advice.Thrown @Nullable Throwable throwable,
140+
@Advice.Enter @Nullable AdviceScope adviceScope) {
116141

117-
scope.close();
118-
if (throwable != null) {
119-
instrumenter().end(context, otelRequest, null, throwable);
142+
if (adviceScope != null) {
143+
adviceScope.end(throwable);
120144
}
121-
// span will be ended in QueryResultBuilderInstrumentation
122145
}
123146
}
124147
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/sql/SqlClientBaseInstrumentation.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,21 @@ 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) {
56-
callDepth = CallDepth.forClass(SqlClientBase.class);
53+
public static CallDepth onEnter(@Advice.This SqlClientBase<?> sqlClientBase) {
54+
CallDepth callDepth = CallDepth.forClass(SqlClientBase.class);
5755
if (callDepth.getAndIncrement() > 0) {
58-
return;
56+
return callDepth;
5957
}
6058

6159
// set connection options to ThreadLocal, they will be read in QueryExecutor constructor
6260
SqlConnectOptions sqlConnectOptions = getSqlConnectOptions(sqlClientBase);
6361
setSqlConnectOptions(sqlConnectOptions);
62+
return callDepth;
6463
}
6564

6665
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6766
public static void onExit(
68-
@Advice.Thrown Throwable throwable, @Advice.Local("otelCallDepth") CallDepth callDepth) {
67+
@Advice.Thrown Throwable throwable, @Advice.Enter CallDepth callDepth) {
6968
if (callDepth.decrementAndGet() > 0) {
7069
return;
7170
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_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.Handler;
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) Handler<?> handler) {
36-
handler = HandlerWrapper.wrap(handler);
37+
public static Handler<?> wrapHandler(@Advice.Return Handler<?> handler) {
38+
return HandlerWrapper.wrap(handler);
3739
}
3840
}
3941
}

instrumentation/vertx/vertx-sql-client/vertx-sql-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_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)