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

Commit e941daa

Browse files
committed
Add spans to query and listPartitions
1 parent db232a5 commit e941daa

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
@@ -865,6 +865,17 @@ public boolean delete(JobId jobId) {
865865
Strings.isNullOrEmpty(jobId.getProject())
866866
? getOptions().getProjectId()
867867
: jobId.getProject());
868+
Span jobDelete = null;
869+
if (getOptions().isOpenTelemetryTracingEnabled()
870+
&& getOptions().getOpenTelemetryTracer() != null) {
871+
jobDelete =
872+
getOptions()
873+
.getOpenTelemetryTracer()
874+
.spanBuilder("com.google.cloud.bigquery.BigQuery.deleteJob")
875+
.setAttribute("language", "Java")
876+
.setAllAttributes(completeJobId.getOtelAttributes())
877+
.startSpan();
878+
}
868879
try {
869880
return BigQueryRetryHelper.runWithRetries(
870881
new Callable<Boolean>() {
@@ -880,6 +891,10 @@ public Boolean call() throws IOException {
880891
EMPTY_RETRY_CONFIG);
881892
} catch (BigQueryRetryHelperException e) {
882893
throw BigQueryException.translateAndThrow(e);
894+
} finally {
895+
if (jobDelete != null) {
896+
jobDelete.end();
897+
}
883898
}
884899
}
885900

@@ -1381,26 +1396,43 @@ && getOptions().getOpenTelemetryTracer() != null) {
13811396

13821397
@Override
13831398
public List<String> listPartitions(TableId tableId) {
1384-
List<String> partitions = new ArrayList<String>();
1385-
String partitionsTable = tableId.getTable() + "$__PARTITIONS_SUMMARY__";
1386-
TableId metaTableId =
1387-
tableId.getProject() == null
1388-
? TableId.of(tableId.getDataset(), partitionsTable)
1389-
: TableId.of(tableId.getProject(), tableId.getDataset(), partitionsTable);
1390-
Table metaTable = getTable(metaTableId);
1391-
Schema metaSchema = metaTable.getDefinition().getSchema();
1392-
String partition_id = null;
1393-
for (Field field : metaSchema.getFields()) {
1394-
if (field.getName().equals("partition_id")) {
1395-
partition_id = field.getName();
1396-
break;
1397-
}
1399+
Span listPartitions = null;
1400+
if (getOptions().isOpenTelemetryTracingEnabled()
1401+
&& getOptions().getOpenTelemetryTracer() != null) {
1402+
listPartitions =
1403+
getOptions()
1404+
.getOpenTelemetryTracer()
1405+
.spanBuilder("com.google.cloud.bigquery.BigQuery.listPartitions")
1406+
.setAttribute("language", "Java")
1407+
.setAllAttributes(tableId.getOtelAttributes())
1408+
.startSpan();
13981409
}
1399-
TableResult result = metaTable.list(metaSchema);
1400-
for (FieldValueList list : result.iterateAll()) {
1401-
partitions.add(list.get(partition_id).getStringValue());
1410+
try (Scope listPartitionsScope = listPartitions != null ? listPartitions.makeCurrent() : null) {
1411+
List<String> partitions = new ArrayList<String>();
1412+
String partitionsTable = tableId.getTable() + "$__PARTITIONS_SUMMARY__";
1413+
TableId metaTableId =
1414+
tableId.getProject() == null
1415+
? TableId.of(tableId.getDataset(), partitionsTable)
1416+
: TableId.of(tableId.getProject(), tableId.getDataset(), partitionsTable);
1417+
Table metaTable = getTable(metaTableId);
1418+
Schema metaSchema = metaTable.getDefinition().getSchema();
1419+
String partition_id = null;
1420+
for (Field field : metaSchema.getFields()) {
1421+
if (field.getName().equals("partition_id")) {
1422+
partition_id = field.getName();
1423+
break;
1424+
}
1425+
}
1426+
TableResult result = metaTable.list(metaSchema);
1427+
for (FieldValueList list : result.iterateAll()) {
1428+
partitions.add(list.get(partition_id).getStringValue());
1429+
}
1430+
return partitions;
1431+
} finally {
1432+
if (listPartitions != null) {
1433+
listPartitions.end();
1434+
}
14021435
}
1403-
return partitions;
14041436
}
14051437

14061438
private static Page<Table> listTables(
@@ -1883,20 +1915,38 @@ public TableResult query(QueryJobConfiguration configuration, JobOption... optio
18831915
.setJobCreationMode(getOptions().getDefaultJobCreationMode())
18841916
.build();
18851917

1886-
// If all parameters passed in configuration are supported by the query() method on the backend,
1887-
// put on fast path
1888-
QueryRequestInfo requestInfo =
1889-
new QueryRequestInfo(configuration, getOptions().getUseInt64Timestamps());
1890-
if (requestInfo.isFastQuerySupported(null)) {
1891-
String projectId = getOptions().getProjectId();
1892-
QueryRequest content = requestInfo.toPb();
1893-
if (getOptions().getLocation() != null) {
1894-
content.setLocation(getOptions().getLocation());
1918+
Span querySpan = null;
1919+
if (getOptions().isOpenTelemetryTracingEnabled()
1920+
&& getOptions().getOpenTelemetryTracer() != null) {
1921+
querySpan =
1922+
getOptions()
1923+
.getOpenTelemetryTracer()
1924+
.spanBuilder("com.google.cloud.bigquery.BigQuery.query")
1925+
.setAllAttributes(configuration.getOtelAttributes())
1926+
.setAllAttributes(otelAttributesFromOptions(options))
1927+
.startSpan();
1928+
}
1929+
try (Scope queryScope = querySpan != null ? querySpan.makeCurrent() : null) {
1930+
// If all parameters passed in configuration are supported by the query() method on the
1931+
// backend,
1932+
// put on fast path
1933+
QueryRequestInfo requestInfo =
1934+
new QueryRequestInfo(configuration, getOptions().getUseInt64Timestamps());
1935+
if (requestInfo.isFastQuerySupported(null)) {
1936+
String projectId = getOptions().getProjectId();
1937+
QueryRequest content = requestInfo.toPb();
1938+
if (getOptions().getLocation() != null) {
1939+
content.setLocation(getOptions().getLocation());
1940+
}
1941+
return queryRpc(projectId, content, options);
1942+
}
1943+
// Otherwise, fall back to the existing create query job logic
1944+
return create(JobInfo.of(configuration), options).getQueryResults();
1945+
} finally {
1946+
if (querySpan != null) {
1947+
querySpan.end();
18951948
}
1896-
return queryRpc(projectId, content, options);
18971949
}
1898-
// Otherwise, fall back to the existing create query job logic
1899-
return create(JobInfo.of(configuration), options).getQueryResults();
19001950
}
19011951

19021952
private TableResult queryRpc(
@@ -2005,31 +2055,51 @@ public TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOp
20052055
throws InterruptedException, JobException {
20062056
Job.checkNotDryRun(configuration, "query");
20072057

2008-
// If all parameters passed in configuration are supported by the query() method on the backend,
2009-
// put on fast path
2010-
QueryRequestInfo requestInfo =
2011-
new QueryRequestInfo(configuration, getOptions().getUseInt64Timestamps());
2012-
if (requestInfo.isFastQuerySupported(jobId)) {
2013-
// Be careful when setting the projectID in JobId, if a projectID is specified in the JobId,
2014-
// the job created by the query method will use that project. This may cause the query to
2015-
// fail with "Access denied" if the project do not have enough permissions to run the job.
2016-
2017-
String projectId =
2018-
jobId.getProject() != null ? jobId.getProject() : getOptions().getProjectId();
2019-
QueryRequest content = requestInfo.toPb();
2020-
// Be careful when setting the location, if a location is specified in the BigQueryOption or
2021-
// JobId the job created by the query method will be in that location, even if the table to be
2022-
// queried is in a different location. This may cause the query to fail with
2023-
// "BigQueryException: Not found"
2024-
if (jobId.getLocation() != null) {
2025-
content.setLocation(jobId.getLocation());
2026-
} else if (getOptions().getLocation() != null) {
2027-
content.setLocation(getOptions().getLocation());
2028-
}
2058+
Span querySpan = null;
2059+
if (getOptions().isOpenTelemetryTracingEnabled()
2060+
&& getOptions().getOpenTelemetryTracer() != null) {
2061+
querySpan =
2062+
getOptions()
2063+
.getOpenTelemetryTracer()
2064+
.spanBuilder("com.google.cloud.bigquery.BigQuery.query")
2065+
.setAllAttributes(configuration.getOtelAttributes())
2066+
.setAllAttributes(jobId.getOtelAttributes())
2067+
.setAllAttributes(otelAttributesFromOptions(options))
2068+
.startSpan();
2069+
}
2070+
try (Scope queryScope = querySpan != null ? querySpan.makeCurrent() : null) {
2071+
// If all parameters passed in configuration are supported by the query() method on the
2072+
// backend,
2073+
// put on fast path
2074+
QueryRequestInfo requestInfo =
2075+
new QueryRequestInfo(configuration, getOptions().getUseInt64Timestamps());
2076+
if (requestInfo.isFastQuerySupported(jobId)) {
2077+
// Be careful when setting the projectID in JobId, if a projectID is specified in the JobId,
2078+
// the job created by the query method will use that project. This may cause the query to
2079+
// fail with "Access denied" if the project do not have enough permissions to run the job.
2080+
2081+
String projectId =
2082+
jobId.getProject() != null ? jobId.getProject() : getOptions().getProjectId();
2083+
QueryRequest content = requestInfo.toPb();
2084+
// Be careful when setting the location, if a location is specified in the BigQueryOption or
2085+
// JobId the job created by the query method will be in that location, even if the table to
2086+
// be
2087+
// queried is in a different location. This may cause the query to fail with
2088+
// "BigQueryException: Not found"
2089+
if (jobId.getLocation() != null) {
2090+
content.setLocation(jobId.getLocation());
2091+
} else if (getOptions().getLocation() != null) {
2092+
content.setLocation(getOptions().getLocation());
2093+
}
20292094

2030-
return queryRpc(projectId, content, options);
2095+
return queryRpc(projectId, content, options);
2096+
}
2097+
return create(JobInfo.of(jobId, configuration), options).getQueryResults();
2098+
} finally {
2099+
if (querySpan != null) {
2100+
querySpan.end();
2101+
}
20312102
}
2032-
return create(JobInfo.of(jobId, configuration), options).getQueryResults();
20332103
}
20342104

20352105
@Override
@@ -2299,8 +2369,6 @@ private static Attributes otelAttributesFromQueryRequest(QueryRequest request) {
22992369
.put("location", getFieldAsString(request.getLocation()))
23002370
.put("maxResults", getFieldAsString(request.getMaxResults()))
23012371
.put("maximumBytesBilled", getFieldAsString(request.getMaximumBytesBilled()))
2302-
.put("query", getFieldAsString(request.getQuery()))
2303-
.put("queryParameters", getFieldAsString(request.getQueryParameters()))
23042372
.put("requestId", getFieldAsString(request.getRequestId()))
23052373
.put("reservation", getFieldAsString(request.getReservation()))
23062374
.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
@@ -207,7 +207,7 @@ public JobCreationMode getDefaultJobCreationMode() {
207207
*
208208
* @return true if tracing is enabled, false if not
209209
*/
210-
@BetaApi
210+
@BetaApi("Span names and attributes are subject to change without notice")
211211
public boolean isOpenTelemetryTracingEnabled() {
212212
return enableOpenTelemetryTracing;
213213
}
@@ -217,7 +217,7 @@ public boolean isOpenTelemetryTracingEnabled() {
217217
*
218218
* @return OpenTelemetry tracer object or {@code null} if not set
219219
*/
220-
@BetaApi
220+
@BetaApi("Span names and attributes are subject to change without notice")
221221
public Tracer getOpenTelemetryTracer() {
222222
return openTelemetryTracer;
223223
}

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)