2828import com .google .cloud .bigquery .exception .BigQueryJdbcException ;
2929import com .google .cloud .bigquery .exception .BigQueryJdbcRuntimeException ;
3030import com .google .cloud .bigquery .exception .BigQueryJdbcSqlFeatureNotSupportedException ;
31+ import com .google .cloud .bigquery .jdbc .telemetry .v1 .DriverFeature ;
32+ import com .google .cloud .bigquery .jdbc .telemetry .v1 .StatementExecution ;
33+ import com .google .cloud .bigquery .jdbc .telemetry .v1 .TelemetryManager ;
3134import com .google .cloud .bigquery .storage .v1 .BatchCommitWriteStreamsRequest ;
3235import com .google .cloud .bigquery .storage .v1 .BatchCommitWriteStreamsResponse ;
3336import com .google .cloud .bigquery .storage .v1 .BigQueryWriteClient ;
@@ -323,7 +326,16 @@ public int[] executeBatch() throws SQLException {
323326 if (this .batchParameters .isEmpty ()) {
324327 return result ;
325328 }
329+
326330 if (useWriteAPI ()) {
331+ long startTime = System .currentTimeMillis ();
332+ StatementExecution .Builder writeApiExecutionBuilder =
333+ StatementExecution .newBuilder ()
334+ .setStatementType (
335+ com .google .cloud .bigquery .jdbc .telemetry .v1 .StatementType .STATEMENT_TYPE_INSERT )
336+ .setQueryApiType (
337+ com .google .cloud .bigquery .jdbc .telemetry .v1 .QueryApiType
338+ .QUERY_API_TYPE_WRITE_API );
327339 try (BigQueryWriteClient writeClient = this .connection .getBigQueryWriteClient ()) {
328340 LOG .info ("Using Write API for bulk INSERT operation." );
329341 ArrayList <BigQueryJdbcParameter > currentParameterList = this .batchParameters .peek ();
@@ -336,10 +348,24 @@ public int[] executeBatch() throws SQLException {
336348 long rowCount = bulkInsertWithWriteAPI (writeClient );
337349 int [] insertArray = new int [Math .toIntExact (rowCount )];
338350 Arrays .fill (insertArray , 1 );
351+
352+ writeApiExecutionBuilder .setStatus (
353+ com .google .cloud .bigquery .jdbc .telemetry .v1 .Status .STATUS_SUCCESS );
354+
339355 return insertArray ;
340356
341357 } catch (DescriptorValidationException | IOException | InterruptedException e ) {
358+ writeApiExecutionBuilder
359+ .setStatus (com .google .cloud .bigquery .jdbc .telemetry .v1 .Status .STATUS_ERROR )
360+ .setErrorCode (TelemetryManager .extractErrorCode (e ));
361+ if (e instanceof InterruptedException ) {
362+ Thread .currentThread ().interrupt ();
363+ }
342364 throw new BigQueryJdbcRuntimeException ("Failed to execute batch with Write API" , e );
365+ } finally {
366+ long durationMs = System .currentTimeMillis () - startTime ;
367+ TelemetryManager .recordStatementExecution (writeApiExecutionBuilder , durationMs );
368+ TelemetryManager .recordFeatureUsage (DriverFeature .DRIVER_FEATURE_BATCH_OPERATIONS );
343369 }
344370
345371 } else {
@@ -370,6 +396,8 @@ public int[] executeBatch() throws SQLException {
370396 throw new BigQueryJdbcRuntimeException ("Interrupted during individual INSERT batch" , ex );
371397 } catch (SQLException e ) {
372398 throw new BigQueryJdbcException ("SQL error during individual INSERT batch" , e );
399+ } finally {
400+ TelemetryManager .recordFeatureUsage (DriverFeature .DRIVER_FEATURE_BATCH_OPERATIONS );
373401 }
374402 }
375403 }
@@ -542,6 +570,11 @@ public ResultSetMetaData getMetaData() throws SQLException {
542570 if (this .insertSchema != null ) {
543571 return BigQueryResultSetMetadata .of (this .insertSchema .getFields (), this );
544572 }
573+
574+ TelemetryManager .recordFeatureUsage (
575+ DriverFeature .DRIVER_FEATURE_METADATA_RETRIEVAL ,
576+ "DRIVER_FEATURE_RESULTSET_METADATA_RETRIEVAL" );
577+
545578 return null ;
546579 }
547580
0 commit comments