Skip to content

Commit 75d33c4

Browse files
committed
Add Vert.x SQL batch size attribute
1 parent 3d1a5e6 commit 75d33c4

6 files changed

Lines changed: 55 additions & 13 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.vertx.sqlclient.SqlConnectOptions;
2323
import io.vertx.sqlclient.impl.PreparedStatement;
2424
import io.vertx.sqlclient.impl.QueryExecutorUtil;
25+
import java.util.Collection;
2526
import javax.annotation.Nullable;
2627
import net.bytebuddy.asm.Advice;
2728
import net.bytebuddy.description.type.TypeDescription;
@@ -73,7 +74,7 @@ private AdviceScope(
7374
this.scope = scope;
7475
}
7576

76-
public static AdviceScope start(Object queryExecutor, Object[] arguments) {
77+
public static AdviceScope start(Object queryExecutor, String methodName, Object[] arguments) {
7778
CallDepth callDepth = CallDepth.forClass(queryExecutor.getClass());
7879
if (callDepth.getAndIncrement() > 0) {
7980
return new AdviceScope(callDepth);
@@ -86,6 +87,7 @@ public static AdviceScope start(Object queryExecutor, Object[] arguments) {
8687
String sql = null;
8788
boolean preparedStatement = false;
8889
PromiseInternal<?> promiseInternal = null;
90+
Long batchSize = null;
8991
for (Object argument : arguments) {
9092
if (sql == null) {
9193
if (argument instanceof String) {
@@ -97,6 +99,10 @@ public static AdviceScope start(Object queryExecutor, Object[] arguments) {
9799
} else if (argument instanceof PromiseInternal) {
98100
promiseInternal = (PromiseInternal<?>) argument;
99101
}
102+
if (methodName.equals("executeBatchQuery") && argument instanceof Collection) {
103+
int size = ((Collection<?>) argument).size();
104+
batchSize = size > 1 ? (long) size : null;
105+
}
100106
}
101107
if (sql == null || promiseInternal == null) {
102108
return new AdviceScope(callDepth);
@@ -116,7 +122,7 @@ public static AdviceScope start(Object queryExecutor, Object[] arguments) {
116122
dbSystem = VertxSqlClientUtil.getDbSystemNameFromClassName(connectOptions);
117123
}
118124
VertxSqlClientRequest otelRequest =
119-
new VertxSqlClientRequest(sql, connectOptions, preparedStatement, dbSystem);
125+
new VertxSqlClientRequest(sql, connectOptions, preparedStatement, dbSystem, batchSize);
120126
Context parentContext = Context.current();
121127
if (!instrumenter().shouldStart(parentContext, otelRequest)) {
122128
return new AdviceScope(callDepth);
@@ -145,8 +151,10 @@ public void end(@Nullable Throwable throwable) {
145151

146152
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
147153
public static AdviceScope onEnter(
148-
@Advice.This Object queryExecutor, @Advice.AllArguments Object[] arguments) {
149-
return AdviceScope.start(queryExecutor, arguments);
154+
@Advice.This Object queryExecutor,
155+
@Advice.Origin("#m") String methodName,
156+
@Advice.AllArguments Object[] arguments) {
157+
return AdviceScope.start(queryExecutor, methodName, arguments);
150158
}
151159

152160
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false)

instrumentation/vertx/vertx-sql-client/vertx-sql-client-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/vertx/sqlclient/v4_0/VertxSqlClientTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static io.opentelemetry.instrumentation.testing.junit.service.SemconvServiceStabilityUtil.maybeStablePeerService;
1111
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
1212
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
13+
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_BATCH_SIZE;
1314
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_SUMMARY;
1415
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
1516
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE;
@@ -290,7 +291,10 @@ void testBatch() throws Exception {
290291
trace.hasSpansSatisfyingExactly(
291292
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL),
292293
span ->
293-
span.hasName(emitStableDatabaseSemconv() ? "insert test" : "INSERT tempdb.test")
294+
span.hasName(
295+
emitStableDatabaseSemconv()
296+
? "BATCH insert test"
297+
: "INSERT tempdb.test")
294298
.hasKind(SpanKind.CLIENT)
295299
.hasParent(trace.getSpan(0))
296300
.hasAttributesSatisfyingExactly(
@@ -304,7 +308,9 @@ void testBatch() throws Exception {
304308
"insert into test values ($1, $2) returning *"),
305309
equalTo(
306310
DB_QUERY_SUMMARY,
307-
emitStableDatabaseSemconv() ? "insert test" : null),
311+
emitStableDatabaseSemconv() ? "BATCH insert test" : null),
312+
equalTo(
313+
DB_OPERATION_BATCH_SIZE, emitStableDatabaseSemconv() ? 2L : null),
308314
equalTo(
309315
maybeStable(DB_OPERATION),
310316
emitStableDatabaseSemconv() ? null : "INSERT"),

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.vertx.sqlclient.SqlConnectOptions;
2323
import io.vertx.sqlclient.impl.QueryExecutorUtil;
2424
import io.vertx.sqlclient.internal.PreparedStatement;
25+
import java.util.Collection;
2526
import javax.annotation.Nullable;
2627
import net.bytebuddy.asm.Advice;
2728
import net.bytebuddy.description.type.TypeDescription;
@@ -72,7 +73,7 @@ private AdviceScope(
7273
this.scope = scope;
7374
}
7475

75-
public static AdviceScope start(Object queryExecutor, Object[] arguments) {
76+
public static AdviceScope start(Object queryExecutor, String methodName, Object[] arguments) {
7677
CallDepth callDepth = CallDepth.forClass(queryExecutor.getClass());
7778
if (callDepth.getAndIncrement() > 0) {
7879
return new AdviceScope(callDepth);
@@ -85,6 +86,7 @@ public static AdviceScope start(Object queryExecutor, Object[] arguments) {
8586
String sql = null;
8687
boolean preparedStatement = false;
8788
PromiseInternal<?> promiseInternal = null;
89+
Long batchSize = null;
8890
for (Object argument : arguments) {
8991
if (sql == null) {
9092
if (argument instanceof String) {
@@ -96,6 +98,10 @@ public static AdviceScope start(Object queryExecutor, Object[] arguments) {
9698
} else if (argument instanceof PromiseInternal) {
9799
promiseInternal = (PromiseInternal<?>) argument;
98100
}
101+
if (methodName.equals("executeBatchQuery") && argument instanceof Collection) {
102+
int size = ((Collection<?>) argument).size();
103+
batchSize = size > 1 ? (long) size : null;
104+
}
99105
}
100106
if (sql == null || promiseInternal == null) {
101107
return new AdviceScope(callDepth);
@@ -110,7 +116,7 @@ public static AdviceScope start(Object queryExecutor, Object[] arguments) {
110116
}
111117
String dbSystem = VertxSqlClientSingletons.getConnectOptionsDbSystem(connectOptions);
112118
VertxSqlClientRequest otelRequest =
113-
new VertxSqlClientRequest(sql, connectOptions, preparedStatement, dbSystem);
119+
new VertxSqlClientRequest(sql, connectOptions, preparedStatement, dbSystem, batchSize);
114120
Context parentContext = Context.current();
115121
if (!instrumenter().shouldStart(parentContext, otelRequest)) {
116122
return new AdviceScope(callDepth);
@@ -139,8 +145,10 @@ public void end(@Nullable Throwable throwable) {
139145

140146
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
141147
public static AdviceScope onEnter(
142-
@Advice.This Object queryExecutor, @Advice.AllArguments Object[] arguments) {
143-
return AdviceScope.start(queryExecutor, arguments);
148+
@Advice.This Object queryExecutor,
149+
@Advice.Origin("#m") String methodName,
150+
@Advice.AllArguments Object[] arguments) {
151+
return AdviceScope.start(queryExecutor, methodName, arguments);
144152
}
145153

146154
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false)

instrumentation/vertx/vertx-sql-client/vertx-sql-client-5.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/vertx/sqlclient/v5_0/VertxSqlClientTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static io.opentelemetry.instrumentation.testing.junit.service.SemconvServiceStabilityUtil.maybeStablePeerService;
1111
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
1212
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
13+
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_BATCH_SIZE;
1314
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_SUMMARY;
1415
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
1516
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE;
@@ -291,7 +292,10 @@ void testBatch() throws Exception {
291292
trace.hasSpansSatisfyingExactly(
292293
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL),
293294
span ->
294-
span.hasName(emitStableDatabaseSemconv() ? "insert test" : "INSERT tempdb.test")
295+
span.hasName(
296+
emitStableDatabaseSemconv()
297+
? "BATCH insert test"
298+
: "INSERT tempdb.test")
295299
.hasKind(SpanKind.CLIENT)
296300
.hasParent(trace.getSpan(0))
297301
.hasAttributesSatisfyingExactly(
@@ -305,7 +309,9 @@ void testBatch() throws Exception {
305309
"insert into test values ($1, $2) returning *"),
306310
equalTo(
307311
DB_QUERY_SUMMARY,
308-
emitStableDatabaseSemconv() ? "insert test" : null),
312+
emitStableDatabaseSemconv() ? "BATCH insert test" : null),
313+
equalTo(
314+
DB_OPERATION_BATCH_SIZE, emitStableDatabaseSemconv() ? 2L : null),
309315
equalTo(
310316
maybeStable(DB_OPERATION),
311317
emitStableDatabaseSemconv() ? null : "INSERT"),

instrumentation/vertx/vertx-sql-client/vertx-sql-client-common-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/sqlclient/common/v4_0/VertxSqlClientAttributesGetter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ public Collection<String> getRawQueryTexts(VertxSqlClientRequest request) {
9090
return singleton(request.getQueryText());
9191
}
9292

93+
@Nullable
94+
@Override
95+
public Long getDbOperationBatchSize(VertxSqlClientRequest request) {
96+
return request.getOperationBatchSize();
97+
}
98+
9399
@Nullable
94100
@Override
95101
public String getErrorType(

instrumentation/vertx/vertx-sql-client/vertx-sql-client-common-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/sqlclient/common/v4_0/VertxSqlClientRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ public class VertxSqlClientRequest {
1414
@Nullable private final SqlConnectOptions sqlConnectOptions;
1515
private final boolean parameterizedQuery;
1616
private final String dbSystemName;
17+
@Nullable private final Long operationBatchSize;
1718

1819
public VertxSqlClientRequest(
1920
String queryText,
2021
@Nullable SqlConnectOptions sqlConnectOptions,
2122
boolean parameterizedQuery,
22-
String dbSystemName) {
23+
String dbSystemName,
24+
@Nullable Long operationBatchSize) {
2325
this.queryText = queryText;
2426
this.sqlConnectOptions = sqlConnectOptions;
2527
this.parameterizedQuery = parameterizedQuery;
2628
this.dbSystemName = dbSystemName;
29+
this.operationBatchSize = operationBatchSize;
2730
}
2831

2932
public String getQueryText() {
@@ -57,4 +60,9 @@ public boolean isParameterizedQuery() {
5760
public String getDbSystemName() {
5861
return dbSystemName;
5962
}
63+
64+
@Nullable
65+
public Long getOperationBatchSize() {
66+
return operationBatchSize;
67+
}
6068
}

0 commit comments

Comments
 (0)