Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 8c52f6f

Browse files
committed
Add spans to query and listPartitions
1 parent c988c2e commit 8c52f6f

7 files changed

Lines changed: 147 additions & 61 deletions

File tree

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

Lines changed: 123 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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()))

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public boolean getUseInt64Timestamps() {
199199
*
200200
* @return true if tracing is enabled, false if not
201201
*/
202-
@BetaApi
202+
@BetaApi("Span names and attributes are subject to change without notice")
203203
public boolean isOpenTelemetryTracingEnabled() {
204204
return enableOpenTelemetryTracing;
205205
}
@@ -209,7 +209,7 @@ public boolean isOpenTelemetryTracingEnabled() {
209209
*
210210
* @return OpenTelemetry tracer object or {@code null} if not set
211211
*/
212-
@BetaApi
212+
@BetaApi("Span names and attributes are subject to change without notice")
213213
public Tracer getOpenTelemetryTracer() {
214214
return openTelemetryTracer;
215215
}

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetId.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ protected Attributes getOtelAttributes() {
9090
return Attributes.builder()
9191
.put("project", this.getProject())
9292
.put("dataset", this.getDataset())
93-
.put("db.name", this.getDataset())
9493
.build();
9594
}
9695
}

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ModelId.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ protected Attributes getOtelAttributes() {
111111
return Attributes.builder()
112112
.put("project", this.getProject())
113113
.put("dataset", this.getDataset())
114-
.put("db.name", this.getDataset())
115114
.put("model", this.getModel())
116115
.build();
117116
}

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryJobConfiguration.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.common.collect.Iterables;
3333
import com.google.common.collect.Lists;
3434
import com.google.common.collect.Maps;
35+
import io.opentelemetry.api.common.Attributes;
3536
import java.util.List;
3637
import java.util.Map;
3738
import java.util.Objects;
@@ -1213,4 +1214,25 @@ public QueryParameter apply(Map.Entry<String, QueryParameterValue> entry) {
12131214
return queryParameterPb;
12141215
}
12151216
};
1217+
1218+
private static String getFieldAsString(Object field) {
1219+
return field == null ? "null" : field.toString();
1220+
}
1221+
1222+
public Attributes getOtelAttributes() {
1223+
return Attributes.builder()
1224+
.put("destinationTable", getFieldAsString(this.getDestinationTable()))
1225+
.put("defaultDataset", getFieldAsString(this.getDefaultDataset()))
1226+
.put("createSession", getFieldAsString(this.createSession()))
1227+
.put("flattenResults", getFieldAsString(this.flattenResults()))
1228+
.put("priority", getFieldAsString(this.getPriority()))
1229+
.put("useQueryCache", getFieldAsString(this.useQueryCache()))
1230+
.put("dryRun", getFieldAsString(this.dryRun()))
1231+
.put("jobTimeoutMs", getFieldAsString(this.getJobTimeoutMs()))
1232+
.put("labels", getFieldAsString(this.getLabels()))
1233+
.put("connectionProperties", getFieldAsString(this.getConnectionProperties()))
1234+
.put("jobCreationMode", getFieldAsString(this.getJobCreationMode()))
1235+
.put("reservation", getFieldAsString(this.getReservation()))
1236+
.build();
1237+
}
12161238
}

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ protected Attributes getOtelAttributes() {
114114
return Attributes.builder()
115115
.put("project", this.getProject())
116116
.put("dataset", this.getDataset())
117-
.put("db.name", this.getDataset())
118117
.put("routine", this.getRoutine())
119118
.build();
120119
}

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableId.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ protected Attributes getOtelAttributes() {
120120
return Attributes.builder()
121121
.put("project", this.getProject())
122122
.put("dataset", this.getDataset())
123-
.put("db.name", this.getDataset())
124123
.put("table", this.getTable())
125124
.build();
126125
}

0 commit comments

Comments
 (0)