Skip to content

Commit 2903ab5

Browse files
committed
[jdbc] Capture custom object types in prepared statement parameter instrumentation
Previously, setObject() calls with custom types (e.g. ORM-managed ID classes) were silently dropped because the instrumentation only captured a hardcoded whitelist of known JDBC types. Any class not in the list produced no parameter attribute at all, indistinguishable from a parameter that was never set.
1 parent b0a6cdb commit 2903ab5

2 files changed

Lines changed: 39 additions & 45 deletions

File tree

instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/PreparedStatementInstrumentation.java

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@
2121
import io.opentelemetry.javaagent.bootstrap.CallDepth;
2222
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2323
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
24-
import java.net.URL;
25-
import java.sql.Date;
2624
import java.sql.PreparedStatement;
27-
import java.sql.RowId;
2825
import java.sql.Statement;
29-
import java.sql.Time;
30-
import java.sql.Timestamp;
3126
import java.util.Calendar;
3227
import javax.annotation.Nullable;
3328
import net.bytebuddy.asm.Advice;
@@ -168,22 +163,8 @@ public static void onExit(
168163
return;
169164
}
170165

171-
String str = null;
172-
173-
if (value instanceof Boolean
174-
// Byte, Short, Int, Long, Float, Double, BigDecimal
175-
|| value instanceof Number
176-
|| value instanceof String
177-
|| value instanceof Date
178-
|| value instanceof Time
179-
|| value instanceof Timestamp
180-
|| value instanceof URL
181-
|| value instanceof RowId) {
182-
str = value.toString();
183-
}
184-
185-
if (str != null) {
186-
JdbcData.addParameter(statement, Integer.toString(index - 1), str);
166+
if (value != null) {
167+
JdbcData.addParameter(statement, Integer.toString(index - 1), value.toString());
187168
}
188169
}
189170
}
@@ -207,22 +188,8 @@ public static void onExit(
207188
return;
208189
}
209190

210-
String str = null;
211-
212-
if (value instanceof Boolean
213-
// Byte, Short, Int, Long, Float, Double, BigDecimal
214-
|| value instanceof Number
215-
|| value instanceof String
216-
|| value instanceof Date
217-
|| value instanceof Time
218-
|| value instanceof Timestamp
219-
|| value instanceof URL
220-
|| value instanceof RowId) {
221-
str = value.toString();
222-
}
223-
224-
if (str != null) {
225-
JdbcData.addParameter(statement, Integer.toString(index - 1), str);
191+
if (value != null) {
192+
JdbcData.addParameter(statement, Integer.toString(index - 1), value.toString());
226193
}
227194
}
228195
}
@@ -246,14 +213,8 @@ public static void onExit(
246213
return;
247214
}
248215

249-
String str = null;
250-
251-
if (value instanceof Date || value instanceof Time || value instanceof Timestamp) {
252-
str = value.toString();
253-
}
254-
255-
if (str != null) {
256-
JdbcData.addParameter(statement, Integer.toString(index - 1), str);
216+
if (value != null) {
217+
JdbcData.addParameter(statement, Integer.toString(index - 1), value.toString());
257218
}
258219
}
259220
}

instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/testing/AbstractPreparedStatementParametersTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.opentelemetry.api.trace.SpanKind;
2727
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
2828
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
29+
import java.io.Serializable;
2930
import java.math.BigDecimal;
3031
import java.sql.Connection;
3132
import java.sql.Date;
@@ -374,6 +375,31 @@ void testObjectPreparedStatementParameter(
374375
"S");
375376
}
376377

378+
@ParameterizedTest
379+
@MethodSource("preparedStatementStream")
380+
void testCustomObjectPreparedStatementParameter(
381+
String system,
382+
Connection connection,
383+
String username,
384+
String query,
385+
String sanitizedQuery,
386+
String spanName,
387+
String url,
388+
String table)
389+
throws SQLException {
390+
test(
391+
system,
392+
connection,
393+
username,
394+
query,
395+
sanitizedQuery,
396+
spanName,
397+
url,
398+
table,
399+
statement -> statement.setObject(1, new IdType()),
400+
"id");
401+
}
402+
377403
@ParameterizedTest
378404
@MethodSource("preparedStatementStream")
379405
void testObjectWithTypePreparedStatementParameter(
@@ -658,6 +684,13 @@ DB_CONNECTION_STRING, emitStableDatabaseSemconv() ? null : url),
658684
expectedParameterValue))));
659685
}
660686

687+
private static class IdType implements Serializable {
688+
@Override
689+
public String toString() {
690+
return "id";
691+
}
692+
}
693+
661694
private interface ThrowingConsumer<T, E extends Throwable> {
662695
void accept(T t) throws E;
663696
}

0 commit comments

Comments
 (0)