@@ -866,6 +866,17 @@ public boolean delete(JobId jobId) {
866866 Strings .isNullOrEmpty (jobId .getProject ())
867867 ? getOptions ().getProjectId ()
868868 : jobId .getProject ());
869+ Span jobDelete = null ;
870+ if (getOptions ().isOpenTelemetryTracingEnabled ()
871+ && getOptions ().getOpenTelemetryTracer () != null ) {
872+ jobDelete =
873+ getOptions ()
874+ .getOpenTelemetryTracer ()
875+ .spanBuilder ("com.google.cloud.bigquery.BigQuery.deleteJob" )
876+ .setAttribute ("language" , "Java" )
877+ .setAllAttributes (completeJobId .getOtelAttributes ())
878+ .startSpan ();
879+ }
869880 try {
870881 return BigQueryRetryHelper .runWithRetries (
871882 new Callable <Boolean >() {
@@ -881,6 +892,10 @@ public Boolean call() throws IOException {
881892 EMPTY_RETRY_CONFIG );
882893 } catch (BigQueryRetryHelperException e ) {
883894 throw BigQueryException .translateAndThrow (e );
895+ } finally {
896+ if (jobDelete != null ) {
897+ jobDelete .end ();
898+ }
884899 }
885900 }
886901
@@ -1382,26 +1397,43 @@ && getOptions().getOpenTelemetryTracer() != null) {
13821397
13831398 @ Override
13841399 public List <String > listPartitions (TableId tableId ) {
1385- List <String > partitions = new ArrayList <String >();
1386- String partitionsTable = tableId .getTable () + "$__PARTITIONS_SUMMARY__" ;
1387- TableId metaTableId =
1388- tableId .getProject () == null
1389- ? TableId .of (tableId .getDataset (), partitionsTable )
1390- : TableId .of (tableId .getProject (), tableId .getDataset (), partitionsTable );
1391- Table metaTable = getTable (metaTableId );
1392- Schema metaSchema = metaTable .getDefinition ().getSchema ();
1393- String partition_id = null ;
1394- for (Field field : metaSchema .getFields ()) {
1395- if (field .getName ().equals ("partition_id" )) {
1396- partition_id = field .getName ();
1397- break ;
1398- }
1400+ Span listPartitions = null ;
1401+ if (getOptions ().isOpenTelemetryTracingEnabled ()
1402+ && getOptions ().getOpenTelemetryTracer () != null ) {
1403+ listPartitions =
1404+ getOptions ()
1405+ .getOpenTelemetryTracer ()
1406+ .spanBuilder ("com.google.cloud.bigquery.BigQuery.listPartitions" )
1407+ .setAttribute ("language" , "Java" )
1408+ .setAllAttributes (tableId .getOtelAttributes ())
1409+ .startSpan ();
13991410 }
1400- TableResult result = metaTable .list (metaSchema );
1401- for (FieldValueList list : result .iterateAll ()) {
1402- partitions .add (list .get (partition_id ).getStringValue ());
1411+ try (Scope listPartitionsScope = listPartitions != null ? listPartitions .makeCurrent () : null ) {
1412+ List <String > partitions = new ArrayList <String >();
1413+ String partitionsTable = tableId .getTable () + "$__PARTITIONS_SUMMARY__" ;
1414+ TableId metaTableId =
1415+ tableId .getProject () == null
1416+ ? TableId .of (tableId .getDataset (), partitionsTable )
1417+ : TableId .of (tableId .getProject (), tableId .getDataset (), partitionsTable );
1418+ Table metaTable = getTable (metaTableId );
1419+ Schema metaSchema = metaTable .getDefinition ().getSchema ();
1420+ String partition_id = null ;
1421+ for (Field field : metaSchema .getFields ()) {
1422+ if (field .getName ().equals ("partition_id" )) {
1423+ partition_id = field .getName ();
1424+ break ;
1425+ }
1426+ }
1427+ TableResult result = metaTable .list (metaSchema );
1428+ for (FieldValueList list : result .iterateAll ()) {
1429+ partitions .add (list .get (partition_id ).getStringValue ());
1430+ }
1431+ return partitions ;
1432+ } finally {
1433+ if (listPartitions != null ) {
1434+ listPartitions .end ();
1435+ }
14031436 }
1404- return partitions ;
14051437 }
14061438
14071439 private static Page <Table > listTables (
@@ -1886,20 +1918,38 @@ public TableResult query(QueryJobConfiguration configuration, JobOption... optio
18861918 .build ();
18871919 }
18881920
1889- // If all parameters passed in configuration are supported by the query() method on the backend,
1890- // put on fast path
1891- QueryRequestInfo requestInfo =
1892- new QueryRequestInfo (configuration , getOptions ().getUseInt64Timestamps ());
1893- if (requestInfo .isFastQuerySupported (null )) {
1894- String projectId = getOptions ().getProjectId ();
1895- QueryRequest content = requestInfo .toPb ();
1896- if (getOptions ().getLocation () != null ) {
1897- content .setLocation (getOptions ().getLocation ());
1921+ Span querySpan = null ;
1922+ if (getOptions ().isOpenTelemetryTracingEnabled ()
1923+ && getOptions ().getOpenTelemetryTracer () != null ) {
1924+ querySpan =
1925+ getOptions ()
1926+ .getOpenTelemetryTracer ()
1927+ .spanBuilder ("com.google.cloud.bigquery.BigQuery.query" )
1928+ .setAllAttributes (configuration .getOtelAttributes ())
1929+ .setAllAttributes (otelAttributesFromOptions (options ))
1930+ .startSpan ();
1931+ }
1932+ try (Scope queryScope = querySpan != null ? querySpan .makeCurrent () : null ) {
1933+ // If all parameters passed in configuration are supported by the query() method on the
1934+ // backend,
1935+ // put on fast path
1936+ QueryRequestInfo requestInfo =
1937+ new QueryRequestInfo (configuration , getOptions ().getUseInt64Timestamps ());
1938+ if (requestInfo .isFastQuerySupported (null )) {
1939+ String projectId = getOptions ().getProjectId ();
1940+ QueryRequest content = requestInfo .toPb ();
1941+ if (getOptions ().getLocation () != null ) {
1942+ content .setLocation (getOptions ().getLocation ());
1943+ }
1944+ return queryRpc (projectId , content , options );
1945+ }
1946+ // Otherwise, fall back to the existing create query job logic
1947+ return create (JobInfo .of (configuration ), options ).getQueryResults ();
1948+ } finally {
1949+ if (querySpan != null ) {
1950+ querySpan .end ();
18981951 }
1899- return queryRpc (projectId , content , options );
19001952 }
1901- // Otherwise, fall back to the existing create query job logic
1902- return create (JobInfo .of (configuration ), options ).getQueryResults ();
19031953 }
19041954
19051955 private TableResult queryRpc (
@@ -2008,31 +2058,51 @@ public TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOp
20082058 throws InterruptedException , JobException {
20092059 Job .checkNotDryRun (configuration , "query" );
20102060
2011- // If all parameters passed in configuration are supported by the query() method on the backend,
2012- // put on fast path
2013- QueryRequestInfo requestInfo =
2014- new QueryRequestInfo (configuration , getOptions ().getUseInt64Timestamps ());
2015- if (requestInfo .isFastQuerySupported (jobId )) {
2016- // Be careful when setting the projectID in JobId, if a projectID is specified in the JobId,
2017- // the job created by the query method will use that project. This may cause the query to
2018- // fail with "Access denied" if the project do not have enough permissions to run the job.
2019-
2020- String projectId =
2021- jobId .getProject () != null ? jobId .getProject () : getOptions ().getProjectId ();
2022- QueryRequest content = requestInfo .toPb ();
2023- // Be careful when setting the location, if a location is specified in the BigQueryOption or
2024- // JobId the job created by the query method will be in that location, even if the table to be
2025- // queried is in a different location. This may cause the query to fail with
2026- // "BigQueryException: Not found"
2027- if (jobId .getLocation () != null ) {
2028- content .setLocation (jobId .getLocation ());
2029- } else if (getOptions ().getLocation () != null ) {
2030- content .setLocation (getOptions ().getLocation ());
2031- }
2061+ Span querySpan = null ;
2062+ if (getOptions ().isOpenTelemetryTracingEnabled ()
2063+ && getOptions ().getOpenTelemetryTracer () != null ) {
2064+ querySpan =
2065+ getOptions ()
2066+ .getOpenTelemetryTracer ()
2067+ .spanBuilder ("com.google.cloud.bigquery.BigQuery.query" )
2068+ .setAllAttributes (configuration .getOtelAttributes ())
2069+ .setAllAttributes (jobId .getOtelAttributes ())
2070+ .setAllAttributes (otelAttributesFromOptions (options ))
2071+ .startSpan ();
2072+ }
2073+ try (Scope queryScope = querySpan != null ? querySpan .makeCurrent () : null ) {
2074+ // If all parameters passed in configuration are supported by the query() method on the
2075+ // backend,
2076+ // put on fast path
2077+ QueryRequestInfo requestInfo =
2078+ new QueryRequestInfo (configuration , getOptions ().getUseInt64Timestamps ());
2079+ if (requestInfo .isFastQuerySupported (jobId )) {
2080+ // Be careful when setting the projectID in JobId, if a projectID is specified in the JobId,
2081+ // the job created by the query method will use that project. This may cause the query to
2082+ // fail with "Access denied" if the project do not have enough permissions to run the job.
2083+
2084+ String projectId =
2085+ jobId .getProject () != null ? jobId .getProject () : getOptions ().getProjectId ();
2086+ QueryRequest content = requestInfo .toPb ();
2087+ // Be careful when setting the location, if a location is specified in the BigQueryOption or
2088+ // JobId the job created by the query method will be in that location, even if the table to
2089+ // be
2090+ // queried is in a different location. This may cause the query to fail with
2091+ // "BigQueryException: Not found"
2092+ if (jobId .getLocation () != null ) {
2093+ content .setLocation (jobId .getLocation ());
2094+ } else if (getOptions ().getLocation () != null ) {
2095+ content .setLocation (getOptions ().getLocation ());
2096+ }
20322097
2033- return queryRpc (projectId , content , options );
2098+ return queryRpc (projectId , content , options );
2099+ }
2100+ return create (JobInfo .of (jobId , configuration ), options ).getQueryResults ();
2101+ } finally {
2102+ if (querySpan != null ) {
2103+ querySpan .end ();
2104+ }
20342105 }
2035- return create (JobInfo .of (jobId , configuration ), options ).getQueryResults ();
20362106 }
20372107
20382108 @ Override
@@ -2302,8 +2372,6 @@ private static Attributes otelAttributesFromQueryRequest(QueryRequest request) {
23022372 .put ("location" , getFieldAsString (request .getLocation ()))
23032373 .put ("maxResults" , getFieldAsString (request .getMaxResults ()))
23042374 .put ("maximumBytesBilled" , getFieldAsString (request .getMaximumBytesBilled ()))
2305- .put ("query" , getFieldAsString (request .getQuery ()))
2306- .put ("queryParameters" , getFieldAsString (request .getQueryParameters ()))
23072375 .put ("requestId" , getFieldAsString (request .getRequestId ()))
23082376 .put ("reservation" , getFieldAsString (request .getReservation ()))
23092377 .put ("timeoutMs" , getFieldAsString (request .getTimeoutMs ()))
0 commit comments