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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions java-bigquery-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
<configuration>
<systemPropertyVariables>
<JDBC_TESTS>true</JDBC_TESTS>
<!-- Test-only: keeps integration runs from emitting driver usage telemetry. -->
<BIGQUERY_JDBC_TELEMETRY_ENABLED>false</BIGQUERY_JDBC_TELEMETRY_ENABLED>
</systemPropertyVariables>
</configuration>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.opentelemetry.api.OpenTelemetry;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
Expand Down Expand Up @@ -196,10 +197,12 @@ public Connection connect(String url, Properties info) throws SQLException {
LOG.log(Level.SEVERE, "Failed to parse connection URL", e);
throw new BigQueryJdbcException("Failed to parse connection URL", e);
}
authType = TelemetryManager.toAuthenticationType(ds.getOAuthType());
if (customOpenTelemetryObj instanceof OpenTelemetry) {
ds.setCustomOpenTelemetry((OpenTelemetry) customOpenTelemetryObj);
}
BigQueryConnection connection = new BigQueryConnection(connectionUri, ds);
DatabaseMetaData metaData = connection.getMetaData();
LOG.info(
"Driver info : { {Database Product Name : %s}, "
+ "{Database Product Version : %s}, "
Expand All @@ -208,17 +211,16 @@ public Connection connect(String url, Properties info) throws SQLException {
+ "{LogLevel : %s}, "
+ "{LogPath : %s}, "
+ "{Driver Instance : %s} }",
connection.getMetaData().getDatabaseProductName(),
connection.getMetaData().getDatabaseProductVersion(),
connection.getMetaData().getDriverName(),
connection.getMetaData().getDriverVersion(),
metaData.getDatabaseProductName(),
metaData.getDatabaseProductVersion(),
metaData.getDriverName(),
metaData.getDriverVersion(),
logLevel,
logPath,
this.toString());

Connection wrapped = BigQueryJdbcContextProxy.wrap(connection, Connection.class);

authType = TelemetryManager.toAuthenticationType(ds.getOAuthType());
TelemetryManager.recordConnectionAttempt(Status.STATUS_SUCCESS, 0, authType);
return wrapped;
} else {
Expand All @@ -227,8 +229,7 @@ public Connection connect(String url, Properties info) throws SQLException {
} catch (IOException e) {
TelemetryManager.recordConnectionAttempt(
Status.STATUS_ERROR, TelemetryManager.extractErrorCode(e), authType);
LOG.warning("Getting a warning: %s", e.getMessage());
return null;
throw new BigQueryJdbcException("Failed to establish connection", e);
} catch (Throwable t) {
TelemetryManager.recordConnectionAttempt(
Status.STATUS_ERROR, TelemetryManager.extractErrorCode(t), authType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ void setParameter(int parameterIndex, Object value, Class type) {
parameter.setParamType(BigQueryStatementParameterType.UNSPECIFIED);
parameter.setScale(-1);

TelemetryManager.recordFeatureUsage(DriverFeature.DRIVER_FEATURE_PARAMETER_BINDING);

LOG.finest("Parameter set { %s }", parameter.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.cloud.bigquery.jdbc;

import com.google.cloud.bigquery.jdbc.telemetry.v1.DriverFeature;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import com.google.common.annotations.VisibleForTesting;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
Expand Down Expand Up @@ -237,7 +235,6 @@ public void rollback() throws SQLException {

@Override
public DatabaseMetaData getMetaData() throws SQLException {
TelemetryManager.recordFeatureUsage(DriverFeature.DRIVER_FEATURE_METADATA_RETRIEVAL);
return bqConnectionDelegate.getMetaData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void close() throws SQLException {

boolean cancelSucceeded = false;
try {
cancel(); // This attempts to cancel jobs and calls closeStatementResources()
cancelInternal(); // Cancels jobs and calls closeStatementResources(); no telemetry.
cancelSucceeded = true;
} catch (SQLException e) {
LOG.warning("Failed to cancel statement during close().", e);
Expand Down Expand Up @@ -467,6 +467,16 @@ public void setQueryTimeout(int seconds) {
@Override
public void cancel() throws SQLException {
LOG.finer("Statement %s cancelled", this);
cancelInternal();
TelemetryManager.recordFeatureUsage(DriverFeature.DRIVER_FEATURE_STATEMENT_CANCEL);
}

/**
* Cancels running jobs and releases statement resources without recording telemetry. Used by
* {@link #close()}, where cancellation is housekeeping, not a user-initiated cancel.
*/
private void cancelInternal() throws SQLException {
LOG.finer("Statement %s cancelled internally", this);
synchronized (cancelLock) {
this.isCanceled = true;
for (JobId jobId : this.jobIds) {
Expand All @@ -488,8 +498,6 @@ public void cancel() throws SQLException {
// If a ResultSet exists, then it will be closed as well, closing the
// ownedThreads
closeStatementResources();

TelemetryManager.recordFeatureUsage(DriverFeature.DRIVER_FEATURE_STATEMENT_CANCEL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@
package com.google.cloud.bigquery.jdbc;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;

import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.jdbc.telemetry.v1.AuthenticationType;
import com.google.cloud.bigquery.jdbc.telemetry.v1.DriverFeature;
import com.google.cloud.bigquery.jdbc.telemetry.v1.Status;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryTestUtils;
import com.google.cloud.bigquery.jdbc.utils.BigQueryJdbcVersionUtility;
import io.opentelemetry.api.OpenTelemetry;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
Expand All @@ -32,11 +43,19 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

public class BigQueryDriverTest extends BigQueryJdbcLoggingBaseTest {

static BigQueryDriver bigQueryDriver;

/** Token-auth URL that connects without network access; shared by the telemetry tests. */
private static final String VALID_TOKEN_URL =
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
+ "OAuthAccessToken=redactedToken;OAuthClientId=redactedToken;"
+ "OAuthClientSecret=redactedToken;";

@BeforeEach
@AfterEach
public void resetTelemetry() {
Expand All @@ -55,13 +74,7 @@ public void testInvalidURLReturnsNull() throws SQLException {

@Test
public void testValidURLDoesConnect() throws SQLException {
Connection connection =
bigQueryDriver.connect(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
+ "OAuthAccessToken=redactedToken;OAuthClientId=redactedToken;"
+ "OAuthClientSecret=redactedToken;",
new Properties());
Connection connection = bigQueryDriver.connect(VALID_TOKEN_URL, new Properties());
assertThat(connection.isClosed()).isFalse();
}

Expand Down Expand Up @@ -199,13 +212,7 @@ public void testInvalidLogLevelExceptionIsLogged() {

@Test
public void testConnect_recordsSuccessfulConnectionTelemetry() throws SQLException {
Connection connection =
bigQueryDriver.connect(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
+ "OAuthAccessToken=redactedToken;OAuthClientId=redactedToken;"
+ "OAuthClientSecret=redactedToken;",
new Properties());
Connection connection = bigQueryDriver.connect(VALID_TOKEN_URL, new Properties());
assertThat(connection).isNotNull();
assertThat(connection.isClosed()).isFalse();
// Verify TelemetryManager is initialized and recorded the connection
Expand All @@ -227,14 +234,62 @@ public void testConnect_recordsFailedConnectionTelemetry() {
@Test
public void testConnect_optOut_noTelemetryRecorded() throws SQLException {
Connection connection =
bigQueryDriver.connect(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
+ "OAuthAccessToken=redactedToken;OAuthClientId=redactedToken;"
+ "OAuthClientSecret=redactedToken;EnableDiagnosticTelemetry=0;",
new Properties());
bigQueryDriver.connect(VALID_TOKEN_URL + "EnableDiagnosticTelemetry=0;", new Properties());
assertThat(connection).isNotNull();
// Since opt-out was requested, TelemetryManager should NOT be initialized
assertThat(TelemetryManager.isInitialized()).isFalse();
}

@Test
public void testConnect_recordsMetadataRetrievalOnce() throws SQLException {
// connect() logs four DatabaseMetaData values; it must fetch the object once, not four times.
try (MockedStatic<TelemetryManager> telemetry =
mockStatic(TelemetryManager.class, CALLS_REAL_METHODS)) {
bigQueryDriver.connect(VALID_TOKEN_URL, new Properties());
telemetry.verify(
() ->
TelemetryManager.recordFeatureUsage(DriverFeature.DRIVER_FEATURE_METADATA_RETRIEVAL),
times(1));
}
}

@Test
public void testConnect_failedAttemptCarriesAuthType() {
// Service-account auth with no key material parses fine but fails while building the
// connection. The failed attempt must report the auth type, not
// AUTHENTICATION_TYPE_UNSPECIFIED.
try (MockedStatic<TelemetryManager> telemetry =
mockStatic(TelemetryManager.class, CALLS_REAL_METHODS)) {
Assertions.assertThrows(
Exception.class,
() ->
bigQueryDriver.connect(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=0;ProjectId=MyBigQueryProject;",
new Properties()));
telemetry.verify(
() ->
TelemetryManager.recordConnectionAttempt(
eq(Status.STATUS_ERROR),
anyInt(),
eq(AuthenticationType.AUTHENTICATION_TYPE_SERVICE_ACCOUNT)));
}
}

@Test
public void testConnect_ioExceptionThrowsInsteadOfReturningNull() {
// BigQueryJdbcRootLogger.setLevel is the only IOException source on the connect path. JDBC
// requires connect() to throw for an accepted URL that fails, not return null.
try (MockedStatic<BigQueryJdbcRootLogger> rootLogger =
mockStatic(BigQueryJdbcRootLogger.class)) {
rootLogger
.when(() -> BigQueryJdbcRootLogger.setLevel(any(), any()))
.thenThrow(new IOException("simulated log path failure"));
BigQueryJdbcException thrown =
Assertions.assertThrows(
BigQueryJdbcException.class,
() -> bigQueryDriver.connect(VALID_TOKEN_URL, new Properties()));
assertThat(thrown.getCause()).isInstanceOf(IOException.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import com.google.cloud.bigquery.jdbc.telemetry.v1.DriverFeature;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import java.io.IOException;
import java.sql.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

public class BigQueryPooledConnectionTest {
private BigQueryConnection bigQueryConnection;
Expand Down Expand Up @@ -173,4 +179,19 @@ public void testFireConnectionError() throws SQLException {

assertFalse(pooledConnection.isListenerPooled(listner));
}

@Test
public void testPooledGetMetaData_delegatesWithoutRecordingTelemetry() throws SQLException {
// The physical BigQueryConnection records DRIVER_FEATURE_METADATA_RETRIEVAL; the pooled
// wrapper must only delegate, otherwise every pooled call is counted twice.
BigQueryPooledConnection pooledConnection = new BigQueryPooledConnection(bigQueryConnection);
try (MockedStatic<TelemetryManager> telemetry = mockStatic(TelemetryManager.class)) {
pooledConnection.getConnection().getMetaData();
verify(bigQueryConnection).getMetaData();
telemetry.verify(
() ->
TelemetryManager.recordFeatureUsage(DriverFeature.DRIVER_FEATURE_METADATA_RETRIEVAL),
never());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

import com.google.cloud.bigquery.Field;
Expand All @@ -34,6 +36,8 @@
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.jdbc.telemetry.v1.DriverFeature;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import com.google.gson.Gson;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
Expand All @@ -59,6 +63,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.MockedStatic;

public class BigQueryPreparedStatementSettersTest {

Expand Down Expand Up @@ -532,4 +537,18 @@ public void testSetObjectLocalDateTime_zeroSecondsStillHasFullDigits() throws SQ
preparedStatement.setObject(1, LocalDateTime.of(2025, 1, 1, 12, 0));
assertEquals("2025-01-01 12:00:00.000000", preparedStatement.parameterHandler.getParameter(1));
}

@Test
public void testPrimitiveSetters_recordParameterBindingFeature() throws SQLException {
// These setters use the (int, Object, Class) overload of BigQueryParameterHandler, which
// previously did not record DRIVER_FEATURE_PARAMETER_BINDING. One record per binding.
try (MockedStatic<TelemetryManager> telemetry = mockStatic(TelemetryManager.class)) {
preparedStatement.setInt(1, 42);
preparedStatement.setLong(2, 42L);
preparedStatement.setString(3, "x");
telemetry.verify(
() -> TelemetryManager.recordFeatureUsage(DriverFeature.DRIVER_FEATURE_PARAMETER_BINDING),
times(3));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
import com.google.cloud.bigquery.TableResult;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.jdbc.BigQueryStatement.JobIdWrapper;
import com.google.cloud.bigquery.jdbc.telemetry.v1.DriverFeature;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import com.google.cloud.bigquery.spi.BigQueryRpcFactory;
import com.google.cloud.bigquery.storage.v1.ArrowRecordBatch;
import com.google.cloud.bigquery.storage.v1.ArrowSchema;
Expand Down Expand Up @@ -125,6 +127,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -1887,4 +1890,32 @@ private Job createScriptChildJobMock(
doReturn(mock(ScriptStatistics.class)).when(stats).getScriptStatistics();
return childJob;
}

@Test
public void testClose_doesNotRecordStatementCancelFeature() throws SQLException {
// close() cancels jobs as housekeeping; that must not count as a user-initiated cancel.
assertFeatureRecorded(
() -> bigQueryStatement.close(), DriverFeature.DRIVER_FEATURE_STATEMENT_CANCEL, 0);
}

@Test
public void testCancel_recordsStatementCancelFeatureOnce() throws SQLException {
assertFeatureRecorded(
() -> bigQueryStatement.cancel(), DriverFeature.DRIVER_FEATURE_STATEMENT_CANCEL, 1);
}

/** Runs {@code action} and asserts how many times {@code feature} was recorded by it. */
private static void assertFeatureRecorded(
SqlRunnable action, DriverFeature feature, int expectedCount) throws SQLException {
try (MockedStatic<TelemetryManager> telemetry = Mockito.mockStatic(TelemetryManager.class)) {
action.run();
telemetry.verify(
() -> TelemetryManager.recordFeatureUsage(feature), Mockito.times(expectedCount));
}
}

@FunctionalInterface
private interface SqlRunnable {
void run() throws SQLException;
}
}
Loading