diff --git a/instrumentation/jdbc/javaagent/src/test/groovy/JdbcInstrumentationTest.groovy b/instrumentation/jdbc/javaagent/src/test/groovy/JdbcInstrumentationTest.groovy deleted file mode 100644 index 39eec0059569..000000000000 --- a/instrumentation/jdbc/javaagent/src/test/groovy/JdbcInstrumentationTest.groovy +++ /dev/null @@ -1,903 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import com.mchange.v2.c3p0.ComboPooledDataSource -import com.zaxxer.hikari.HikariConfig -import com.zaxxer.hikari.HikariDataSource -import io.opentelemetry.api.trace.SpanKind -import io.opentelemetry.instrumentation.jdbc.TestConnection -import io.opentelemetry.instrumentation.jdbc.TestDriver -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification -import io.opentelemetry.javaagent.instrumentation.jdbc.test.ProxyStatementFactory -import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes -import io.opentelemetry.semconv.incubating.DbIncubatingAttributes -import io.opentelemetry.semconv.ServerAttributes -import org.apache.derby.jdbc.EmbeddedDataSource -import org.apache.derby.jdbc.EmbeddedDriver -import org.h2.Driver -import org.h2.jdbcx.JdbcDataSource -import org.hsqldb.jdbc.JDBCDriver -import spock.lang.Shared -import spock.lang.Unroll - -import javax.sql.DataSource -import java.sql.CallableStatement -import java.sql.Connection -import java.sql.DatabaseMetaData -import java.sql.PreparedStatement -import java.sql.ResultSet -import java.sql.SQLException -import java.sql.Statement - -import static io.opentelemetry.api.trace.SpanKind.CLIENT -import static io.opentelemetry.api.trace.SpanKind.INTERNAL - -@Unroll -class JdbcInstrumentationTest extends AgentInstrumentationSpecification { - - @Shared - def dbName = "jdbcUnitTest" - @Shared - def dbNameLower = dbName.toLowerCase() - - @Shared - private Map jdbcUrls = [ - "h2" : "jdbc:h2:mem:$dbName", - "derby" : "jdbc:derby:memory:$dbName", - "hsqldb": "jdbc:hsqldb:mem:$dbName", - ] - - @Shared - private Map jdbcDriverClassNames = [ - "h2" : "org.h2.Driver", - "derby" : "org.apache.derby.jdbc.EmbeddedDriver", - "hsqldb": "org.hsqldb.jdbc.JDBCDriver", - ] - - @Shared - private Map jdbcUserNames = [ - "h2" : null, - "derby" : "APP", - "hsqldb": "SA", - ] - - @Shared - private Properties connectionProps = { - def props = new Properties() -// props.put("user", "someUser") -// props.put("password", "somePassword") - props.put("databaseName", "someDb") - props.put("OPEN_NEW", "true") // So H2 doesn't complain about username/password. - return props - }() - - // JDBC Connection pool name (i.e. HikariCP) -> Map - @Shared - private Map> cpDatasources = new HashMap<>() - - def prepareConnectionPoolDatasources() { - String[] connectionPoolNames = [ - "tomcat", "hikari", "c3p0", - ] - connectionPoolNames.each { - cpName -> - Map dbDSMapping = new HashMap<>() - jdbcUrls.each { - dbType, jdbcUrl -> - dbDSMapping.put(dbType, createDS(cpName, dbType, jdbcUrl)) - } - cpDatasources.put(cpName, dbDSMapping) - } - } - - def createTomcatDS(String dbType, String jdbcUrl) { - DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource() - def jdbcUrlToSet = dbType == "derby" ? jdbcUrl + ";create=true" : jdbcUrl - ds.setUrl(jdbcUrlToSet) - ds.setDriverClassName(jdbcDriverClassNames.get(dbType)) - String username = jdbcUserNames.get(dbType) - if (username != null) { - ds.setUsername(username) - } - ds.setPassword("") - ds.setMaxActive(1) // to test proper caching, having > 1 max active connection will be hard to - // determine whether the connection is properly cached - return ds - } - - def createHikariDS(String dbType, String jdbcUrl) { - HikariConfig config = new HikariConfig() - def jdbcUrlToSet = dbType == "derby" ? jdbcUrl + ";create=true" : jdbcUrl - config.setJdbcUrl(jdbcUrlToSet) - String username = jdbcUserNames.get(dbType) - if (username != null) { - config.setUsername(username) - } - config.setPassword("") - config.addDataSourceProperty("cachePrepStmts", "true") - config.addDataSourceProperty("prepStmtCacheSize", "250") - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048") - config.setMaximumPoolSize(1) - - return new HikariDataSource(config) - } - - def createC3P0DS(String dbType, String jdbcUrl) { - DataSource ds = new ComboPooledDataSource() - ds.setDriverClass(jdbcDriverClassNames.get(dbType)) - def jdbcUrlToSet = dbType == "derby" ? jdbcUrl + ";create=true" : jdbcUrl - ds.setJdbcUrl(jdbcUrlToSet) - String username = jdbcUserNames.get(dbType) - if (username != null) { - ds.setUser(username) - } - ds.setPassword("") - ds.setMaxPoolSize(1) - return ds - } - - def createDS(String connectionPoolName, String dbType, String jdbcUrl) { - DataSource ds = null - if (connectionPoolName == "tomcat") { - ds = createTomcatDS(dbType, jdbcUrl) - } - if (connectionPoolName == "hikari") { - ds = createHikariDS(dbType, jdbcUrl) - } - if (connectionPoolName == "c3p0") { - ds = createC3P0DS(dbType, jdbcUrl) - } - return ds - } - - def setupSpec() { - prepareConnectionPoolDatasources() - } - - def cleanupSpec() { - cpDatasources.values().each { - it.values().each { - datasource -> - if (datasource instanceof Closeable) { - datasource.close() - } - } - } - } - - @SuppressWarnings("deprecation") // TODO DbIncubatingAttributes.DB_CONNECTION_STRING deprecation - def "basic statement with #connection.getClass().getCanonicalName() on #system generates spans"() { - setup: - Statement statement = connection.createStatement() - ResultSet resultSet = runWithSpan("parent") { - return statement.executeQuery(query) - } - - expect: - resultSet.next() - resultSet.getInt(1) == 3 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_NAME" dbNameLower - if (username != null) { - "$DbIncubatingAttributes.DB_USER" username - } - "$DbIncubatingAttributes.DB_CONNECTION_STRING" url - "$DbIncubatingAttributes.DB_STATEMENT" sanitizedQuery - "$DbIncubatingAttributes.DB_OPERATION" "SELECT" - "$DbIncubatingAttributes.DB_SQL_TABLE" table - } - } - } - } - - cleanup: - statement.close() - connection.close() - - where: - system | connection | username | query | sanitizedQuery | spanName | url | table - "h2" | new Driver().connect(jdbcUrls.get("h2"), null) | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | new EmbeddedDriver().connect(jdbcUrls.get("derby"), null) | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "hsqldb" | new JDBCDriver().connect(jdbcUrls.get("hsqldb"), null) | "SA" | "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT INFORMATION_SCHEMA.SYSTEM_USERS" | "hsqldb:mem:" | "INFORMATION_SCHEMA.SYSTEM_USERS" - "h2" | new Driver().connect(jdbcUrls.get("h2"), connectionProps) | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | new EmbeddedDriver().connect(jdbcUrls.get("derby"), connectionProps) | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "hsqldb" | new JDBCDriver().connect(jdbcUrls.get("hsqldb"), connectionProps) | "SA" | "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT INFORMATION_SCHEMA.SYSTEM_USERS" | "hsqldb:mem:" | "INFORMATION_SCHEMA.SYSTEM_USERS" - "h2" | cpDatasources.get("tomcat").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("tomcat").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "hsqldb" | cpDatasources.get("tomcat").get("hsqldb").getConnection() | "SA" | "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT INFORMATION_SCHEMA.SYSTEM_USERS" | "hsqldb:mem:" | "INFORMATION_SCHEMA.SYSTEM_USERS" - "h2" | cpDatasources.get("hikari").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("hikari").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "hsqldb" | cpDatasources.get("hikari").get("hsqldb").getConnection() | "SA" | "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT INFORMATION_SCHEMA.SYSTEM_USERS" | "hsqldb:mem:" | "INFORMATION_SCHEMA.SYSTEM_USERS" - "h2" | cpDatasources.get("c3p0").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("c3p0").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "hsqldb" | cpDatasources.get("c3p0").get("hsqldb").getConnection() | "SA" | "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS" | "SELECT INFORMATION_SCHEMA.SYSTEM_USERS" | "hsqldb:mem:" | "INFORMATION_SCHEMA.SYSTEM_USERS" - } - - def "prepared statement execute on #system with #connection.getClass().getCanonicalName() generates a span"() { - setup: - PreparedStatement statement = connection.prepareStatement(query) - ResultSet resultSet = runWithSpan("parent") { - assert statement.execute() - return statement.resultSet - } - - expect: - resultSet.next() - resultSet.getInt(1) == 3 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_NAME" dbNameLower - if (username != null) { - "$DbIncubatingAttributes.DB_USER" username - } - "$DbIncubatingAttributes.DB_CONNECTION_STRING" url - "$DbIncubatingAttributes.DB_STATEMENT" sanitizedQuery - "$DbIncubatingAttributes.DB_OPERATION" "SELECT" - "$DbIncubatingAttributes.DB_SQL_TABLE" table - } - } - } - } - - cleanup: - statement.close() - connection.close() - - where: - system | connection | username | query | sanitizedQuery | spanName | url | table - "h2" | new Driver().connect(jdbcUrls.get("h2"), null) | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | new EmbeddedDriver().connect(jdbcUrls.get("derby"), null) | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("tomcat").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("tomcat").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("hikari").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("hikari").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("c3p0").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("c3p0").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - } - - def "prepared statement query on #system with #connection.getClass().getCanonicalName() generates a span"() { - setup: - PreparedStatement statement = connection.prepareStatement(query) - ResultSet resultSet = runWithSpan("parent") { - return statement.executeQuery() - } - - expect: - resultSet.next() - resultSet.getInt(1) == 3 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_NAME" dbNameLower - if (username != null) { - "$DbIncubatingAttributes.DB_USER" username - } - "$DbIncubatingAttributes.DB_CONNECTION_STRING" url - "$DbIncubatingAttributes.DB_STATEMENT" sanitizedQuery - "$DbIncubatingAttributes.DB_OPERATION" "SELECT" - "$DbIncubatingAttributes.DB_SQL_TABLE" table - } - } - } - } - - cleanup: - statement.close() - connection.close() - - where: - system | connection | username | query | sanitizedQuery | spanName | url | table - "h2" | new Driver().connect(jdbcUrls.get("h2"), null) | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | new EmbeddedDriver().connect(jdbcUrls.get("derby"), null) | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("tomcat").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("tomcat").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("hikari").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("hikari").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("c3p0").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("c3p0").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - } - - def "prepared call on #system with #connection.getClass().getCanonicalName() generates a span"() { - setup: - CallableStatement statement = connection.prepareCall(query) - ResultSet resultSet = runWithSpan("parent") { - return statement.executeQuery() - } - - expect: - resultSet.next() - resultSet.getInt(1) == 3 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_NAME" dbName.toLowerCase() - if (username != null) { - "$DbIncubatingAttributes.DB_USER" username - } - "$DbIncubatingAttributes.DB_CONNECTION_STRING" url - "$DbIncubatingAttributes.DB_STATEMENT" sanitizedQuery - "$DbIncubatingAttributes.DB_OPERATION" "SELECT" - "$DbIncubatingAttributes.DB_SQL_TABLE" table - } - } - } - } - - cleanup: - statement.close() - connection.close() - - where: - system | connection | username | query | sanitizedQuery | spanName | url | table - "h2" | new Driver().connect(jdbcUrls.get("h2"), null) | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | new EmbeddedDriver().connect(jdbcUrls.get("derby"), null) | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("tomcat").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("tomcat").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("hikari").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("hikari").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - "h2" | cpDatasources.get("c3p0").get("h2").getConnection() | null | "SELECT 3" | "SELECT ?" | "SELECT $dbNameLower" | "h2:mem:" | null - "derby" | cpDatasources.get("c3p0").get("derby").getConnection() | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - } - - def "statement update on #system with #connection.getClass().getCanonicalName() generates a span"() { - setup: - Statement statement = connection.createStatement() - def sql = connection.nativeSQL(query) - - expect: - runWithSpan("parent") { - return !statement.execute(sql) - } - statement.updateCount == 0 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_NAME" dbNameLower - if (username != null) { - "$DbIncubatingAttributes.DB_USER" username - } - "$DbIncubatingAttributes.DB_STATEMENT" query - "$DbIncubatingAttributes.DB_CONNECTION_STRING" url - "$DbIncubatingAttributes.DB_OPERATION" "CREATE TABLE" - "$DbIncubatingAttributes.DB_SQL_TABLE" table - } - } - } - } - - cleanup: - statement.close() - connection.close() - - where: - system | connection | username | query | spanName | url | table - "h2" | new Driver().connect(jdbcUrls.get("h2"), null) | null | "CREATE TABLE S_H2 (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_H2" | "h2:mem:" | "S_H2" - "derby" | new EmbeddedDriver().connect(jdbcUrls.get("derby"), null) | "APP" | "CREATE TABLE S_DERBY (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_DERBY" | "derby:memory:" | "S_DERBY" - "hsqldb" | new JDBCDriver().connect(jdbcUrls.get("hsqldb"), null) | "SA" | "CREATE TABLE PUBLIC.S_HSQLDB (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE PUBLIC.S_HSQLDB" | "hsqldb:mem:" | "PUBLIC.S_HSQLDB" - "h2" | cpDatasources.get("tomcat").get("h2").getConnection() | null | "CREATE TABLE S_H2_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_H2_TOMCAT" | "h2:mem:" | "S_H2_TOMCAT" - "derby" | cpDatasources.get("tomcat").get("derby").getConnection() | "APP" | "CREATE TABLE S_DERBY_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_DERBY_TOMCAT" | "derby:memory:" | "S_DERBY_TOMCAT" - "hsqldb" | cpDatasources.get("tomcat").get("hsqldb").getConnection() | "SA" | "CREATE TABLE PUBLIC.S_HSQLDB_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE PUBLIC.S_HSQLDB_TOMCAT" | "hsqldb:mem:" | "PUBLIC.S_HSQLDB_TOMCAT" - "h2" | cpDatasources.get("hikari").get("h2").getConnection() | null | "CREATE TABLE S_H2_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_H2_HIKARI" | "h2:mem:" | "S_H2_HIKARI" - "derby" | cpDatasources.get("hikari").get("derby").getConnection() | "APP" | "CREATE TABLE S_DERBY_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_DERBY_HIKARI" | "derby:memory:" | "S_DERBY_HIKARI" - "hsqldb" | cpDatasources.get("hikari").get("hsqldb").getConnection() | "SA" | "CREATE TABLE PUBLIC.S_HSQLDB_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE PUBLIC.S_HSQLDB_HIKARI" | "hsqldb:mem:" | "PUBLIC.S_HSQLDB_HIKARI" - "h2" | cpDatasources.get("c3p0").get("h2").getConnection() | null | "CREATE TABLE S_H2_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_H2_C3P0" | "h2:mem:" | "S_H2_C3P0" - "derby" | cpDatasources.get("c3p0").get("derby").getConnection() | "APP" | "CREATE TABLE S_DERBY_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.S_DERBY_C3P0" | "derby:memory:" | "S_DERBY_C3P0" - "hsqldb" | cpDatasources.get("c3p0").get("hsqldb").getConnection() | "SA" | "CREATE TABLE PUBLIC.S_HSQLDB_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE PUBLIC.S_HSQLDB_C3P0" | "hsqldb:mem:" | "PUBLIC.S_HSQLDB_C3P0" - } - - def "prepared statement update on #system with #connection.getClass().getCanonicalName() generates a span"() { - setup: - def sql = connection.nativeSQL(query) - PreparedStatement statement = connection.prepareStatement(sql) - - expect: - runWithSpan("parent") { - return statement.executeUpdate() == 0 - } - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_NAME" dbName.toLowerCase() - if (username != null) { - "$DbIncubatingAttributes.DB_USER" username - } - "$DbIncubatingAttributes.DB_STATEMENT" query - "$DbIncubatingAttributes.DB_CONNECTION_STRING" url - "$DbIncubatingAttributes.DB_OPERATION" "CREATE TABLE" - "$DbIncubatingAttributes.DB_SQL_TABLE" table - } - } - } - } - - cleanup: - statement.close() - connection.close() - - where: - system | connection | username | query | spanName | url | table - "h2" | new Driver().connect(jdbcUrls.get("h2"), null) | null | "CREATE TABLE PS_H2 (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_H2" | "h2:mem:" | "PS_H2" - "derby" | new EmbeddedDriver().connect(jdbcUrls.get("derby"), null) | "APP" | "CREATE TABLE PS_DERBY (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_DERBY" | "derby:memory:" | "PS_DERBY" - "h2" | cpDatasources.get("tomcat").get("h2").getConnection() | null | "CREATE TABLE PS_H2_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_H2_TOMCAT" | "h2:mem:" | "PS_H2_TOMCAT" - "derby" | cpDatasources.get("tomcat").get("derby").getConnection() | "APP" | "CREATE TABLE PS_DERBY_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_DERBY_TOMCAT" | "derby:memory:" | "PS_DERBY_TOMCAT" - "h2" | cpDatasources.get("hikari").get("h2").getConnection() | null | "CREATE TABLE PS_H2_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_H2_HIKARI" | "h2:mem:" | "PS_H2_HIKARI" - "derby" | cpDatasources.get("hikari").get("derby").getConnection() | "APP" | "CREATE TABLE PS_DERBY_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_DERBY_HIKARI" | "derby:memory:" | "PS_DERBY_HIKARI" - "h2" | cpDatasources.get("c3p0").get("h2").getConnection() | null | "CREATE TABLE PS_H2_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_H2_C3P0" | "h2:mem:" | "PS_H2_C3P0" - "derby" | cpDatasources.get("c3p0").get("derby").getConnection() | "APP" | "CREATE TABLE PS_DERBY_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))" | "CREATE TABLE jdbcunittest.PS_DERBY_C3P0" | "derby:memory:" | "PS_DERBY_C3P0" - } - - def "connection constructor throwing then generating correct spans after recovery using #driver connection (prepare statement = #prepareStatement)"() { - setup: - Connection connection = null - - when: - try { - connection = new TestConnection(true) - connection.url = "jdbc:testdb://localhost" - } catch (Exception ignored) { - connection = driver.connect(jdbcUrl, null) - } - - def (Statement statement, ResultSet rs) = runWithSpan("parent") { - if (prepareStatement) { - def stmt = connection.prepareStatement(query) - return new Tuple(stmt, stmt.executeQuery()) - } - - def stmt = connection.createStatement() - return new Tuple(stmt, stmt.executeQuery(query)) - } - - then: - rs.next() - rs.getInt(1) == 3 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_NAME" dbNameLower - if (username != null) { - "$DbIncubatingAttributes.DB_USER" username - } - "$DbIncubatingAttributes.DB_CONNECTION_STRING" url - "$DbIncubatingAttributes.DB_STATEMENT" sanitizedQuery - "$DbIncubatingAttributes.DB_OPERATION" "SELECT" - "$DbIncubatingAttributes.DB_SQL_TABLE" table - } - } - } - } - - cleanup: - statement?.close() - connection?.close() - - where: - prepareStatement | system | driver | jdbcUrl | username | query | sanitizedQuery | spanName | url | table - true | "h2" | new Driver() | "jdbc:h2:mem:" + dbName | null | "SELECT 3;" | "SELECT ?;" | "SELECT $dbNameLower" | "h2:mem:" | null - true | "derby" | new EmbeddedDriver() | "jdbc:derby:memory:" + dbName + ";create=true" | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - false | "h2" | new Driver() | "jdbc:h2:mem:" + dbName | null | "SELECT 3;" | "SELECT ?;" | "SELECT $dbNameLower" | "h2:mem:" | null - false | "derby" | new EmbeddedDriver() | "jdbc:derby:memory:" + dbName + ";create=true" | "APP" | "SELECT 3 FROM SYSIBM.SYSDUMMY1" | "SELECT ? FROM SYSIBM.SYSDUMMY1" | "SELECT SYSIBM.SYSDUMMY1" | "derby:memory:" | "SYSIBM.SYSDUMMY1" - } - - def "calling #datasource.class.simpleName getConnection generates a span when under existing trace"() { - setup: - assert datasource instanceof DataSource - init?.call(datasource) - - when: - datasource.getConnection().close() - - then: - !traces.any { it.any { it.name == "database.connection" } } - clearExportedData() - - when: - runWithSpan("parent") { - datasource.getConnection().close() - } - - then: - assertTraces(1) { - trace(0, recursive ? 3 : 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - - span(1) { - name "${datasource.class.simpleName}.getConnection" - kind INTERNAL - childOf span(0) - attributes { - "$CodeIncubatingAttributes.CODE_NAMESPACE" datasource.class.name - "$CodeIncubatingAttributes.CODE_FUNCTION" "getConnection" - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_USER" { user == null | user == it } - "$DbIncubatingAttributes.DB_NAME" "jdbcunittest" - "$DbIncubatingAttributes.DB_CONNECTION_STRING" connectionString - } - } - if (recursive) { - span(2) { - name "${datasource.class.simpleName}.getConnection" - kind INTERNAL - childOf span(1) - attributes { - "$CodeIncubatingAttributes.CODE_NAMESPACE" datasource.class.name - "$CodeIncubatingAttributes.CODE_FUNCTION" "getConnection" - "$DbIncubatingAttributes.DB_SYSTEM" system - "$DbIncubatingAttributes.DB_USER" { user == null | user == it } - "$DbIncubatingAttributes.DB_NAME" "jdbcunittest" - "$DbIncubatingAttributes.DB_CONNECTION_STRING" connectionString - } - } - } - } - } - - where: - datasource | init | system | user | connectionString - new JdbcDataSource() | { ds -> ds.setURL(jdbcUrls.get("h2")) } | "h2" | null | "h2:mem:" - new EmbeddedDataSource() | { ds -> ds.jdbcurl = jdbcUrls.get("derby") } | "derby" | "APP" | "derby:memory:" - cpDatasources.get("hikari").get("h2") | null | "h2" | null | "h2:mem:" - cpDatasources.get("hikari").get("derby") | null | "derby" | "APP" | "derby:memory:" - cpDatasources.get("c3p0").get("h2") | null | "h2" | null | "h2:mem:" - cpDatasources.get("c3p0").get("derby") | null | "derby" | "APP" | "derby:memory:" - - // Tomcat's pool doesn't work because the getConnection method is - // implemented in a parent class that doesn't implement DataSource - - recursive = datasource instanceof EmbeddedDataSource - } - - def "test getClientInfo exception"() { - setup: - Connection connection = new TestConnection(false) - connection.url = "jdbc:testdb://localhost" - - when: - Statement statement = null - runWithSpan("parent") { - statement = connection.createStatement() - return statement.executeQuery(query) - } - - then: - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "DB Query" - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" "other_sql" - "$DbIncubatingAttributes.DB_STATEMENT" "testing ?" - "$DbIncubatingAttributes.DB_CONNECTION_STRING" "testdb://localhost" - "$ServerAttributes.SERVER_ADDRESS" "localhost" - } - } - } - } - - cleanup: - statement?.close() - connection?.close() - - where: - query = "testing 123" - } - - def "should produce proper span name #spanName"() { - setup: - def driver = new TestDriver() - - when: - def connection = driver.connect(url, null) - runWithSpan("parent") { - def statement = connection.createStatement() - return statement.executeQuery(query) - } - - then: - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name spanName - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" "other_sql" - "$DbIncubatingAttributes.DB_NAME" databaseName - "$DbIncubatingAttributes.DB_CONNECTION_STRING" "testdb://localhost" - "$DbIncubatingAttributes.DB_STATEMENT" sanitizedQuery - "$DbIncubatingAttributes.DB_OPERATION" operation - "$DbIncubatingAttributes.DB_SQL_TABLE" table - "$ServerAttributes.SERVER_ADDRESS" "localhost" - } - } - } - } - - where: - url | query | sanitizedQuery | spanName | databaseName | operation | table - "jdbc:testdb://localhost?databaseName=test" | "SELECT * FROM table" | "SELECT * FROM table" | "SELECT test.table" | "test" | "SELECT" | "table" - "jdbc:testdb://localhost?databaseName=test" | "SELECT 42" | "SELECT ?" | "SELECT test" | "test" | "SELECT" | null - "jdbc:testdb://localhost" | "SELECT * FROM table" | "SELECT * FROM table" | "SELECT table" | null | "SELECT" | "table" - "jdbc:testdb://localhost?databaseName=test" | "CREATE TABLE table" | "CREATE TABLE table" | "CREATE TABLE test.table" | "test" | "CREATE TABLE" | "table" - "jdbc:testdb://localhost" | "CREATE TABLE table" | "CREATE TABLE table" | "CREATE TABLE table" | null | "CREATE TABLE" | "table" - } - - def "#connectionPoolName connections should be cached in case of wrapped connections"() { - setup: - String dbType = "hsqldb" - DataSource ds = createDS(connectionPoolName, dbType, jdbcUrls.get(dbType)) - String query = "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS" - int numQueries = 5 - Connection connection = null - int[] res = new int[numQueries] - - when: - for (int i = 0; i < numQueries; ++i) { - try { - connection = ds.getConnection() - def statement = connection.prepareStatement(query) - def rs = statement.executeQuery() - if (rs.next()) { - res[i] = rs.getInt(1) - } else { - res[i] = 0 - } - } finally { - connection.close() - } - } - - then: - for (int i = 0; i < numQueries; ++i) { - res[i] == 3 - } - assertTraces(numQueries) { - for (int i = 0; i < numQueries; ++i) { - trace(i, 1) { - span(0) { - name "SELECT INFORMATION_SCHEMA.SYSTEM_USERS" - kind CLIENT - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" "hsqldb" - "$DbIncubatingAttributes.DB_NAME" dbNameLower - "$DbIncubatingAttributes.DB_USER" "SA" - "$DbIncubatingAttributes.DB_CONNECTION_STRING" "hsqldb:mem:" - "$DbIncubatingAttributes.DB_STATEMENT" "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS" - "$DbIncubatingAttributes.DB_OPERATION" "SELECT" - "$DbIncubatingAttributes.DB_SQL_TABLE" "INFORMATION_SCHEMA.SYSTEM_USERS" - } - } - } - } - } - - cleanup: - if (ds instanceof Closeable) { - ds.close() - } - - where: - connectionPoolName | _ - "hikari" | _ - "tomcat" | _ - "c3p0" | _ - } - - // regression test for https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/2644 - def "should handle recursive Statements inside Connection.getMetaData(): #desc"() { - given: - def connection = new DbCallingConnection(usePreparedStatementInConnection) - connection.url = "jdbc:testdb://localhost" - - when: - runWithSpan("parent") { - executeQueryFunction(connection, "SELECT * FROM table") - } - - then: - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "SELECT table" - kind CLIENT - childOf span(0) - attributes { - "$DbIncubatingAttributes.DB_SYSTEM" "other_sql" - "$DbIncubatingAttributes.DB_CONNECTION_STRING" "testdb://localhost" - "$DbIncubatingAttributes.DB_STATEMENT" "SELECT * FROM table" - "$DbIncubatingAttributes.DB_OPERATION" "SELECT" - "$DbIncubatingAttributes.DB_SQL_TABLE" "table" - "$ServerAttributes.SERVER_ADDRESS" "localhost" - } - } - } - } - - where: - desc | usePreparedStatementInConnection | executeQueryFunction - "getMetaData() uses Statement, test Statement" | false | { con, query -> con.createStatement().executeQuery(query) } - "getMetaData() uses PreparedStatement, test Statement" | true | { con, query -> con.createStatement().executeQuery(query) } - "getMetaData() uses Statement, test PreparedStatement" | false | { con, query -> con.prepareStatement(query).executeQuery() } - "getMetaData() uses PreparedStatement, test PreparedStatement" | true | { con, query -> con.prepareStatement(query).executeQuery() } - } - - class DbCallingConnection extends TestConnection { - final boolean usePreparedStatement - - DbCallingConnection(boolean usePreparedStatement) { - super(false) - this.usePreparedStatement = usePreparedStatement - } - - @Override - DatabaseMetaData getMetaData() throws SQLException { - // simulate retrieving DB metadata from the DB itself - if (usePreparedStatement) { - prepareStatement("SELECT * from DB_METADATA").executeQuery() - } else { - createStatement().executeQuery("SELECT * from DB_METADATA") - } - return super.getMetaData() - } - } - - // regression test for https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/6015 - def "test proxy statement"() { - def connection = new Driver().connect(jdbcUrls.get("h2"), null) - Statement statement = connection.createStatement() - Statement proxyStatement = ProxyStatementFactory.proxyStatement(statement) - ResultSet resultSet = runWithSpan("parent") { - return proxyStatement.executeQuery("SELECT 3") - } - - expect: - resultSet.next() - resultSet.getInt(1) == 3 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "SELECT $dbNameLower" - kind CLIENT - childOf span(0) - } - } - } - - cleanup: - statement.close() - connection.close() - } - - // regression test for https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/9359 - def "test proxy prepared statement"() { - def connection = new Driver().connect(jdbcUrls.get("h2"), null) - PreparedStatement statement = connection.prepareStatement("SELECT 3") - PreparedStatement proxyStatement = ProxyStatementFactory.proxyPreparedStatement(statement) - ResultSet resultSet = runWithSpan("parent") { - return proxyStatement.executeQuery() - } - - expect: - resultSet.next() - resultSet.getInt(1) == 3 - assertTraces(1) { - trace(0, 2) { - span(0) { - name "parent" - kind SpanKind.INTERNAL - hasNoParent() - } - span(1) { - name "SELECT $dbNameLower" - kind CLIENT - childOf span(0) - } - } - } - - cleanup: - statement.close() - connection.close() - } -} diff --git a/instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/DbCallingConnection.java b/instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/DbCallingConnection.java new file mode 100644 index 000000000000..356199e5c76b --- /dev/null +++ b/instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/DbCallingConnection.java @@ -0,0 +1,30 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.jdbc.test; + +import io.opentelemetry.instrumentation.jdbc.TestConnection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; + +class DbCallingConnection extends TestConnection { + final boolean usePreparedStatement; + + DbCallingConnection(boolean usePreparedStatement) { + super(false); + this.usePreparedStatement = usePreparedStatement; + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + // simulate retrieving DB metadata from the DB itself + if (usePreparedStatement) { + prepareStatement("SELECT * from DB_METADATA").executeQuery(); + } else { + createStatement().executeQuery("SELECT * from DB_METADATA"); + } + return super.getMetaData(); + } +} diff --git a/instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/JdbcInstrumentationTest.java b/instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/JdbcInstrumentationTest.java new file mode 100644 index 000000000000..d89414baa55a --- /dev/null +++ b/instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/JdbcInstrumentationTest.java @@ -0,0 +1,1341 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.jdbc.test; + +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import com.mchange.v2.c3p0.ComboPooledDataSource; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.jdbc.TestConnection; +import io.opentelemetry.instrumentation.jdbc.TestDriver; +import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; +import io.opentelemetry.sdk.testing.assertj.TraceAssert; +import io.opentelemetry.semconv.ServerAttributes; +import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes; +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; +import java.beans.PropertyVetoException; +import java.io.Closeable; +import java.io.IOException; +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.function.Consumer; +import java.util.stream.Stream; +import javax.sql.DataSource; +import org.apache.derby.jdbc.EmbeddedDataSource; +import org.apache.derby.jdbc.EmbeddedDriver; +import org.h2.jdbcx.JdbcDataSource; +import org.hsqldb.jdbc.JDBCDriver; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +class JdbcInstrumentationTest { + + @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); + + @RegisterExtension + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + @SuppressWarnings("deprecation") // TODO DbIncubatingAttributes.DB_CONNECTION_STRING deprecation + static final AttributeKey DB_CONNECTION_STRING = + DbIncubatingAttributes.DB_CONNECTION_STRING; + + private static final String dbName = "jdbcUnitTest"; + private static final String dbNameLower = dbName.toLowerCase(Locale.ROOT); + private static final Map jdbcUrls = + ImmutableMap.of( + "h2", "jdbc:h2:mem:" + dbName, + "derby", "jdbc:derby:memory:" + dbName, + "hsqldb", "jdbc:hsqldb:mem:" + dbName); + private static final Map jdbcDriverClassNames = + ImmutableMap.of( + "h2", "org.h2.Driver", + "derby", "org.apache.derby.jdbc.EmbeddedDriver", + "hsqldb", "org.hsqldb.jdbc.JDBCDriver"); + private static final Map jdbcUserNames = Maps.newHashMap(); + private static final Properties connectionProps = new Properties(); + // JDBC Connection pool name (i.e. HikariCP) -> Map + private static final Map> cpDatasources = Maps.newHashMap(); + + static { + jdbcUserNames.put("derby", "APP"); + jdbcUserNames.put("h2", null); + jdbcUserNames.put("hsqldb", "SA"); + + connectionProps.put("databaseName", "someDb"); + connectionProps.put("OPEN_NEW", "true"); // So H2 doesn't complain about username/password. + } + + @BeforeAll + static void setUp() { + prepareConnectionPoolDatasources(); + } + + @AfterAll + static void tearDown() { + cpDatasources + .values() + .forEach( + k -> + k.values() + .forEach( + dataSource -> { + if (dataSource instanceof Closeable) { + try { + ((Closeable) dataSource).close(); + } catch (IOException ignore) { + // ignore + } + } + })); + } + + static void prepareConnectionPoolDatasources() { + List connectionPoolNames = asList("tomcat", "hikari", "c3p0"); + connectionPoolNames.forEach( + cpName -> { + Map dbDsMapping = new HashMap<>(); + jdbcUrls.forEach( + (dbType, jdbcUrl) -> dbDsMapping.put(dbType, createDs(cpName, dbType, jdbcUrl))); + cpDatasources.put(cpName, dbDsMapping); + }); + } + + static DataSource createTomcatDs(String dbType, String jdbcUrl) { + org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); + String jdbcUrlToSet = dbType.equals("derby") ? jdbcUrl + ";create=true" : jdbcUrl; + ds.setUrl(jdbcUrlToSet); + ds.setDriverClassName(jdbcDriverClassNames.get(dbType)); + String username = jdbcUserNames.get(dbType); + if (username != null) { + ds.setUsername(username); + } + ds.setPassword(""); + ds.setMaxActive(1); // to test proper caching, having > 1 max active connection will be hard to + // determine whether the connection is properly cached + return ds; + } + + static DataSource createHikariDs(String dbType, String jdbcUrl) { + HikariConfig config = new HikariConfig(); + String jdbcUrlToSet = dbType.equals("derby") ? jdbcUrl + ";create=true" : jdbcUrl; + config.setJdbcUrl(jdbcUrlToSet); + String username = jdbcUserNames.get(dbType); + if (username != null) { + config.setUsername(username); + } + config.setPassword(""); + config.addDataSourceProperty("cachePrepStmts", "true"); + config.addDataSourceProperty("prepStmtCacheSize", "250"); + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); + config.setMaximumPoolSize(1); + + return new HikariDataSource(config); + } + + static DataSource createC3P0Ds(String dbType, String jdbcUrl) { + ComboPooledDataSource ds = new ComboPooledDataSource(); + try { + ds.setDriverClass(jdbcDriverClassNames.get(dbType)); + } catch (PropertyVetoException e) { + throw new RuntimeException(e); + } + String jdbcUrlToSet = dbType.equals("derby") ? jdbcUrl + ";create=true" : jdbcUrl; + ds.setJdbcUrl(jdbcUrlToSet); + String username = jdbcUserNames.get(dbType); + if (username != null) { + ds.setUser(username); + } + ds.setPassword(""); + ds.setMaxPoolSize(1); + return ds; + } + + static DataSource createDs(String connectionPoolName, String dbType, String jdbcUrl) { + DataSource ds = null; + if (connectionPoolName.equals("tomcat")) { + ds = createTomcatDs(dbType, jdbcUrl); + } + if (connectionPoolName.equals("hikari")) { + ds = createHikariDs(dbType, jdbcUrl); + } + if (connectionPoolName.equals("c3p0")) { + ds = createC3P0Ds(dbType, jdbcUrl); + } + return ds; + } + + static Stream basicStatementStream() throws SQLException { + return Stream.of( + Arguments.of( + "h2", + new org.h2.Driver().connect(jdbcUrls.get("h2"), null), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + new EmbeddedDriver().connect(jdbcUrls.get("derby"), null), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "hsqldb", + new JDBCDriver().connect(jdbcUrls.get("hsqldb"), null), + "SA", + "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT INFORMATION_SCHEMA.SYSTEM_USERS", + "hsqldb:mem:", + "INFORMATION_SCHEMA.SYSTEM_USERS"), + Arguments.of( + "h2", + new org.h2.Driver().connect(jdbcUrls.get("h2"), connectionProps), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + new EmbeddedDriver().connect(jdbcUrls.get("derby"), connectionProps), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "hsqldb", + new JDBCDriver().connect(jdbcUrls.get("hsqldb"), connectionProps), + "SA", + "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT INFORMATION_SCHEMA.SYSTEM_USERS", + "hsqldb:mem:", + "INFORMATION_SCHEMA.SYSTEM_USERS"), + Arguments.of( + "h2", + cpDatasources.get("tomcat").get("h2").getConnection(), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + cpDatasources.get("tomcat").get("derby").getConnection(), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "hsqldb", + cpDatasources.get("tomcat").get("hsqldb").getConnection(), + "SA", + "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT INFORMATION_SCHEMA.SYSTEM_USERS", + "hsqldb:mem:", + "INFORMATION_SCHEMA.SYSTEM_USERS"), + Arguments.of( + "h2", + cpDatasources.get("hikari").get("h2").getConnection(), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + cpDatasources.get("hikari").get("derby").getConnection(), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "hsqldb", + cpDatasources.get("hikari").get("hsqldb").getConnection(), + "SA", + "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT INFORMATION_SCHEMA.SYSTEM_USERS", + "hsqldb:mem:", + "INFORMATION_SCHEMA.SYSTEM_USERS"), + Arguments.of( + "h2", + cpDatasources.get("c3p0").get("h2").getConnection(), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + cpDatasources.get("c3p0").get("derby").getConnection(), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "hsqldb", + cpDatasources.get("c3p0").get("hsqldb").getConnection(), + "SA", + "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS", + "SELECT INFORMATION_SCHEMA.SYSTEM_USERS", + "hsqldb:mem:", + "INFORMATION_SCHEMA.SYSTEM_USERS")); + } + + @ParameterizedTest + @MethodSource("basicStatementStream") + public void testBasicStatement( + String system, + Connection connection, + String username, + String query, + String sanitizedQuery, + String spanName, + String url, + String table) + throws SQLException { + Statement statement = connection.createStatement(); + cleanup.deferCleanup(statement); + ResultSet resultSet = testing.runWithSpan("parent", () -> statement.executeQuery(query)); + + resultSet.next(); + assertThat(resultSet.getInt(1)).isEqualTo(3); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (username != null) { + val.isEqualTo(username); + } + }), + equalTo(DB_CONNECTION_STRING, url), + equalTo(DbIncubatingAttributes.DB_STATEMENT, sanitizedQuery), + equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table)))); + } + + static Stream preparedStatementStream() throws SQLException { + return Stream.of( + Arguments.of( + "h2", + new org.h2.Driver().connect(jdbcUrls.get("h2"), null), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + new EmbeddedDriver().connect(jdbcUrls.get("derby"), null), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "h2", + cpDatasources.get("tomcat").get("h2").getConnection(), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + cpDatasources.get("tomcat").get("derby").getConnection(), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "h2", + cpDatasources.get("hikari").get("h2").getConnection(), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + cpDatasources.get("hikari").get("derby").getConnection(), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + "h2", + cpDatasources.get("c3p0").get("h2").getConnection(), + null, + "SELECT 3", + "SELECT ?", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + "derby", + cpDatasources.get("c3p0").get("derby").getConnection(), + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1")); + } + + @ParameterizedTest + @MethodSource("preparedStatementStream") + void testPreparedStatementExecute( + String system, + Connection connection, + String username, + String query, + String sanitizedQuery, + String spanName, + String url, + String table) + throws SQLException { + PreparedStatement statement = connection.prepareStatement(query); + cleanup.deferCleanup(statement); + ResultSet resultSet = + testing.runWithSpan( + "parent", + () -> { + statement.execute(); + return statement.getResultSet(); + }); + + resultSet.next(); + assertThat(resultSet.getInt(1)).isEqualTo(3); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (username != null) { + val.isEqualTo(username); + } + }), + equalTo(DB_CONNECTION_STRING, url), + equalTo(DbIncubatingAttributes.DB_STATEMENT, sanitizedQuery), + equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table)))); + } + + @ParameterizedTest + @MethodSource("preparedStatementStream") + void testPreparedStatementQuery( + String system, + Connection connection, + String username, + String query, + String sanitizedQuery, + String spanName, + String url, + String table) + throws SQLException { + PreparedStatement statement = connection.prepareStatement(query); + cleanup.deferCleanup(statement); + ResultSet resultSet = testing.runWithSpan("parent", () -> statement.executeQuery()); + + resultSet.next(); + assertThat(resultSet.getInt(1)).isEqualTo(3); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (username != null) { + val.isEqualTo(username); + } + }), + equalTo(DB_CONNECTION_STRING, url), + equalTo(DbIncubatingAttributes.DB_STATEMENT, sanitizedQuery), + equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table)))); + } + + @ParameterizedTest + @MethodSource("preparedStatementStream") + void testPreparedCall( + String system, + Connection connection, + String username, + String query, + String sanitizedQuery, + String spanName, + String url, + String table) + throws SQLException { + CallableStatement statement = connection.prepareCall(query); + cleanup.deferCleanup(statement); + ResultSet resultSet = testing.runWithSpan("parent", () -> statement.executeQuery()); + + resultSet.next(); + assertThat(resultSet.getInt(1)).isEqualTo(3); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (username != null) { + val.isEqualTo(username); + } + }), + equalTo(DB_CONNECTION_STRING, url), + equalTo(DbIncubatingAttributes.DB_STATEMENT, sanitizedQuery), + equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table)))); + } + + static Stream statementUpdateStream() throws SQLException { + return Stream.of( + Arguments.of( + "h2", + new org.h2.Driver().connect(jdbcUrls.get("h2"), null), + null, + "CREATE TABLE S_H2 (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_H2", + "h2:mem:", + "S_H2"), + Arguments.of( + "derby", + new EmbeddedDriver().connect(jdbcUrls.get("derby"), null), + "APP", + "CREATE TABLE S_DERBY (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_DERBY", + "derby:memory:", + "S_DERBY"), + Arguments.of( + "hsqldb", + new JDBCDriver().connect(jdbcUrls.get("hsqldb"), null), + "SA", + "CREATE TABLE PUBLIC.S_HSQLDB (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE PUBLIC.S_HSQLDB", + "hsqldb:mem:", + "PUBLIC.S_HSQLDB"), + Arguments.of( + "h2", + cpDatasources.get("tomcat").get("h2").getConnection(), + null, + "CREATE TABLE S_H2_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_H2_TOMCAT", + "h2:mem:", + "S_H2_TOMCAT"), + Arguments.of( + "derby", + cpDatasources.get("tomcat").get("derby").getConnection(), + "APP", + "CREATE TABLE S_DERBY_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_DERBY_TOMCAT", + "derby:memory:", + "S_DERBY_TOMCAT"), + Arguments.of( + "hsqldb", + cpDatasources.get("tomcat").get("hsqldb").getConnection(), + "SA", + "CREATE TABLE PUBLIC.S_HSQLDB_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE PUBLIC.S_HSQLDB_TOMCAT", + "hsqldb:mem:", + "PUBLIC.S_HSQLDB_TOMCAT"), + Arguments.of( + "h2", + cpDatasources.get("hikari").get("h2").getConnection(), + null, + "CREATE TABLE S_H2_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_H2_HIKARI", + "h2:mem:", + "S_H2_HIKARI"), + Arguments.of( + "derby", + cpDatasources.get("hikari").get("derby").getConnection(), + "APP", + "CREATE TABLE S_DERBY_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_DERBY_HIKARI", + "derby:memory:", + "S_DERBY_HIKARI"), + Arguments.of( + "hsqldb", + cpDatasources.get("hikari").get("hsqldb").getConnection(), + "SA", + "CREATE TABLE PUBLIC.S_HSQLDB_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE PUBLIC.S_HSQLDB_HIKARI", + "hsqldb:mem:", + "PUBLIC.S_HSQLDB_HIKARI"), + Arguments.of( + "h2", + cpDatasources.get("c3p0").get("h2").getConnection(), + null, + "CREATE TABLE S_H2_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_H2_C3P0", + "h2:mem:", + "S_H2_C3P0"), + Arguments.of( + "derby", + cpDatasources.get("c3p0").get("derby").getConnection(), + "APP", + "CREATE TABLE S_DERBY_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.S_DERBY_C3P0", + "derby:memory:", + "S_DERBY_C3P0"), + Arguments.of( + "hsqldb", + cpDatasources.get("c3p0").get("hsqldb").getConnection(), + "SA", + "CREATE TABLE PUBLIC.S_HSQLDB_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE PUBLIC.S_HSQLDB_C3P0", + "hsqldb:mem:", + "PUBLIC.S_HSQLDB_C3P0")); + } + + @ParameterizedTest + @MethodSource("statementUpdateStream") + void testStatementUpdate( + String system, + Connection connection, + String username, + String query, + String spanName, + String url, + String table) + throws SQLException { + Statement statement = connection.createStatement(); + cleanup.deferCleanup(statement); + String sql = connection.nativeSQL(query); + testing.runWithSpan("parent", () -> assertThat(statement.execute(sql)).isFalse()); + + assertThat(statement.getUpdateCount()).isEqualTo(0); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (username != null) { + val.isEqualTo(username); + } + }), + equalTo(DB_CONNECTION_STRING, url), + equalTo(DbIncubatingAttributes.DB_STATEMENT, query), + equalTo(DbIncubatingAttributes.DB_OPERATION, "CREATE TABLE"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table)))); + } + + static Stream preparedStatementUpdateStream() throws SQLException { + return Stream.of( + Arguments.of( + "h2", + new org.h2.Driver().connect(jdbcUrls.get("h2"), null), + null, + "CREATE TABLE PS_H2 (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_H2", + "h2:mem:", + "PS_H2"), + Arguments.of( + "derby", + new EmbeddedDriver().connect(jdbcUrls.get("derby"), null), + "APP", + "CREATE TABLE PS_DERBY (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_DERBY", + "derby:memory:", + "PS_DERBY"), + Arguments.of( + "h2", + cpDatasources.get("tomcat").get("h2").getConnection(), + null, + "CREATE TABLE PS_H2_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_H2_TOMCAT", + "h2:mem:", + "PS_H2_TOMCAT"), + Arguments.of( + "derby", + cpDatasources.get("tomcat").get("derby").getConnection(), + "APP", + "CREATE TABLE PS_DERBY_TOMCAT (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_DERBY_TOMCAT", + "derby:memory:", + "PS_DERBY_TOMCAT"), + Arguments.of( + "h2", + cpDatasources.get("hikari").get("h2").getConnection(), + null, + "CREATE TABLE PS_H2_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_H2_HIKARI", + "h2:mem:", + "PS_H2_HIKARI"), + Arguments.of( + "derby", + cpDatasources.get("hikari").get("derby").getConnection(), + "APP", + "CREATE TABLE PS_DERBY_HIKARI (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_DERBY_HIKARI", + "derby:memory:", + "PS_DERBY_HIKARI"), + Arguments.of( + "h2", + cpDatasources.get("c3p0").get("h2").getConnection(), + null, + "CREATE TABLE PS_H2_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_H2_C3P0", + "h2:mem:", + "PS_H2_C3P0"), + Arguments.of( + "derby", + cpDatasources.get("c3p0").get("derby").getConnection(), + "APP", + "CREATE TABLE PS_DERBY_C3P0 (id INTEGER not NULL, PRIMARY KEY ( id ))", + "CREATE TABLE jdbcunittest.PS_DERBY_C3P0", + "derby:memory:", + "PS_DERBY_C3P0")); + } + + @ParameterizedTest + @MethodSource("preparedStatementUpdateStream") + void testPreparedStatementUpdate( + String system, + Connection connection, + String username, + String query, + String spanName, + String url, + String table) + throws SQLException { + String sql = connection.nativeSQL(query); + PreparedStatement statement = connection.prepareStatement(sql); + cleanup.deferCleanup(statement); + testing.runWithSpan("parent", () -> assertThat(statement.executeUpdate()).isEqualTo(0)); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (username != null) { + val.isEqualTo(username); + } + }), + equalTo(DB_CONNECTION_STRING, url), + equalTo(DbIncubatingAttributes.DB_STATEMENT, query), + equalTo(DbIncubatingAttributes.DB_OPERATION, "CREATE TABLE"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table)))); + } + + static Stream connectionConstructorStream() { + return Stream.of( + Arguments.of( + true, + "h2", + new org.h2.Driver(), + "jdbc:h2:mem:" + dbName, + null, + "SELECT 3;", + "SELECT ?;", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + true, + "derby", + new EmbeddedDriver(), + "jdbc:derby:memory:" + dbName + ";create=true", + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1"), + Arguments.of( + false, + "h2", + new org.h2.Driver(), + "jdbc:h2:mem:" + dbName, + null, + "SELECT 3;", + "SELECT ?;", + "SELECT " + dbNameLower, + "h2:mem:", + null), + Arguments.of( + false, + "derby", + new EmbeddedDriver(), + "jdbc:derby:memory:" + dbName + ";create=true", + "APP", + "SELECT 3 FROM SYSIBM.SYSDUMMY1", + "SELECT ? FROM SYSIBM.SYSDUMMY1", + "SELECT SYSIBM.SYSDUMMY1", + "derby:memory:", + "SYSIBM.SYSDUMMY1")); + } + + @SuppressWarnings("CatchingUnchecked") + @ParameterizedTest + @MethodSource("connectionConstructorStream") + void testConnectionConstructorThrowing( + boolean prepareStatement, + String system, + Driver driver, + String jdbcUrl, + String username, + String query, + String sanitizedQuery, + String spanName, + String url, + String table) + throws SQLException { + Connection connection = null; + + try { + connection = new TestConnection(true); + } catch (Exception ignored) { + connection = driver.connect(jdbcUrl, null); + } + cleanup.deferCleanup(connection); + Connection finalConnection = connection; + ResultSet rs = + testing.runWithSpan( + "parent", + () -> { + if (prepareStatement) { + PreparedStatement stmt = finalConnection.prepareStatement(query); + cleanup.deferCleanup(stmt); + return stmt.executeQuery(); + } else { + Statement stmt = finalConnection.createStatement(); + cleanup.deferCleanup(stmt); + return stmt.executeQuery(query); + } + }); + + rs.next(); + assertThat(rs.getInt(1)).isEqualTo(3); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (username != null) { + val.isEqualTo(username); + } + }), + equalTo(DB_CONNECTION_STRING, url), + equalTo(DbIncubatingAttributes.DB_STATEMENT, sanitizedQuery), + equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table)))); + } + + static Stream getConnectionStream() { + return Stream.of( + Arguments.of( + new JdbcDataSource(), + (Consumer) ds -> ((JdbcDataSource) ds).setUrl(jdbcUrls.get("h2")), + "h2", + null, + "h2:mem:"), + Arguments.of( + new EmbeddedDataSource(), + (Consumer) + ds -> ((EmbeddedDataSource) ds).setDatabaseName("memory:" + dbName), + "derby", + "APP", + "derby:memory:"), + Arguments.of(cpDatasources.get("hikari").get("h2"), null, "h2", null, "h2:mem:"), + Arguments.of( + cpDatasources.get("hikari").get("derby"), null, "derby", "APP", "derby:memory:"), + Arguments.of(cpDatasources.get("c3p0").get("h2"), null, "h2", null, "h2:mem:"), + Arguments.of( + cpDatasources.get("c3p0").get("derby"), null, "derby", "APP", "derby:memory:")); + } + + @ParameterizedTest(autoCloseArguments = false) + @MethodSource("getConnectionStream") + void testGetConnection( + DataSource datasource, + Consumer init, + String system, + String user, + String connectionString) + throws SQLException { + // Tomcat's pool doesn't work because the getConnection method is + // implemented in a parent class that doesn't implement DataSource + boolean recursive = datasource instanceof EmbeddedDataSource; + + if (init != null) { + init.accept(datasource); + } + datasource.getConnection().close(); + assertThat(testing.spans()).noneMatch(span -> span.getName().equals("database.connection")); + + testing.clearData(); + + testing.runWithSpan("parent", () -> datasource.getConnection().close()); + testing.waitAndAssertTraces( + trace -> { + List> assertions = + new ArrayList<>( + Arrays.asList( + span1 -> span1.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span1 -> + span1 + .hasName(datasource.getClass().getSimpleName() + ".getConnection") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo( + CodeIncubatingAttributes.CODE_NAMESPACE, + datasource.getClass().getName()), + equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "getConnection"), + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (user != null) { + val.isEqualTo(user); + } + }), + equalTo(DbIncubatingAttributes.DB_NAME, "jdbcunittest"), + equalTo(DB_CONNECTION_STRING, connectionString)))); + if (recursive) { + assertions.add( + span -> + span.hasName(datasource.getClass().getSimpleName() + ".getConnection") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(1)) + .hasAttributesSatisfying( + equalTo( + CodeIncubatingAttributes.CODE_NAMESPACE, + datasource.getClass().getName()), + equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "getConnection"), + equalTo(DbIncubatingAttributes.DB_SYSTEM, system), + satisfies( + DbIncubatingAttributes.DB_USER, + val -> { + if (user != null) { + val.isEqualTo(user); + } + }), + equalTo(DbIncubatingAttributes.DB_NAME, "jdbcunittest"), + equalTo(DB_CONNECTION_STRING, connectionString))); + } + trace.hasSpansSatisfyingExactly(assertions); + }); + } + + @ParameterizedTest + @DisplayName("test getClientInfo exception") + @ValueSource(strings = "testing 123") + void testGetClientInfoException(String query) throws SQLException { + TestConnection connection = new TestConnection(false); + cleanup.deferCleanup(connection); + connection.setUrl("jdbc:testdb://localhost"); + + Statement statement = + testing.runWithSpan( + "parent", + () -> { + Statement stmt = connection.createStatement(); + stmt.executeQuery(query); + return stmt; + }); + cleanup.deferCleanup(statement); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("DB Query") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, "other_sql"), + equalTo(DbIncubatingAttributes.DB_STATEMENT, "testing ?"), + equalTo(DB_CONNECTION_STRING, "testdb://localhost"), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost")))); + } + + static Stream spanNameStream() { + return Stream.of( + Arguments.of( + "jdbc:testdb://localhost?databaseName=test", + "SELECT * FROM table", + "SELECT * FROM table", + "SELECT test.table", + "test", + "SELECT", + "table"), + Arguments.of( + "jdbc:testdb://localhost?databaseName=test", + "SELECT 42", + "SELECT ?", + "SELECT test", + "test", + "SELECT", + null), + Arguments.of( + "jdbc:testdb://localhost", + "SELECT * FROM table", + "SELECT * FROM table", + "SELECT table", + null, + "SELECT", + "table"), + Arguments.of( + "jdbc:testdb://localhost?databaseName=test", + "CREATE TABLE table", + "CREATE TABLE table", + "CREATE TABLE test.table", + "test", + "CREATE TABLE", + "table"), + Arguments.of( + "jdbc:testdb://localhost", + "CREATE TABLE table", + "CREATE TABLE table", + "CREATE TABLE table", + null, + "CREATE TABLE", + "table")); + } + + @ParameterizedTest + @MethodSource("spanNameStream") + void testProduceProperSpanName( + String url, + String query, + String sanitizedQuery, + String spanName, + String databaseName, + String operation, + String table) + throws SQLException { + Driver driver = new TestDriver(); + Connection connection = driver.connect(url, null); + cleanup.deferCleanup(connection); + + testing.runWithSpan( + "parent", + () -> { + Statement statement = connection.createStatement(); + statement.executeQuery(query); + }); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName(spanName) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, "other_sql"), + equalTo(DbIncubatingAttributes.DB_NAME, databaseName), + equalTo(DB_CONNECTION_STRING, "testdb://localhost"), + equalTo(DbIncubatingAttributes.DB_STATEMENT, sanitizedQuery), + equalTo(DbIncubatingAttributes.DB_OPERATION, operation), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, table), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost")))); + } + + @ParameterizedTest + @ValueSource(strings = {"hikari", "tomcat", "c3p0"}) + void testConnectionCached(String connectionPoolName) throws SQLException { + String dbType = "hsqldb"; + DataSource ds = createDs(connectionPoolName, dbType, jdbcUrls.get(dbType)); + cleanup.deferCleanup( + () -> { + if (ds instanceof Closeable) { + ((Closeable) ds).close(); + } + }); + String query = "SELECT 3 FROM INFORMATION_SCHEMA.SYSTEM_USERS"; + int numQueries = 5; + int[] res = new int[numQueries]; + + for (int i = 0; i < numQueries; ++i) { + try (Connection connection = ds.getConnection(); + PreparedStatement statement = connection.prepareStatement(query)) { + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + res[i] = rs.getInt(1); + } else { + res[i] = 0; + } + } + } + + for (int i = 0; i < numQueries; ++i) { + assertThat(res[i]).isEqualTo(3); + } + + List> assertions = new ArrayList<>(); + Consumer traceAssertConsumer = + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("SELECT INFORMATION_SCHEMA.SYSTEM_USERS") + .hasKind(SpanKind.CLIENT) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, "hsqldb"), + equalTo(DbIncubatingAttributes.DB_NAME, dbNameLower), + equalTo(DbIncubatingAttributes.DB_USER, "SA"), + equalTo(DB_CONNECTION_STRING, "hsqldb:mem:"), + equalTo( + DbIncubatingAttributes.DB_STATEMENT, + "SELECT ? FROM INFORMATION_SCHEMA.SYSTEM_USERS"), + equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"), + equalTo( + DbIncubatingAttributes.DB_SQL_TABLE, + "INFORMATION_SCHEMA.SYSTEM_USERS"))); + for (int i = 0; i < numQueries; i++) { + assertions.add(traceAssertConsumer); + } + + testing.waitAndAssertTraces(assertions); + } + + @FunctionalInterface + public interface ThrowingBiConsumer { + void accept(T t, U u) throws Exception; + } + + static Stream recursiveStatementsStream() { + return Stream.of( + Arguments.of( + "getMetaData() uses Statement, test Statement", + false, + (ThrowingBiConsumer) + (con, query) -> con.createStatement().executeQuery(query)), + Arguments.of( + "getMetaData() uses PreparedStatement, test Statement", + true, + (ThrowingBiConsumer) + (con, query) -> con.createStatement().executeQuery(query)), + Arguments.of( + "getMetaData() uses Statement, test PreparedStatement", + false, + (ThrowingBiConsumer) + (con, query) -> con.prepareStatement(query).executeQuery()), + Arguments.of( + "getMetaData() uses PreparedStatement, test PreparedStatement", + true, + (ThrowingBiConsumer) + (con, query) -> con.prepareStatement(query).executeQuery())); + } + + // regression test for + // https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/2644 + @ParameterizedTest + @MethodSource("recursiveStatementsStream") + void testHandleRecursiveStatements( + String desc, + boolean usePreparedStatementInConnection, + ThrowingBiConsumer executeQueryFunction) + throws Exception { + DbCallingConnection connection = new DbCallingConnection(usePreparedStatementInConnection); + connection.setUrl("jdbc:testdb://localhost"); + + testing.runWithSpan( + "parent", + () -> { + executeQueryFunction.accept(connection, "SELECT * FROM table"); + }); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("SELECT table") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfying( + equalTo(DbIncubatingAttributes.DB_SYSTEM, "other_sql"), + equalTo(DB_CONNECTION_STRING, "testdb://localhost"), + equalTo(DbIncubatingAttributes.DB_STATEMENT, "SELECT * FROM table"), + equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"), + equalTo(DbIncubatingAttributes.DB_SQL_TABLE, "table"), + equalTo(ServerAttributes.SERVER_ADDRESS, "localhost")))); + } + + // regression test for + // https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/6015 + @DisplayName("test proxy statement") + @Test + void testProxyStatement() throws Exception { + Connection connection = new org.h2.Driver().connect(jdbcUrls.get("h2"), null); + Statement statement = connection.createStatement(); + cleanup.deferCleanup(statement); + cleanup.deferCleanup(connection); + + Statement proxyStatement = ProxyStatementFactory.proxyStatement(statement); + ResultSet resultSet = + testing.runWithSpan("parent", () -> proxyStatement.executeQuery("SELECT 3")); + + resultSet.next(); + assertThat(resultSet.getInt(1)).isEqualTo(3); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("SELECT " + dbNameLower) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)))); + } + + // regression test for + // https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/9359 + @DisplayName("test proxy prepared statement") + @Test + void testProxyPreparedStatement() throws SQLException { + Connection connection = new org.h2.Driver().connect(jdbcUrls.get("h2"), null); + PreparedStatement statement = connection.prepareStatement("SELECT 3"); + cleanup.deferCleanup(statement); + cleanup.deferCleanup(connection); + + PreparedStatement proxyStatement = ProxyStatementFactory.proxyPreparedStatement(statement); + ResultSet resultSet = testing.runWithSpan("parent", () -> proxyStatement.executeQuery()); + + resultSet.next(); + assertThat(resultSet.getInt(1)).isEqualTo(3); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("SELECT " + dbNameLower) + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)))); + } +} diff --git a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/AnotherTestDriver.groovy b/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/AnotherTestDriver.groovy deleted file mode 100644 index 0658c9bd318c..000000000000 --- a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/AnotherTestDriver.groovy +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.jdbc - - -import java.sql.Connection -import java.sql.Driver -import java.sql.DriverPropertyInfo -import java.sql.SQLException -import java.sql.SQLFeatureNotSupportedException -import java.util.logging.Logger - -class AnotherTestDriver implements Driver { - @Override - Connection connect(String url, Properties info) throws SQLException { - return null - } - - @Override - boolean acceptsURL(String url) throws SQLException { - return false - } - - @Override - DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { - return new DriverPropertyInfo[0] - } - - @Override - int getMajorVersion() { - return 0 - } - - @Override - int getMinorVersion() { - return 0 - } - - @Override - boolean jdbcCompliant() { - return false - } - - @Override - Logger getParentLogger() throws SQLFeatureNotSupportedException { - return null - } -} diff --git a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestCallableStatement.groovy b/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestCallableStatement.groovy deleted file mode 100644 index a39a259fc560..000000000000 --- a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestCallableStatement.groovy +++ /dev/null @@ -1,587 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.jdbc - - -import java.sql.Array -import java.sql.Blob -import java.sql.CallableStatement -import java.sql.Clob -import java.sql.Date -import java.sql.NClob -import java.sql.Ref -import java.sql.RowId -import java.sql.SQLException -import java.sql.SQLXML -import java.sql.Time -import java.sql.Timestamp - -class TestCallableStatement extends TestPreparedStatement implements CallableStatement { - @Override - void registerOutParameter(int parameterIndex, int sqlType) throws SQLException { - - } - - @Override - void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException { - - } - - @Override - boolean wasNull() throws SQLException { - return false - } - - @Override - String getString(int parameterIndex) throws SQLException { - return null - } - - @Override - boolean getBoolean(int parameterIndex) throws SQLException { - return false - } - - @Override - byte getByte(int parameterIndex) throws SQLException { - return 0 - } - - @Override - short getShort(int parameterIndex) throws SQLException { - return 0 - } - - @Override - int getInt(int parameterIndex) throws SQLException { - return 0 - } - - @Override - long getLong(int parameterIndex) throws SQLException { - return 0 - } - - @Override - float getFloat(int parameterIndex) throws SQLException { - return 0 - } - - @Override - double getDouble(int parameterIndex) throws SQLException { - return 0 - } - - @Override - BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException { - return null - } - - @Override - byte[] getBytes(int parameterIndex) throws SQLException { - return new byte[0] - } - - @Override - Date getDate(int parameterIndex) throws SQLException { - return null - } - - @Override - Time getTime(int parameterIndex) throws SQLException { - return null - } - - @Override - Timestamp getTimestamp(int parameterIndex) throws SQLException { - return null - } - - @Override - Object getObject(int parameterIndex) throws SQLException { - return null - } - - @Override - BigDecimal getBigDecimal(int parameterIndex) throws SQLException { - return null - } - - @Override - Object getObject(int parameterIndex, Map> map) throws SQLException { - return null - } - - @Override - Ref getRef(int parameterIndex) throws SQLException { - return null - } - - @Override - Blob getBlob(int parameterIndex) throws SQLException { - return null - } - - @Override - Clob getClob(int parameterIndex) throws SQLException { - return null - } - - @Override - Array getArray(int parameterIndex) throws SQLException { - return null - } - - @Override - Date getDate(int parameterIndex, Calendar cal) throws SQLException { - return null - } - - @Override - Time getTime(int parameterIndex, Calendar cal) throws SQLException { - return null - } - - @Override - Timestamp getTimestamp(int parameterIndex, Calendar cal) throws SQLException { - return null - } - - @Override - void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException { - - } - - @Override - void registerOutParameter(String parameterName, int sqlType) throws SQLException { - - } - - @Override - void registerOutParameter(String parameterName, int sqlType, int scale) throws SQLException { - - } - - @Override - void registerOutParameter(String parameterName, int sqlType, String typeName) throws SQLException { - - } - - @Override - URL getURL(int parameterIndex) throws SQLException { - return null - } - - @Override - void setURL(String parameterName, URL val) throws SQLException { - - } - - @Override - void setNull(String parameterName, int sqlType) throws SQLException { - - } - - @Override - void setBoolean(String parameterName, boolean x) throws SQLException { - - } - - @Override - void setByte(String parameterName, byte x) throws SQLException { - - } - - @Override - void setShort(String parameterName, short x) throws SQLException { - - } - - @Override - void setInt(String parameterName, int x) throws SQLException { - - } - - @Override - void setLong(String parameterName, long x) throws SQLException { - - } - - @Override - void setFloat(String parameterName, float x) throws SQLException { - - } - - @Override - void setDouble(String parameterName, double x) throws SQLException { - - } - - @Override - void setBigDecimal(String parameterName, BigDecimal x) throws SQLException { - - } - - @Override - void setString(String parameterName, String x) throws SQLException { - - } - - @Override - void setBytes(String parameterName, byte[] x) throws SQLException { - - } - - @Override - void setDate(String parameterName, Date x) throws SQLException { - - } - - @Override - void setTime(String parameterName, Time x) throws SQLException { - - } - - @Override - void setTimestamp(String parameterName, Timestamp x) throws SQLException { - - } - - @Override - void setAsciiStream(String parameterName, InputStream x, int length) throws SQLException { - - } - - @Override - void setBinaryStream(String parameterName, InputStream x, int length) throws SQLException { - - } - - @Override - void setObject(String parameterName, Object x, int targetSqlType, int scale) throws SQLException { - - } - - @Override - void setObject(String parameterName, Object x, int targetSqlType) throws SQLException { - - } - - @Override - void setObject(String parameterName, Object x) throws SQLException { - - } - - @Override - void setCharacterStream(String parameterName, Reader reader, int length) throws SQLException { - - } - - @Override - void setDate(String parameterName, Date x, Calendar cal) throws SQLException { - - } - - @Override - void setTime(String parameterName, Time x, Calendar cal) throws SQLException { - - } - - @Override - void setTimestamp(String parameterName, Timestamp x, Calendar cal) throws SQLException { - - } - - @Override - void setNull(String parameterName, int sqlType, String typeName) throws SQLException { - - } - - @Override - String getString(String parameterName) throws SQLException { - return null - } - - @Override - boolean getBoolean(String parameterName) throws SQLException { - return false - } - - @Override - byte getByte(String parameterName) throws SQLException { - return 0 - } - - @Override - short getShort(String parameterName) throws SQLException { - return 0 - } - - @Override - int getInt(String parameterName) throws SQLException { - return 0 - } - - @Override - long getLong(String parameterName) throws SQLException { - return 0 - } - - @Override - float getFloat(String parameterName) throws SQLException { - return 0 - } - - @Override - double getDouble(String parameterName) throws SQLException { - return 0 - } - - @Override - byte[] getBytes(String parameterName) throws SQLException { - return new byte[0] - } - - @Override - Date getDate(String parameterName) throws SQLException { - return null - } - - @Override - Time getTime(String parameterName) throws SQLException { - return null - } - - @Override - Timestamp getTimestamp(String parameterName) throws SQLException { - return null - } - - @Override - Object getObject(String parameterName) throws SQLException { - return null - } - - @Override - BigDecimal getBigDecimal(String parameterName) throws SQLException { - return null - } - - @Override - Object getObject(String parameterName, Map> map) throws SQLException { - return null - } - - @Override - Ref getRef(String parameterName) throws SQLException { - return null - } - - @Override - Blob getBlob(String parameterName) throws SQLException { - return null - } - - @Override - Clob getClob(String parameterName) throws SQLException { - return null - } - - @Override - Array getArray(String parameterName) throws SQLException { - return null - } - - @Override - Date getDate(String parameterName, Calendar cal) throws SQLException { - return null - } - - @Override - Time getTime(String parameterName, Calendar cal) throws SQLException { - return null - } - - @Override - Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLException { - return null - } - - @Override - URL getURL(String parameterName) throws SQLException { - return null - } - - @Override - RowId getRowId(int parameterIndex) throws SQLException { - return null - } - - @Override - RowId getRowId(String parameterName) throws SQLException { - return null - } - - @Override - void setRowId(String parameterName, RowId x) throws SQLException { - - } - - @Override - void setNString(String parameterName, String value) throws SQLException { - - } - - @Override - void setNCharacterStream(String parameterName, Reader value, long length) throws SQLException { - - } - - @Override - void setNClob(String parameterName, NClob value) throws SQLException { - - } - - @Override - void setClob(String parameterName, Reader reader, long length) throws SQLException { - - } - - @Override - void setBlob(String parameterName, InputStream inputStream, long length) throws SQLException { - - } - - @Override - void setNClob(String parameterName, Reader reader, long length) throws SQLException { - - } - - @Override - NClob getNClob(int parameterIndex) throws SQLException { - return null - } - - @Override - NClob getNClob(String parameterName) throws SQLException { - return null - } - - @Override - void setSQLXML(String parameterName, SQLXML xmlObject) throws SQLException { - - } - - @Override - SQLXML getSQLXML(int parameterIndex) throws SQLException { - return null - } - - @Override - SQLXML getSQLXML(String parameterName) throws SQLException { - return null - } - - @Override - String getNString(int parameterIndex) throws SQLException { - return null - } - - @Override - String getNString(String parameterName) throws SQLException { - return null - } - - @Override - Reader getNCharacterStream(int parameterIndex) throws SQLException { - return null - } - - @Override - Reader getNCharacterStream(String parameterName) throws SQLException { - return null - } - - @Override - Reader getCharacterStream(int parameterIndex) throws SQLException { - return null - } - - @Override - Reader getCharacterStream(String parameterName) throws SQLException { - return null - } - - @Override - void setBlob(String parameterName, Blob x) throws SQLException { - - } - - @Override - void setClob(String parameterName, Clob x) throws SQLException { - - } - - @Override - void setAsciiStream(String parameterName, InputStream x, long length) throws SQLException { - - } - - @Override - void setBinaryStream(String parameterName, InputStream x, long length) throws SQLException { - - } - - @Override - void setCharacterStream(String parameterName, Reader reader, long length) throws SQLException { - - } - - @Override - void setAsciiStream(String parameterName, InputStream x) throws SQLException { - - } - - @Override - void setBinaryStream(String parameterName, InputStream x) throws SQLException { - - } - - @Override - void setCharacterStream(String parameterName, Reader reader) throws SQLException { - - } - - @Override - void setNCharacterStream(String parameterName, Reader value) throws SQLException { - - } - - @Override - void setClob(String parameterName, Reader reader) throws SQLException { - - } - - @Override - void setBlob(String parameterName, InputStream inputStream) throws SQLException { - - } - - @Override - void setNClob(String parameterName, Reader reader) throws SQLException { - - } - - @Override - def T getObject(int parameterIndex, Class type) throws SQLException { - return null - } - - @Override - def T getObject(String parameterName, Class type) throws SQLException { - return null - } -} diff --git a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestConnection.groovy b/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestConnection.groovy deleted file mode 100644 index abee3ad6b9e9..000000000000 --- a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestConnection.groovy +++ /dev/null @@ -1,317 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.jdbc - -import java.sql.Array -import java.sql.Blob -import java.sql.CallableStatement -import java.sql.Clob -import java.sql.Connection -import java.sql.DatabaseMetaData -import java.sql.NClob -import java.sql.PreparedStatement -import java.sql.SQLClientInfoException -import java.sql.SQLException -import java.sql.SQLWarning -import java.sql.SQLXML -import java.sql.Savepoint -import java.sql.Statement -import java.sql.Struct -import java.util.concurrent.Executor - -/** - * A JDBC connection class that optionally throws an exception in the constructor, used to test - */ -class TestConnection implements Connection { - private String url - - TestConnection() { - this(false) - } - - TestConnection(boolean throwException) { - if (throwException) { - throw new IllegalStateException("connection exception") - } - } - - @Override - Statement createStatement() throws SQLException { - return new TestStatement(this) - } - - @Override - Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { - return new TestStatement(this) - } - - @Override - Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - return new TestStatement(this) - } - - @Override - PreparedStatement prepareStatement(String sql) throws SQLException { - return new TestPreparedStatement(this) - } - - @Override - PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { - return new TestPreparedStatement(this) - } - - @Override - PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - return new TestPreparedStatement(this) - } - - @Override - PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { - return new TestPreparedStatement(this) - } - - @Override - PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { - return new TestPreparedStatement(this) - } - - @Override - PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { - return new TestPreparedStatement(this) - } - - @Override - CallableStatement prepareCall(String sql) throws SQLException { - return new TestCallableStatement() - } - - @Override - CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { - return new TestCallableStatement() - } - - @Override - CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - return new TestCallableStatement() - } - - @Override - String nativeSQL(String sql) throws SQLException { - return null - } - - @Override - void setAutoCommit(boolean autoCommit) throws SQLException { - - } - - @Override - boolean getAutoCommit() throws SQLException { - return false - } - - @Override - void commit() throws SQLException { - - } - - @Override - void rollback() throws SQLException { - - } - - @Override - void close() throws SQLException { - - } - - @Override - boolean isClosed() throws SQLException { - return false - } - - @Override - DatabaseMetaData getMetaData() throws SQLException { - if (url) { - return new TestDatabaseMetaData(url) - } - return new TestDatabaseMetaData() - } - - @Override - void setReadOnly(boolean readOnly) throws SQLException { - - } - - @Override - boolean isReadOnly() throws SQLException { - return false - } - - @Override - void setCatalog(String catalog) throws SQLException { - - } - - @Override - String getCatalog() throws SQLException { - return null - } - - @Override - void setTransactionIsolation(int level) throws SQLException { - - } - - @Override - int getTransactionIsolation() throws SQLException { - return 0 - } - - @Override - SQLWarning getWarnings() throws SQLException { - return null - } - - @Override - void clearWarnings() throws SQLException { - - } - - @Override - Map> getTypeMap() throws SQLException { - return null - } - - @Override - void setTypeMap(Map> map) throws SQLException { - - } - - @Override - void setHoldability(int holdability) throws SQLException { - - } - - @Override - int getHoldability() throws SQLException { - return 0 - } - - @Override - Savepoint setSavepoint() throws SQLException { - return null - } - - @Override - Savepoint setSavepoint(String name) throws SQLException { - return null - } - - @Override - void rollback(Savepoint savepoint) throws SQLException { - - } - - @Override - void releaseSavepoint(Savepoint savepoint) throws SQLException { - - } - - @Override - Clob createClob() throws SQLException { - return null - } - - @Override - Blob createBlob() throws SQLException { - return null - } - - @Override - NClob createNClob() throws SQLException { - return null - } - - @Override - SQLXML createSQLXML() throws SQLException { - return null - } - - @Override - boolean isValid(int timeout) throws SQLException { - return false - } - - @Override - void setClientInfo(String name, String value) throws SQLClientInfoException { - - } - - @Override - void setClientInfo(Properties properties) throws SQLClientInfoException { - - } - - @Override - String getClientInfo(String name) throws SQLException { - throw new UnsupportedOperationException("Test 123") - } - - @Override - Properties getClientInfo() throws SQLException { - throw new Throwable("Test 123") - } - - @Override - Array createArrayOf(String typeName, Object[] elements) throws SQLException { - return null - } - - @Override - Struct createStruct(String typeName, Object[] attributes) throws SQLException { - return null - } - - @Override - void setSchema(String schema) throws SQLException { - - } - - @Override - String getSchema() throws SQLException { - return null - } - - @Override - void abort(Executor executor) throws SQLException { - - } - - @Override - void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { - - } - - @Override - int getNetworkTimeout() throws SQLException { - return 0 - } - - @Override - def T unwrap(Class iface) throws SQLException { - return null - } - - @Override - boolean isWrapperFor(Class iface) throws SQLException { - return false - } - - void setUrl(String url) { - this.url = url - } -} diff --git a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestDatabaseMetaData.groovy b/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestDatabaseMetaData.groovy deleted file mode 100644 index a65835f08a9c..000000000000 --- a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestDatabaseMetaData.groovy +++ /dev/null @@ -1,904 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.jdbc - -import java.sql.Connection -import java.sql.DatabaseMetaData -import java.sql.ResultSet -import java.sql.RowIdLifetime -import java.sql.SQLException - -class TestDatabaseMetaData implements DatabaseMetaData { - final String url - - TestDatabaseMetaData() { - this("jdbc:postgresql://127.0.0.1:5432/dbname") - } - - TestDatabaseMetaData(String url) { - this.url = url - } - - @Override - boolean allProceduresAreCallable() throws SQLException { - return false - } - - @Override - boolean allTablesAreSelectable() throws SQLException { - return false - } - - @Override - String getURL() throws SQLException { - return url - } - - @Override - String getUserName() throws SQLException { - return null - } - - @Override - boolean isReadOnly() throws SQLException { - return false - } - - @Override - boolean nullsAreSortedHigh() throws SQLException { - return false - } - - @Override - boolean nullsAreSortedLow() throws SQLException { - return false - } - - @Override - boolean nullsAreSortedAtStart() throws SQLException { - return false - } - - @Override - boolean nullsAreSortedAtEnd() throws SQLException { - return false - } - - @Override - String getDatabaseProductName() throws SQLException { - return null - } - - @Override - String getDatabaseProductVersion() throws SQLException { - return null - } - - @Override - String getDriverName() throws SQLException { - return null - } - - @Override - String getDriverVersion() throws SQLException { - return null - } - - @Override - int getDriverMajorVersion() { - return 0 - } - - @Override - int getDriverMinorVersion() { - return 0 - } - - @Override - boolean usesLocalFiles() throws SQLException { - return false - } - - @Override - boolean usesLocalFilePerTable() throws SQLException { - return false - } - - @Override - boolean supportsMixedCaseIdentifiers() throws SQLException { - return false - } - - @Override - boolean storesUpperCaseIdentifiers() throws SQLException { - return false - } - - @Override - boolean storesLowerCaseIdentifiers() throws SQLException { - return false - } - - @Override - boolean storesMixedCaseIdentifiers() throws SQLException { - return false - } - - @Override - boolean supportsMixedCaseQuotedIdentifiers() throws SQLException { - return false - } - - @Override - boolean storesUpperCaseQuotedIdentifiers() throws SQLException { - return false - } - - @Override - boolean storesLowerCaseQuotedIdentifiers() throws SQLException { - return false - } - - @Override - boolean storesMixedCaseQuotedIdentifiers() throws SQLException { - return false - } - - @Override - String getIdentifierQuoteString() throws SQLException { - return null - } - - @Override - String getSQLKeywords() throws SQLException { - return null - } - - @Override - String getNumericFunctions() throws SQLException { - return null - } - - @Override - String getStringFunctions() throws SQLException { - return null - } - - @Override - String getSystemFunctions() throws SQLException { - return null - } - - @Override - String getTimeDateFunctions() throws SQLException { - return null - } - - @Override - String getSearchStringEscape() throws SQLException { - return null - } - - @Override - String getExtraNameCharacters() throws SQLException { - return null - } - - @Override - boolean supportsAlterTableWithAddColumn() throws SQLException { - return false - } - - @Override - boolean supportsAlterTableWithDropColumn() throws SQLException { - return false - } - - @Override - boolean supportsColumnAliasing() throws SQLException { - return false - } - - @Override - boolean nullPlusNonNullIsNull() throws SQLException { - return false - } - - @Override - boolean supportsConvert() throws SQLException { - return false - } - - @Override - boolean supportsConvert(int fromType, int toType) throws SQLException { - return false - } - - @Override - boolean supportsTableCorrelationNames() throws SQLException { - return false - } - - @Override - boolean supportsDifferentTableCorrelationNames() throws SQLException { - return false - } - - @Override - boolean supportsExpressionsInOrderBy() throws SQLException { - return false - } - - @Override - boolean supportsOrderByUnrelated() throws SQLException { - return false - } - - @Override - boolean supportsGroupBy() throws SQLException { - return false - } - - @Override - boolean supportsGroupByUnrelated() throws SQLException { - return false - } - - @Override - boolean supportsGroupByBeyondSelect() throws SQLException { - return false - } - - @Override - boolean supportsLikeEscapeClause() throws SQLException { - return false - } - - @Override - boolean supportsMultipleResultSets() throws SQLException { - return false - } - - @Override - boolean supportsMultipleTransactions() throws SQLException { - return false - } - - @Override - boolean supportsNonNullableColumns() throws SQLException { - return false - } - - @Override - boolean supportsMinimumSQLGrammar() throws SQLException { - return false - } - - @Override - boolean supportsCoreSQLGrammar() throws SQLException { - return false - } - - @Override - boolean supportsExtendedSQLGrammar() throws SQLException { - return false - } - - @Override - boolean supportsANSI92EntryLevelSQL() throws SQLException { - return false - } - - @Override - boolean supportsANSI92IntermediateSQL() throws SQLException { - return false - } - - @Override - boolean supportsANSI92FullSQL() throws SQLException { - return false - } - - @Override - boolean supportsIntegrityEnhancementFacility() throws SQLException { - return false - } - - @Override - boolean supportsOuterJoins() throws SQLException { - return false - } - - @Override - boolean supportsFullOuterJoins() throws SQLException { - return false - } - - @Override - boolean supportsLimitedOuterJoins() throws SQLException { - return false - } - - @Override - String getSchemaTerm() throws SQLException { - return null - } - - @Override - String getProcedureTerm() throws SQLException { - return null - } - - @Override - String getCatalogTerm() throws SQLException { - return null - } - - @Override - boolean isCatalogAtStart() throws SQLException { - return false - } - - @Override - String getCatalogSeparator() throws SQLException { - return null - } - - @Override - boolean supportsSchemasInDataManipulation() throws SQLException { - return false - } - - @Override - boolean supportsSchemasInProcedureCalls() throws SQLException { - return false - } - - @Override - boolean supportsSchemasInTableDefinitions() throws SQLException { - return false - } - - @Override - boolean supportsSchemasInIndexDefinitions() throws SQLException { - return false - } - - @Override - boolean supportsSchemasInPrivilegeDefinitions() throws SQLException { - return false - } - - @Override - boolean supportsCatalogsInDataManipulation() throws SQLException { - return false - } - - @Override - boolean supportsCatalogsInProcedureCalls() throws SQLException { - return false - } - - @Override - boolean supportsCatalogsInTableDefinitions() throws SQLException { - return false - } - - @Override - boolean supportsCatalogsInIndexDefinitions() throws SQLException { - return false - } - - @Override - boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException { - return false - } - - @Override - boolean supportsPositionedDelete() throws SQLException { - return false - } - - @Override - boolean supportsPositionedUpdate() throws SQLException { - return false - } - - @Override - boolean supportsSelectForUpdate() throws SQLException { - return false - } - - @Override - boolean supportsStoredProcedures() throws SQLException { - return false - } - - @Override - boolean supportsSubqueriesInComparisons() throws SQLException { - return false - } - - @Override - boolean supportsSubqueriesInExists() throws SQLException { - return false - } - - @Override - boolean supportsSubqueriesInIns() throws SQLException { - return false - } - - @Override - boolean supportsSubqueriesInQuantifieds() throws SQLException { - return false - } - - @Override - boolean supportsCorrelatedSubqueries() throws SQLException { - return false - } - - @Override - boolean supportsUnion() throws SQLException { - return false - } - - @Override - boolean supportsUnionAll() throws SQLException { - return false - } - - @Override - boolean supportsOpenCursorsAcrossCommit() throws SQLException { - return false - } - - @Override - boolean supportsOpenCursorsAcrossRollback() throws SQLException { - return false - } - - @Override - boolean supportsOpenStatementsAcrossCommit() throws SQLException { - return false - } - - @Override - boolean supportsOpenStatementsAcrossRollback() throws SQLException { - return false - } - - @Override - int getMaxBinaryLiteralLength() throws SQLException { - return 0 - } - - @Override - int getMaxCharLiteralLength() throws SQLException { - return 0 - } - - @Override - int getMaxColumnNameLength() throws SQLException { - return 0 - } - - @Override - int getMaxColumnsInGroupBy() throws SQLException { - return 0 - } - - @Override - int getMaxColumnsInIndex() throws SQLException { - return 0 - } - - @Override - int getMaxColumnsInOrderBy() throws SQLException { - return 0 - } - - @Override - int getMaxColumnsInSelect() throws SQLException { - return 0 - } - - @Override - int getMaxColumnsInTable() throws SQLException { - return 0 - } - - @Override - int getMaxConnections() throws SQLException { - return 0 - } - - @Override - int getMaxCursorNameLength() throws SQLException { - return 0 - } - - @Override - int getMaxIndexLength() throws SQLException { - return 0 - } - - @Override - int getMaxSchemaNameLength() throws SQLException { - return 0 - } - - @Override - int getMaxProcedureNameLength() throws SQLException { - return 0 - } - - @Override - int getMaxCatalogNameLength() throws SQLException { - return 0 - } - - @Override - int getMaxRowSize() throws SQLException { - return 0 - } - - @Override - boolean doesMaxRowSizeIncludeBlobs() throws SQLException { - return false - } - - @Override - int getMaxStatementLength() throws SQLException { - return 0 - } - - @Override - int getMaxStatements() throws SQLException { - return 0 - } - - @Override - int getMaxTableNameLength() throws SQLException { - return 0 - } - - @Override - int getMaxTablesInSelect() throws SQLException { - return 0 - } - - @Override - int getMaxUserNameLength() throws SQLException { - return 0 - } - - @Override - int getDefaultTransactionIsolation() throws SQLException { - return 0 - } - - @Override - boolean supportsTransactions() throws SQLException { - return false - } - - @Override - boolean supportsTransactionIsolationLevel(int level) throws SQLException { - return false - } - - @Override - boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException { - return false - } - - @Override - boolean supportsDataManipulationTransactionsOnly() throws SQLException { - return false - } - - @Override - boolean dataDefinitionCausesTransactionCommit() throws SQLException { - return false - } - - @Override - boolean dataDefinitionIgnoredInTransactions() throws SQLException { - return false - } - - @Override - ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException { - return null - } - - @Override - ResultSet getSchemas() throws SQLException { - return null - } - - @Override - ResultSet getCatalogs() throws SQLException { - return null - } - - @Override - ResultSet getTableTypes() throws SQLException { - return null - } - - @Override - ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) throws SQLException { - return null - } - - @Override - ResultSet getVersionColumns(String catalog, String schema, String table) throws SQLException { - return null - } - - @Override - ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { - return null - } - - @Override - ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { - return null - } - - @Override - ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException { - return null - } - - @Override - ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException { - return null - } - - @Override - ResultSet getTypeInfo() throws SQLException { - return null - } - - @Override - ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException { - return null - } - - @Override - boolean supportsResultSetType(int type) throws SQLException { - return false - } - - @Override - boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException { - return false - } - - @Override - boolean ownUpdatesAreVisible(int type) throws SQLException { - return false - } - - @Override - boolean ownDeletesAreVisible(int type) throws SQLException { - return false - } - - @Override - boolean ownInsertsAreVisible(int type) throws SQLException { - return false - } - - @Override - boolean othersUpdatesAreVisible(int type) throws SQLException { - return false - } - - @Override - boolean othersDeletesAreVisible(int type) throws SQLException { - return false - } - - @Override - boolean othersInsertsAreVisible(int type) throws SQLException { - return false - } - - @Override - boolean updatesAreDetected(int type) throws SQLException { - return false - } - - @Override - boolean deletesAreDetected(int type) throws SQLException { - return false - } - - @Override - boolean insertsAreDetected(int type) throws SQLException { - return false - } - - @Override - boolean supportsBatchUpdates() throws SQLException { - return false - } - - @Override - ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException { - return null - } - - @Override - Connection getConnection() throws SQLException { - return null - } - - @Override - boolean supportsSavepoints() throws SQLException { - return false - } - - @Override - boolean supportsNamedParameters() throws SQLException { - return false - } - - @Override - boolean supportsMultipleOpenResults() throws SQLException { - return false - } - - @Override - boolean supportsGetGeneratedKeys() throws SQLException { - return false - } - - @Override - ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) throws SQLException { - return null - } - - @Override - boolean supportsResultSetHoldability(int holdability) throws SQLException { - return false - } - - @Override - int getResultSetHoldability() throws SQLException { - return 0 - } - - @Override - int getDatabaseMajorVersion() throws SQLException { - return 0 - } - - @Override - int getDatabaseMinorVersion() throws SQLException { - return 0 - } - - @Override - int getJDBCMajorVersion() throws SQLException { - return 0 - } - - @Override - int getJDBCMinorVersion() throws SQLException { - return 0 - } - - @Override - int getSQLStateType() throws SQLException { - return 0 - } - - @Override - boolean locatorsUpdateCopy() throws SQLException { - return false - } - - @Override - boolean supportsStatementPooling() throws SQLException { - return false - } - - @Override - RowIdLifetime getRowIdLifetime() throws SQLException { - return null - } - - @Override - ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException { - return null - } - - @Override - boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException { - return false - } - - @Override - boolean autoCommitFailureClosesAllResultSets() throws SQLException { - return false - } - - @Override - ResultSet getClientInfoProperties() throws SQLException { - return null - } - - @Override - ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) throws SQLException { - return null - } - - @Override - ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { - return null - } - - @Override - boolean generatedKeyAlwaysReturned() throws SQLException { - return false - } - - @Override - def T unwrap(Class iface) throws SQLException { - return null - } - - @Override - boolean isWrapperFor(Class iface) throws SQLException { - return false - } -} diff --git a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestDriver.groovy b/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestDriver.groovy deleted file mode 100644 index 033cf445a60a..000000000000 --- a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestDriver.groovy +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.jdbc - - -import java.sql.Connection -import java.sql.Driver -import java.sql.DriverPropertyInfo -import java.sql.SQLException -import java.sql.SQLFeatureNotSupportedException -import java.util.logging.Logger - -class TestDriver implements Driver { - @Override - Connection connect(String url, Properties info) throws SQLException { - return new TestConnection() - } - - @Override - boolean acceptsURL(String url) throws SQLException { - return url?.startsWith("jdbc:test:") - } - - @Override - DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { - return [new DriverPropertyInfo("test", "test")] - } - - @Override - int getMajorVersion() { - return 0 - } - - @Override - int getMinorVersion() { - return 0 - } - - @Override - boolean jdbcCompliant() { - return false - } - - @Override - Logger getParentLogger() throws SQLFeatureNotSupportedException { - return null - } -} diff --git a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestPreparedStatement.groovy b/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestPreparedStatement.groovy deleted file mode 100644 index c8f21d061beb..000000000000 --- a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestPreparedStatement.groovy +++ /dev/null @@ -1,309 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.jdbc - -import java.sql.Array -import java.sql.Blob -import java.sql.Clob -import java.sql.Connection -import java.sql.Date -import java.sql.NClob -import java.sql.ParameterMetaData -import java.sql.PreparedStatement -import java.sql.Ref -import java.sql.ResultSet -import java.sql.ResultSetMetaData -import java.sql.RowId -import java.sql.SQLException -import java.sql.SQLXML -import java.sql.Time -import java.sql.Timestamp - -class TestPreparedStatement extends TestStatement implements PreparedStatement { - - TestPreparedStatement() { - super() - } - - TestPreparedStatement(Connection connection) { - super(connection) - } - - @Override - boolean execute() throws SQLException { - return true - } - - @Override - ResultSet executeQuery() throws SQLException { - return null - } - - @Override - int executeUpdate() throws SQLException { - return 0 - } - - @Override - void setNull(int parameterIndex, int sqlType) throws SQLException { - - } - - @Override - void setBoolean(int parameterIndex, boolean x) throws SQLException { - - } - - @Override - void setByte(int parameterIndex, byte x) throws SQLException { - - } - - @Override - void setShort(int parameterIndex, short x) throws SQLException { - - } - - @Override - void setInt(int parameterIndex, int x) throws SQLException { - - } - - @Override - void setLong(int parameterIndex, long x) throws SQLException { - - } - - @Override - void setFloat(int parameterIndex, float x) throws SQLException { - - } - - @Override - void setDouble(int parameterIndex, double x) throws SQLException { - - } - - @Override - void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { - - } - - @Override - void setString(int parameterIndex, String x) throws SQLException { - - } - - @Override - void setBytes(int parameterIndex, byte[] x) throws SQLException { - - } - - @Override - void setDate(int parameterIndex, Date x) throws SQLException { - - } - - @Override - void setTime(int parameterIndex, Time x) throws SQLException { - - } - - @Override - void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { - - } - - @Override - void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { - - } - - @Override - void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { - - } - - @Override - void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { - - } - - @Override - void clearParameters() throws SQLException { - - } - - @Override - void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { - - } - - @Override - void setObject(int parameterIndex, Object x) throws SQLException { - - } - - @Override - void addBatch() throws SQLException { - - } - - @Override - void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { - - } - - @Override - void setRef(int parameterIndex, Ref x) throws SQLException { - - } - - @Override - void setBlob(int parameterIndex, Blob x) throws SQLException { - - } - - @Override - void setClob(int parameterIndex, Clob x) throws SQLException { - - } - - @Override - void setArray(int parameterIndex, Array x) throws SQLException { - - } - - @Override - ResultSetMetaData getMetaData() throws SQLException { - return null - } - - @Override - void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { - - } - - @Override - void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { - - } - - @Override - void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { - - } - - @Override - void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { - - } - - @Override - void setURL(int parameterIndex, URL x) throws SQLException { - - } - - @Override - ParameterMetaData getParameterMetaData() throws SQLException { - return null - } - - @Override - void setRowId(int parameterIndex, RowId x) throws SQLException { - - } - - @Override - void setNString(int parameterIndex, String value) throws SQLException { - - } - - @Override - void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { - - } - - @Override - void setNClob(int parameterIndex, NClob value) throws SQLException { - - } - - @Override - void setClob(int parameterIndex, Reader reader, long length) throws SQLException { - - } - - @Override - void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { - - } - - @Override - void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { - - } - - @Override - void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { - - } - - @Override - void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { - - } - - @Override - void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { - - } - - @Override - void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { - - } - - @Override - void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException { - - } - - @Override - void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { - - } - - @Override - void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { - - } - - @Override - void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { - - } - - @Override - void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { - - } - - @Override - void setClob(int parameterIndex, Reader reader) throws SQLException { - - } - - @Override - void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { - - } - - @Override - void setNClob(int parameterIndex, Reader reader) throws SQLException { - - } -} diff --git a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestStatement.groovy b/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestStatement.groovy deleted file mode 100644 index 4c833c93358c..000000000000 --- a/instrumentation/jdbc/testing/src/main/groovy/io/opentelemetry/instrumentation/jdbc/TestStatement.groovy +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.jdbc - -import java.sql.Connection -import java.sql.ResultSet -import java.sql.SQLException -import java.sql.SQLWarning -import java.sql.Statement - -class TestStatement implements Statement { - final Connection connection - - TestStatement() { - this.connection = null - } - - TestStatement(Connection connection) { - this.connection = connection - } - - @Override - ResultSet executeQuery(String sql) throws SQLException { - return null - } - - @Override - int executeUpdate(String sql) throws SQLException { - return 0 - } - - @Override - void close() throws SQLException { - - } - - @Override - int getMaxFieldSize() throws SQLException { - return 0 - } - - @Override - void setMaxFieldSize(int max) throws SQLException { - - } - - @Override - int getMaxRows() throws SQLException { - return 0 - } - - @Override - void setMaxRows(int max) throws SQLException { - - } - - @Override - void setEscapeProcessing(boolean enable) throws SQLException { - - } - - @Override - int getQueryTimeout() throws SQLException { - return 0 - } - - @Override - void setQueryTimeout(int seconds) throws SQLException { - - } - - @Override - void cancel() throws SQLException { - - } - - @Override - SQLWarning getWarnings() throws SQLException { - return null - } - - @Override - void clearWarnings() throws SQLException { - - } - - @Override - void setCursorName(String name) throws SQLException { - - } - - @Override - boolean execute(String sql) throws SQLException { - return true - } - - @Override - ResultSet getResultSet() throws SQLException { - return null - } - - @Override - int getUpdateCount() throws SQLException { - return 0 - } - - @Override - boolean getMoreResults() throws SQLException { - return false - } - - @Override - void setFetchDirection(int direction) throws SQLException { - - } - - @Override - int getFetchDirection() throws SQLException { - return 0 - } - - @Override - void setFetchSize(int rows) throws SQLException { - - } - - @Override - int getFetchSize() throws SQLException { - return 0 - } - - @Override - int getResultSetConcurrency() throws SQLException { - return 0 - } - - @Override - int getResultSetType() throws SQLException { - return 0 - } - - @Override - void addBatch(String sql) throws SQLException { - - } - - @Override - void clearBatch() throws SQLException { - - } - - @Override - int[] executeBatch() throws SQLException { - return new int[0] - } - - @Override - Connection getConnection() throws SQLException { - return connection - } - - @Override - boolean getMoreResults(int current) throws SQLException { - return false - } - - @Override - ResultSet getGeneratedKeys() throws SQLException { - return null - } - - @Override - int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - return 0 - } - - @Override - int executeUpdate(String sql, int[] columnIndexes) throws SQLException { - return 0 - } - - @Override - int executeUpdate(String sql, String[] columnNames) throws SQLException { - return 0 - } - - @Override - boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - return true - } - - @Override - boolean execute(String sql, int[] columnIndexes) throws SQLException { - return true - } - - @Override - boolean execute(String sql, String[] columnNames) throws SQLException { - return true - } - - @Override - int getResultSetHoldability() throws SQLException { - return 0 - } - - @Override - boolean isClosed() throws SQLException { - return false - } - - @Override - void setPoolable(boolean poolable) throws SQLException { - - } - - @Override - boolean isPoolable() throws SQLException { - return false - } - - @Override - void closeOnCompletion() throws SQLException { - - } - - @Override - boolean isCloseOnCompletion() throws SQLException { - return false - } - - @Override - def T unwrap(Class iface) throws SQLException { - return null - } - - @Override - boolean isWrapperFor(Class iface) throws SQLException { - return false - } -} diff --git a/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/AnotherTestDriver.java b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/AnotherTestDriver.java new file mode 100644 index 000000000000..a25b1a3d72c0 --- /dev/null +++ b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/AnotherTestDriver.java @@ -0,0 +1,51 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jdbc; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; +import java.util.logging.Logger; + +class AnotherTestDriver implements Driver { + @Override + public Connection connect(String url, Properties info) throws SQLException { + return null; + } + + @Override + public boolean acceptsURL(String url) throws SQLException { + return false; + } + + @Override + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + return new DriverPropertyInfo[0]; + } + + @Override + public int getMajorVersion() { + return 0; + } + + @Override + public int getMinorVersion() { + return 0; + } + + @Override + public boolean jdbcCompliant() { + return false; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } +} diff --git a/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestCallableStatement.java b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestCallableStatement.java new file mode 100644 index 000000000000..1dbfeb93d751 --- /dev/null +++ b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestCallableStatement.java @@ -0,0 +1,503 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jdbc; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Date; +import java.sql.NClob; +import java.sql.Ref; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLXML; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Map; + +class TestCallableStatement extends TestPreparedStatement implements CallableStatement { + @Override + public Array getArray(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Array getArray(String parameterName) throws SQLException { + return null; + } + + @Override + @Deprecated + public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException { + return null; + } + + @Override + public BigDecimal getBigDecimal(int parameterIndex) throws SQLException { + return null; + } + + @Override + public BigDecimal getBigDecimal(String parameterName) throws SQLException { + return null; + } + + @Override + public Blob getBlob(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Blob getBlob(String parameterName) throws SQLException { + return null; + } + + @Override + public boolean getBoolean(int parameterIndex) throws SQLException { + return false; + } + + @Override + public boolean getBoolean(String parameterName) throws SQLException { + return false; + } + + @Override + public byte getByte(int parameterIndex) throws SQLException { + return 0; + } + + @Override + public byte getByte(String parameterName) throws SQLException { + return 0; + } + + @Override + public byte[] getBytes(int parameterIndex) throws SQLException { + return new byte[0]; + } + + @Override + public byte[] getBytes(String parameterName) throws SQLException { + return new byte[0]; + } + + @Override + public Reader getCharacterStream(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Reader getCharacterStream(String parameterName) throws SQLException { + return null; + } + + @Override + public Clob getClob(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Clob getClob(String parameterName) throws SQLException { + return null; + } + + @Override + public Date getDate(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Date getDate(int parameterIndex, Calendar cal) throws SQLException { + return null; + } + + @Override + public Date getDate(String parameterName) throws SQLException { + return null; + } + + @Override + public Date getDate(String parameterName, Calendar cal) throws SQLException { + return null; + } + + @Override + public double getDouble(int parameterIndex) throws SQLException { + return 0; + } + + @Override + public double getDouble(String parameterName) throws SQLException { + return 0; + } + + @Override + public float getFloat(int parameterIndex) throws SQLException { + return 0; + } + + @Override + public float getFloat(String parameterName) throws SQLException { + return 0; + } + + @Override + public int getInt(int parameterIndex) throws SQLException { + return 0; + } + + @Override + public int getInt(String parameterName) throws SQLException { + return 0; + } + + @Override + public long getLong(int parameterIndex) throws SQLException { + return 0; + } + + @Override + public long getLong(String parameterName) throws SQLException { + return 0; + } + + @Override + public Reader getNCharacterStream(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Reader getNCharacterStream(String parameterName) throws SQLException { + return null; + } + + @Override + public NClob getNClob(int parameterIndex) throws SQLException { + return null; + } + + @Override + public NClob getNClob(String parameterName) throws SQLException { + return null; + } + + @Override + public String getNString(int parameterIndex) throws SQLException { + return null; + } + + @Override + public String getNString(String parameterName) throws SQLException { + return null; + } + + @Override + public Object getObject(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Object getObject(int parameterIndex, Map> map) throws SQLException { + return null; + } + + @Override + public Object getObject(String parameterName) throws SQLException { + return null; + } + + @Override + public Object getObject(String parameterName, Map> map) throws SQLException { + return null; + } + + @Override + public T getObject(int parameterIndex, Class type) throws SQLException { + return null; + } + + @Override + public T getObject(String parameterName, Class type) throws SQLException { + return null; + } + + @Override + public Ref getRef(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Ref getRef(String parameterName) throws SQLException { + return null; + } + + @Override + public RowId getRowId(int parameterIndex) throws SQLException { + return null; + } + + @Override + public RowId getRowId(String parameterName) throws SQLException { + return null; + } + + @Override + public SQLXML getSQLXML(int parameterIndex) throws SQLException { + return null; + } + + @Override + public SQLXML getSQLXML(String parameterName) throws SQLException { + return null; + } + + @Override + public short getShort(int parameterIndex) throws SQLException { + return 0; + } + + @Override + public short getShort(String parameterName) throws SQLException { + return 0; + } + + @Override + public String getString(int parameterIndex) throws SQLException { + return null; + } + + @Override + public String getString(String parameterName) throws SQLException { + return null; + } + + @Override + public Time getTime(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Time getTime(int parameterIndex, Calendar cal) throws SQLException { + return null; + } + + @Override + public Time getTime(String parameterName) throws SQLException { + return null; + } + + @Override + public Time getTime(String parameterName, Calendar cal) throws SQLException { + return null; + } + + @Override + public Timestamp getTimestamp(int parameterIndex) throws SQLException { + return null; + } + + @Override + public Timestamp getTimestamp(int parameterIndex, Calendar cal) throws SQLException { + return null; + } + + @Override + public Timestamp getTimestamp(String parameterName) throws SQLException { + return null; + } + + @Override + public Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLException { + return null; + } + + @Override + public URL getURL(int parameterIndex) throws SQLException { + return null; + } + + @Override + public URL getURL(String parameterName) throws SQLException { + return null; + } + + @Override + public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException {} + + @Override + public void registerOutParameter(int parameterIndex, int sqlType, int scale) + throws SQLException {} + + @Override + public void registerOutParameter(int parameterIndex, int sqlType, String typeName) + throws SQLException {} + + @Override + public void registerOutParameter(String parameterName, int sqlType) throws SQLException {} + + @Override + public void registerOutParameter(String parameterName, int sqlType, int scale) + throws SQLException {} + + @Override + public void registerOutParameter(String parameterName, int sqlType, String typeName) + throws SQLException {} + + @Override + public void setAsciiStream(String parameterName, InputStream x, int length) throws SQLException {} + + @Override + public void setAsciiStream(String parameterName, InputStream x, long length) + throws SQLException {} + + @Override + public void setAsciiStream(String parameterName, InputStream x) throws SQLException {} + + @Override + public void setBigDecimal(String parameterName, BigDecimal x) throws SQLException {} + + @Override + public void setBinaryStream(String parameterName, InputStream x, int length) + throws SQLException {} + + @Override + public void setBinaryStream(String parameterName, InputStream x, long length) + throws SQLException {} + + @Override + public void setBinaryStream(String parameterName, InputStream x) throws SQLException {} + + @Override + public void setBlob(String parameterName, InputStream inputStream, long length) + throws SQLException {} + + @Override + public void setBlob(String parameterName, Blob x) throws SQLException {} + + @Override + public void setBlob(String parameterName, InputStream inputStream) throws SQLException {} + + @Override + public void setBoolean(String parameterName, boolean x) throws SQLException {} + + @Override + public void setByte(String parameterName, byte x) throws SQLException {} + + @Override + public void setBytes(String parameterName, byte[] x) throws SQLException {} + + @Override + public void setCharacterStream(String parameterName, Reader reader, int length) + throws SQLException {} + + @Override + public void setCharacterStream(String parameterName, Reader reader, long length) + throws SQLException {} + + @Override + public void setCharacterStream(String parameterName, Reader reader) throws SQLException {} + + @Override + public void setClob(String parameterName, Reader reader, long length) throws SQLException {} + + @Override + public void setClob(String parameterName, Clob x) throws SQLException {} + + @Override + public void setClob(String parameterName, Reader reader) throws SQLException {} + + @Override + public void setDate(String parameterName, Date x) throws SQLException {} + + @Override + public void setDate(String parameterName, Date x, Calendar cal) throws SQLException {} + + @Override + public void setDouble(String parameterName, double x) throws SQLException {} + + @Override + public void setFloat(String parameterName, float x) throws SQLException {} + + @Override + public void setInt(String parameterName, int x) throws SQLException {} + + @Override + public void setLong(String parameterName, long x) throws SQLException {} + + @Override + public void setNCharacterStream(String parameterName, Reader value, long length) + throws SQLException {} + + @Override + public void setNCharacterStream(String parameterName, Reader value) throws SQLException {} + + @Override + public void setNClob(String parameterName, NClob value) throws SQLException {} + + @Override + public void setNClob(String parameterName, Reader reader, long length) throws SQLException {} + + @Override + public void setNClob(String parameterName, Reader reader) throws SQLException {} + + @Override + public void setNString(String parameterName, String value) throws SQLException {} + + @Override + public void setNull(String parameterName, int sqlType) throws SQLException {} + + @Override + public void setNull(String parameterName, int sqlType, String typeName) throws SQLException {} + + @Override + public void setObject(String parameterName, Object x, int targetSqlType, int scale) + throws SQLException {} + + @Override + public void setObject(String parameterName, Object x, int targetSqlType) throws SQLException {} + + @Override + public void setObject(String parameterName, Object x) throws SQLException {} + + @Override + public void setRowId(String parameterName, RowId x) throws SQLException {} + + @Override + public void setSQLXML(String parameterName, SQLXML xmlObject) throws SQLException {} + + @Override + public void setShort(String parameterName, short x) throws SQLException {} + + @Override + public void setString(String parameterName, String x) throws SQLException {} + + @Override + public void setTime(String parameterName, Time x) throws SQLException {} + + @Override + public void setTime(String parameterName, Time x, Calendar cal) throws SQLException {} + + @Override + public void setTimestamp(String parameterName, Timestamp x) throws SQLException {} + + @Override + public void setTimestamp(String parameterName, Timestamp x, Calendar cal) throws SQLException {} + + @Override + public void setURL(String parameterName, URL val) throws SQLException {} + + @Override + public boolean wasNull() throws SQLException { + return false; + } +} diff --git a/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestConnection.java b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestConnection.java new file mode 100644 index 000000000000..6230bcbecdc0 --- /dev/null +++ b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestConnection.java @@ -0,0 +1,292 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jdbc; + +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executor; + +/** A JDBC connection class that optionally throws an exception in the constructor, used to test */ +public class TestConnection implements Connection { + private String url; + + public TestConnection() { + this(false); + } + + public TestConnection(boolean throwException) { + if (throwException) { + throw new IllegalStateException("connection exception"); + } + } + + @Override + public void abort(Executor executor) throws SQLException {} + + @Override + public void clearWarnings() throws SQLException {} + + @Override + public void close() throws SQLException {} + + @Override + public void commit() throws SQLException {} + + @Override + public Array createArrayOf(String typeName, Object[] elements) throws SQLException { + return null; + } + + @Override + public Blob createBlob() throws SQLException { + return null; + } + + @Override + public Clob createClob() throws SQLException { + return null; + } + + @Override + public NClob createNClob() throws SQLException { + return null; + } + + @Override + public SQLXML createSQLXML() throws SQLException { + return null; + } + + @Override + public Statement createStatement() throws SQLException { + return new TestStatement(this); + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency) + throws SQLException { + return new TestStatement(this); + } + + @Override + public Statement createStatement( + int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + return new TestStatement(this); + } + + @Override + public Struct createStruct(String typeName, Object[] attributes) throws SQLException { + return null; + } + + @Override + public boolean getAutoCommit() throws SQLException { + return false; + } + + @Override + public String getCatalog() throws SQLException { + return null; + } + + @Override + public String getClientInfo(String name) throws SQLException { + throw new UnsupportedOperationException("Test 123"); + } + + @Override + public Properties getClientInfo() throws SQLException { + throw new UnsupportedOperationException("Test 123"); + } + + @Override + public int getHoldability() throws SQLException { + return 0; + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + if (url != null) { + return new TestDatabaseMetaData(url); + } + return new TestDatabaseMetaData(); + } + + @Override + public int getNetworkTimeout() throws SQLException { + return 0; + } + + @Override + public String getSchema() throws SQLException { + return null; + } + + @Override + public int getTransactionIsolation() throws SQLException { + return 0; + } + + @Override + public Map> getTypeMap() throws SQLException { + return new HashMap<>(); + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public boolean isReadOnly() throws SQLException { + return false; + } + + @Override + public boolean isValid(int timeout) throws SQLException { + return false; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + @Override + public String nativeSQL(String sql) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql) throws SQLException { + return new TestCallableStatement(); + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { + return new TestCallableStatement(); + } + + @Override + public CallableStatement prepareCall( + String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + return new TestCallableStatement(); + } + + @Override + public PreparedStatement prepareStatement(String sql) throws SQLException { + return new TestPreparedStatement(this); + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { + return new TestPreparedStatement(this); + } + + @Override + public PreparedStatement prepareStatement( + String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + return new TestPreparedStatement(this); + } + + @Override + public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { + return new TestPreparedStatement(this); + } + + @Override + public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { + return new TestPreparedStatement(this); + } + + @Override + public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { + return new TestPreparedStatement(this); + } + + @Override + public void releaseSavepoint(Savepoint savepoint) throws SQLException {} + + @Override + public void rollback(Savepoint savepoint) throws SQLException {} + + @Override + public void rollback() throws SQLException {} + + @Override + public void setAutoCommit(boolean autoCommit) throws SQLException {} + + @Override + public void setCatalog(String catalog) throws SQLException {} + + @Override + public void setClientInfo(Properties properties) throws SQLClientInfoException {} + + @Override + public void setClientInfo(String name, String value) throws SQLClientInfoException {} + + @Override + public void setHoldability(int holdability) throws SQLException {} + + @Override + public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {} + + @Override + public void setReadOnly(boolean readOnly) throws SQLException {} + + @Override + public Savepoint setSavepoint() throws SQLException { + return null; + } + + @Override + public Savepoint setSavepoint(String name) throws SQLException { + return null; + } + + @Override + public void setSchema(String schema) throws SQLException {} + + @Override + public void setTransactionIsolation(int level) throws SQLException {} + + @Override + public void setTypeMap(Map> map) throws SQLException {} + + public void setUrl(String url) { + this.url = url; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } +} diff --git a/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestDatabaseMetaData.java b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestDatabaseMetaData.java new file mode 100644 index 000000000000..0494a212a04e --- /dev/null +++ b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestDatabaseMetaData.java @@ -0,0 +1,938 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jdbc; + +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.RowIdLifetime; +import java.sql.SQLException; + +class TestDatabaseMetaData implements DatabaseMetaData { + final String url; + + TestDatabaseMetaData() { + this("jdbc:postgresql://127.0.0.1:5432/dbname"); + } + + TestDatabaseMetaData(String url) { + this.url = url; + } + + @Override + public boolean allProceduresAreCallable() throws SQLException { + return false; + } + + @Override + public boolean allTablesAreSelectable() throws SQLException { + return false; + } + + @Override + public boolean autoCommitFailureClosesAllResultSets() throws SQLException { + return false; + } + + @Override + public boolean dataDefinitionCausesTransactionCommit() throws SQLException { + return false; + } + + @Override + public boolean dataDefinitionIgnoredInTransactions() throws SQLException { + return false; + } + + @Override + public boolean deletesAreDetected(int type) throws SQLException { + return false; + } + + @Override + public boolean doesMaxRowSizeIncludeBlobs() throws SQLException { + return false; + } + + @Override + public boolean generatedKeyAlwaysReturned() throws SQLException { + return false; + } + + @Override + public ResultSet getAttributes( + String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getBestRowIdentifier( + String catalog, String schema, String table, int scope, boolean nullable) + throws SQLException { + return null; + } + + @Override + public String getCatalogSeparator() throws SQLException { + return null; + } + + @Override + public String getCatalogTerm() throws SQLException { + return null; + } + + @Override + public ResultSet getCatalogs() throws SQLException { + return null; + } + + @Override + public ResultSet getClientInfoProperties() throws SQLException { + return null; + } + + @Override + public ResultSet getColumnPrivileges( + String catalog, String schema, String table, String columnNamePattern) throws SQLException { + return null; + } + + @Override + public ResultSet getColumns( + String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) + throws SQLException { + return null; + } + + @Override + public Connection getConnection() throws SQLException { + return null; + } + + @Override + public ResultSet getCrossReference( + String parentCatalog, + String parentSchema, + String parentTable, + String foreignCatalog, + String foreignSchema, + String foreignTable) + throws SQLException { + return null; + } + + @Override + public int getDatabaseMajorVersion() throws SQLException { + return 0; + } + + @Override + public int getDatabaseMinorVersion() throws SQLException { + return 0; + } + + @Override + public String getDatabaseProductName() throws SQLException { + return null; + } + + @Override + public String getDatabaseProductVersion() throws SQLException { + return null; + } + + @Override + public int getDefaultTransactionIsolation() throws SQLException { + return 0; + } + + @Override + public int getDriverMajorVersion() { + return 0; + } + + @Override + public int getDriverMinorVersion() { + return 0; + } + + @Override + public String getDriverName() throws SQLException { + return null; + } + + @Override + public String getDriverVersion() throws SQLException { + return null; + } + + @Override + public ResultSet getExportedKeys(String catalog, String schema, String table) + throws SQLException { + return null; + } + + @Override + public String getExtraNameCharacters() throws SQLException { + return null; + } + + @Override + public ResultSet getFunctionColumns( + String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) + throws SQLException { + return null; + } + + @Override + public String getIdentifierQuoteString() throws SQLException { + return null; + } + + @Override + public ResultSet getImportedKeys(String catalog, String schema, String table) + throws SQLException { + return null; + } + + @Override + public ResultSet getIndexInfo( + String catalog, String schema, String table, boolean unique, boolean approximate) + throws SQLException { + return null; + } + + @Override + public int getJDBCMajorVersion() throws SQLException { + return 0; + } + + @Override + public int getJDBCMinorVersion() throws SQLException { + return 0; + } + + @Override + public int getMaxBinaryLiteralLength() throws SQLException { + return 0; + } + + @Override + public int getMaxCatalogNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxCharLiteralLength() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInGroupBy() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInIndex() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInOrderBy() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInSelect() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInTable() throws SQLException { + return 0; + } + + @Override + public int getMaxConnections() throws SQLException { + return 0; + } + + @Override + public int getMaxCursorNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxIndexLength() throws SQLException { + return 0; + } + + @Override + public int getMaxProcedureNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxRowSize() throws SQLException { + return 0; + } + + @Override + public int getMaxSchemaNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxStatementLength() throws SQLException { + return 0; + } + + @Override + public int getMaxStatements() throws SQLException { + return 0; + } + + @Override + public int getMaxTableNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxTablesInSelect() throws SQLException { + return 0; + } + + @Override + public int getMaxUserNameLength() throws SQLException { + return 0; + } + + @Override + public String getNumericFunctions() throws SQLException { + return null; + } + + @Override + public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { + return null; + } + + @Override + public ResultSet getProcedureColumns( + String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) + throws SQLException { + return null; + } + + @Override + public String getProcedureTerm() throws SQLException { + return null; + } + + @Override + public ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getPseudoColumns( + String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) + throws SQLException { + return null; + } + + @Override + public int getResultSetHoldability() throws SQLException { + return 0; + } + + @Override + public RowIdLifetime getRowIdLifetime() throws SQLException { + return null; + } + + @Override + public String getSQLKeywords() throws SQLException { + return null; + } + + @Override + public int getSQLStateType() throws SQLException { + return 0; + } + + @Override + public String getSchemaTerm() throws SQLException { + return null; + } + + @Override + public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException { + return null; + } + + @Override + public ResultSet getSchemas() throws SQLException { + return null; + } + + @Override + public String getSearchStringEscape() throws SQLException { + return null; + } + + @Override + public String getStringFunctions() throws SQLException { + return null; + } + + @Override + public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) + throws SQLException { + return null; + } + + @Override + public String getSystemFunctions() throws SQLException { + return null; + } + + @Override + public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getTableTypes() throws SQLException { + return null; + } + + @Override + public ResultSet getTables( + String catalog, String schemaPattern, String tableNamePattern, String[] types) + throws SQLException { + return null; + } + + @Override + public String getTimeDateFunctions() throws SQLException { + return null; + } + + @Override + public ResultSet getTypeInfo() throws SQLException { + return null; + } + + @Override + public ResultSet getUDTs( + String catalog, String schemaPattern, String typeNamePattern, int[] types) + throws SQLException { + return null; + } + + @Override + public String getURL() throws SQLException { + return url; + } + + @Override + public String getUserName() throws SQLException { + return null; + } + + @Override + public ResultSet getVersionColumns(String catalog, String schema, String table) + throws SQLException { + return null; + } + + @Override + public boolean insertsAreDetected(int type) throws SQLException { + return false; + } + + @Override + public boolean isCatalogAtStart() throws SQLException { + return false; + } + + @Override + public boolean isReadOnly() throws SQLException { + return false; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + @Override + public boolean locatorsUpdateCopy() throws SQLException { + return false; + } + + @Override + public boolean nullPlusNonNullIsNull() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedAtEnd() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedAtStart() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedHigh() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedLow() throws SQLException { + return false; + } + + @Override + public boolean othersDeletesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean othersInsertsAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean othersUpdatesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean ownDeletesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean ownInsertsAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean ownUpdatesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean storesLowerCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesLowerCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesMixedCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesMixedCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesUpperCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesUpperCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean supportsANSI92EntryLevelSQL() throws SQLException { + return false; + } + + @Override + public boolean supportsANSI92FullSQL() throws SQLException { + return false; + } + + @Override + public boolean supportsANSI92IntermediateSQL() throws SQLException { + return false; + } + + @Override + public boolean supportsAlterTableWithAddColumn() throws SQLException { + return false; + } + + @Override + public boolean supportsAlterTableWithDropColumn() throws SQLException { + return false; + } + + @Override + public boolean supportsBatchUpdates() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInDataManipulation() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInIndexDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInProcedureCalls() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInTableDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsColumnAliasing() throws SQLException { + return false; + } + + @Override + public boolean supportsConvert() throws SQLException { + return false; + } + + @Override + public boolean supportsConvert(int fromType, int toType) throws SQLException { + return false; + } + + @Override + public boolean supportsCoreSQLGrammar() throws SQLException { + return false; + } + + @Override + public boolean supportsCorrelatedSubqueries() throws SQLException { + return false; + } + + @Override + public boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException { + return false; + } + + @Override + public boolean supportsDataManipulationTransactionsOnly() throws SQLException { + return false; + } + + @Override + public boolean supportsDifferentTableCorrelationNames() throws SQLException { + return false; + } + + @Override + public boolean supportsExpressionsInOrderBy() throws SQLException { + return false; + } + + @Override + public boolean supportsExtendedSQLGrammar() throws SQLException { + return false; + } + + @Override + public boolean supportsFullOuterJoins() throws SQLException { + return false; + } + + @Override + public boolean supportsGetGeneratedKeys() throws SQLException { + return false; + } + + @Override + public boolean supportsGroupBy() throws SQLException { + return false; + } + + @Override + public boolean supportsGroupByBeyondSelect() throws SQLException { + return false; + } + + @Override + public boolean supportsGroupByUnrelated() throws SQLException { + return false; + } + + @Override + public boolean supportsIntegrityEnhancementFacility() throws SQLException { + return false; + } + + @Override + public boolean supportsLikeEscapeClause() throws SQLException { + return false; + } + + @Override + public boolean supportsLimitedOuterJoins() throws SQLException { + return false; + } + + @Override + public boolean supportsMinimumSQLGrammar() throws SQLException { + return false; + } + + @Override + public boolean supportsMixedCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean supportsMultipleOpenResults() throws SQLException { + return false; + } + + @Override + public boolean supportsMultipleResultSets() throws SQLException { + return false; + } + + @Override + public boolean supportsMultipleTransactions() throws SQLException { + return false; + } + + @Override + public boolean supportsNamedParameters() throws SQLException { + return false; + } + + @Override + public boolean supportsNonNullableColumns() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenCursorsAcrossCommit() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenCursorsAcrossRollback() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenStatementsAcrossCommit() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenStatementsAcrossRollback() throws SQLException { + return false; + } + + @Override + public boolean supportsOrderByUnrelated() throws SQLException { + return false; + } + + @Override + public boolean supportsOuterJoins() throws SQLException { + return false; + } + + @Override + public boolean supportsPositionedDelete() throws SQLException { + return false; + } + + @Override + public boolean supportsPositionedUpdate() throws SQLException { + return false; + } + + @Override + public boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException { + return false; + } + + @Override + public boolean supportsResultSetHoldability(int holdability) throws SQLException { + return false; + } + + @Override + public boolean supportsResultSetType(int type) throws SQLException { + return false; + } + + @Override + public boolean supportsSavepoints() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInDataManipulation() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInIndexDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInPrivilegeDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInProcedureCalls() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInTableDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsSelectForUpdate() throws SQLException { + return false; + } + + @Override + public boolean supportsStatementPooling() throws SQLException { + return false; + } + + @Override + public boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException { + return false; + } + + @Override + public boolean supportsStoredProcedures() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInComparisons() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInExists() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInIns() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInQuantifieds() throws SQLException { + return false; + } + + @Override + public boolean supportsTableCorrelationNames() throws SQLException { + return false; + } + + @Override + public boolean supportsTransactionIsolationLevel(int level) throws SQLException { + return false; + } + + @Override + public boolean supportsTransactions() throws SQLException { + return false; + } + + @Override + public boolean supportsUnion() throws SQLException { + return false; + } + + @Override + public boolean supportsUnionAll() throws SQLException { + return false; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean updatesAreDetected(int type) throws SQLException { + return false; + } + + @Override + public boolean usesLocalFilePerTable() throws SQLException { + return false; + } + + @Override + public boolean usesLocalFiles() throws SQLException { + return false; + } +} diff --git a/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestDriver.java b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestDriver.java new file mode 100644 index 000000000000..dd0e185dff3f --- /dev/null +++ b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestDriver.java @@ -0,0 +1,52 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jdbc; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; +import java.util.logging.Logger; + +public class TestDriver implements Driver { + @Override + public Connection connect(String url, Properties info) throws SQLException { + return new TestConnection(); + } + + @Override + public boolean acceptsURL(String url) throws SQLException { + return url.startsWith("jdbc:test:"); + } + + @Override + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + DriverPropertyInfo driverPropertyInfo = new DriverPropertyInfo("test", "test"); + return new DriverPropertyInfo[] {driverPropertyInfo}; + } + + @Override + public int getMajorVersion() { + return 0; + } + + @Override + public int getMinorVersion() { + return 0; + } + + @Override + public boolean jdbcCompliant() { + return false; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } +} diff --git a/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestPreparedStatement.java b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestPreparedStatement.java new file mode 100644 index 000000000000..8c8c0092a3db --- /dev/null +++ b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestPreparedStatement.java @@ -0,0 +1,220 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jdbc; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.Date; +import java.sql.NClob; +import java.sql.ParameterMetaData; +import java.sql.PreparedStatement; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLXML; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; + +class TestPreparedStatement extends TestStatement implements PreparedStatement { + + TestPreparedStatement() { + super(); + } + + TestPreparedStatement(Connection connection) { + super(connection); + } + + @Override + public void addBatch() throws SQLException {} + + @Override + public void clearParameters() throws SQLException {} + + @Override + public boolean execute() throws SQLException { + return true; + } + + @Override + public ResultSet executeQuery() throws SQLException { + return null; + } + + @Override + public int executeUpdate() throws SQLException { + return 0; + } + + @Override + public ResultSetMetaData getMetaData() throws SQLException { + return null; + } + + @Override + public ParameterMetaData getParameterMetaData() throws SQLException { + return null; + } + + @Override + public void setArray(int parameterIndex, Array x) throws SQLException {} + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {} + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {} + + @Override + public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {} + + @Override + public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {} + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {} + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {} + + @Override + public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {} + + @Override + public void setBlob(int parameterIndex, Blob x) throws SQLException {} + + @Override + public void setBlob(int parameterIndex, InputStream inputStream, long length) + throws SQLException {} + + @Override + public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {} + + @Override + public void setBoolean(int parameterIndex, boolean x) throws SQLException {} + + @Override + public void setByte(int parameterIndex, byte x) throws SQLException {} + + @Override + public void setBytes(int parameterIndex, byte[] x) throws SQLException {} + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, int length) + throws SQLException {} + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, long length) + throws SQLException {} + + @Override + public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {} + + @Override + public void setClob(int parameterIndex, Clob x) throws SQLException {} + + @Override + public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {} + + @Override + public void setClob(int parameterIndex, Reader reader) throws SQLException {} + + @Override + public void setDate(int parameterIndex, Date x) throws SQLException {} + + @Override + public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {} + + @Override + public void setDouble(int parameterIndex, double x) throws SQLException {} + + @Override + public void setFloat(int parameterIndex, float x) throws SQLException {} + + @Override + public void setInt(int parameterIndex, int x) throws SQLException {} + + @Override + public void setLong(int parameterIndex, long x) throws SQLException {} + + @Override + public void setNCharacterStream(int parameterIndex, Reader value, long length) + throws SQLException {} + + @Override + public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {} + + @Override + public void setNClob(int parameterIndex, NClob value) throws SQLException {} + + @Override + public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {} + + @Override + public void setNClob(int parameterIndex, Reader reader) throws SQLException {} + + @Override + public void setNString(int parameterIndex, String value) throws SQLException {} + + @Override + public void setNull(int parameterIndex, int sqlType) throws SQLException {} + + @Override + public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {} + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {} + + @Override + public void setObject(int parameterIndex, Object x) throws SQLException {} + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) + throws SQLException {} + + @Override + public void setRef(int parameterIndex, Ref x) throws SQLException {} + + @Override + public void setRowId(int parameterIndex, RowId x) throws SQLException {} + + @Override + public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {} + + @Override + public void setShort(int parameterIndex, short x) throws SQLException {} + + @Override + public void setString(int parameterIndex, String x) throws SQLException {} + + @Override + public void setTime(int parameterIndex, Time x) throws SQLException {} + + @Override + public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {} + + @Override + public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {} + + @Override + public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {} + + @Override + public void setURL(int parameterIndex, URL x) throws SQLException {} + + @Override + @Deprecated + public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {} +} diff --git a/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestStatement.java b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestStatement.java new file mode 100644 index 000000000000..b6db71df4565 --- /dev/null +++ b/instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/TestStatement.java @@ -0,0 +1,216 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jdbc; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.Statement; + +class TestStatement implements Statement { + final Connection connection; + + TestStatement() { + this.connection = null; + } + + TestStatement(Connection connection) { + this.connection = connection; + } + + @Override + public void addBatch(String sql) throws SQLException {} + + @Override + public void cancel() throws SQLException {} + + @Override + public void clearBatch() throws SQLException {} + + @Override + public void clearWarnings() throws SQLException {} + + @Override + public void close() throws SQLException {} + + @Override + public void closeOnCompletion() throws SQLException {} + + @Override + public boolean execute(String sql) throws SQLException { + return true; + } + + @Override + public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { + return true; + } + + @Override + public boolean execute(String sql, int[] columnIndexes) throws SQLException { + return true; + } + + @Override + public boolean execute(String sql, String[] columnNames) throws SQLException { + return true; + } + + @Override + public int[] executeBatch() throws SQLException { + return new int[0]; + } + + @Override + public ResultSet executeQuery(String sql) throws SQLException { + return null; + } + + @Override + public int executeUpdate(String sql) throws SQLException { + return 0; + } + + @Override + public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { + return 0; + } + + @Override + public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { + return 0; + } + + @Override + public int executeUpdate(String sql, String[] columnNames) throws SQLException { + return 0; + } + + @Override + public Connection getConnection() throws SQLException { + return connection; + } + + @Override + public int getFetchDirection() throws SQLException { + return 0; + } + + @Override + public int getFetchSize() throws SQLException { + return 0; + } + + @Override + public ResultSet getGeneratedKeys() throws SQLException { + return null; + } + + @Override + public int getMaxFieldSize() throws SQLException { + return 0; + } + + @Override + public int getMaxRows() throws SQLException { + return 0; + } + + @Override + public boolean getMoreResults() throws SQLException { + return false; + } + + @Override + public boolean getMoreResults(int current) throws SQLException { + return false; + } + + @Override + public int getQueryTimeout() throws SQLException { + return 0; + } + + @Override + public ResultSet getResultSet() throws SQLException { + return null; + } + + @Override + public int getResultSetConcurrency() throws SQLException { + return 0; + } + + @Override + public int getResultSetHoldability() throws SQLException { + return 0; + } + + @Override + public int getResultSetType() throws SQLException { + return 0; + } + + @Override + public int getUpdateCount() throws SQLException { + return 0; + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public boolean isCloseOnCompletion() throws SQLException { + return false; + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public boolean isPoolable() throws SQLException { + return false; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + @Override + public void setCursorName(String name) throws SQLException {} + + @Override + public void setEscapeProcessing(boolean enable) throws SQLException {} + + @Override + public void setFetchDirection(int direction) throws SQLException {} + + @Override + public void setFetchSize(int rows) throws SQLException {} + + @Override + public void setMaxFieldSize(int max) throws SQLException {} + + @Override + public void setMaxRows(int max) throws SQLException {} + + @Override + public void setPoolable(boolean poolable) throws SQLException {} + + @Override + public void setQueryTimeout(int seconds) throws SQLException {} + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } +}