Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.net.URL;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.RowId;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -168,22 +163,8 @@ public static void onExit(
return;
}

String str = null;

if (value instanceof Boolean
// Byte, Short, Int, Long, Float, Double, BigDecimal
|| value instanceof Number
|| value instanceof String
|| value instanceof Date
|| value instanceof Time
|| value instanceof Timestamp
|| value instanceof URL
|| value instanceof RowId) {
str = value.toString();
}

if (str != null) {
JdbcData.addParameter(statement, Integer.toString(index - 1), str);
if (value != null) {
JdbcData.addParameter(statement, Integer.toString(index - 1), value.toString());
}
}
}
Expand All @@ -207,22 +188,8 @@ public static void onExit(
return;
}

String str = null;

if (value instanceof Boolean
// Byte, Short, Int, Long, Float, Double, BigDecimal
|| value instanceof Number
|| value instanceof String
|| value instanceof Date
|| value instanceof Time
|| value instanceof Timestamp
|| value instanceof URL
|| value instanceof RowId) {
str = value.toString();
}

if (str != null) {
JdbcData.addParameter(statement, Integer.toString(index - 1), str);
if (value != null) {
JdbcData.addParameter(statement, Integer.toString(index - 1), value.toString());
}
}
}
Expand All @@ -246,14 +213,8 @@ public static void onExit(
return;
}

String str = null;

if (value instanceof Date || value instanceof Time || value instanceof Timestamp) {
str = value.toString();
}

if (str != null) {
JdbcData.addParameter(statement, Integer.toString(index - 1), str);
if (value != null) {
JdbcData.addParameter(statement, Integer.toString(index - 1), value.toString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,6 @@ private void putParameter(int index, Object value) {
}
}

private void putParameterIfRecognizedType(int index, Object value) {
if (this.captureQueryParameters && value != null) {
String str = null;

if (value instanceof Boolean
// Byte, Short, Int, Long, Float, Double, BigDecimal
|| value instanceof Number
|| value instanceof String
|| value instanceof Date
|| value instanceof Time
|| value instanceof Timestamp
|| value instanceof URL
|| value instanceof RowId) {
str = value.toString();
}

if (str != null) {
parameters.put(Integer.toString(index - 1), str);
}
}
}

@Override
public ResultSet executeQuery() throws SQLException {
return OpenTelemetryResultSet.wrap(wrapCall(query, delegate::executeQuery), this);
Expand Down Expand Up @@ -267,14 +245,14 @@ public void setBinaryStream(int parameterIndex, InputStream x) throws SQLExcepti
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
delegate.setObject(parameterIndex, x, targetSqlType);
putParameterIfRecognizedType(parameterIndex, x);
putParameter(parameterIndex, x);
}

@SuppressWarnings("UngroupedOverloads")
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
delegate.setObject(parameterIndex, x);
putParameterIfRecognizedType(parameterIndex, x);
putParameter(parameterIndex, x);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
Expand Down Expand Up @@ -374,6 +376,35 @@ void testObjectPreparedStatementParameter(
"S");
}

@ParameterizedTest
@MethodSource("preparedStatementStream")
void testCustomObjectPreparedStatementParameter(
String system,
Connection connection,
String username,
String query,
String sanitizedQuery,
String spanName,
String url,
String table)
throws SQLException {
// Derby and HSQLDB reject setObject() with an unknown custom type at execution time
Assumptions.assumeFalse(system.equalsIgnoreCase("derby"));
Assumptions.assumeFalse(system.equalsIgnoreCase("hsqldb"));

test(
system,
connection,
username,
query,
sanitizedQuery,
spanName,
url,
table,
statement -> statement.setObject(1, new IdType()),
"id");
}

@ParameterizedTest
@MethodSource("preparedStatementStream")
void testObjectWithTypePreparedStatementParameter(
Expand Down Expand Up @@ -658,6 +689,13 @@ DB_CONNECTION_STRING, emitStableDatabaseSemconv() ? null : url),
expectedParameterValue))));
}

private static class IdType implements Serializable {
@Override
public String toString() {
return "id";
}
}

private interface ThrowingConsumer<T, E extends Throwable> {
void accept(T t) throws E;
}
Expand Down
Loading