Skip to content

Commit 6e3e07a

Browse files
authored
feat(bigquery): Integrate Otel in client lib (#3747)
* feat(bigquery): Integrate Otel in client lib * Refactor Otel code into separate file * Add copyright comment * fix style * remove unused dependencies * remove unused test dependencies * fix dependency issue * fix it test * refactor out OtelHelper class * change test name to start with lowercase * change datasetId key to dataset * remove OtelHelper class * Clean up attribute values * Add spans to query and listPartitions * remove extraneous attributes * add attribute namespaces, change to snake_case * Resolve merge conflict * change previewEnable to JobCreationMode * fix style
1 parent c5a5c2a commit 6e3e07a

File tree

15 files changed

+1225
-194
lines changed

15 files changed

+1225
-194
lines changed

google-cloud-bigquery/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@
156156
<artifactId>error_prone_annotations</artifactId>
157157
</dependency>
158158

159+
<!-- OpenTelemetry -->
160+
<dependency>
161+
<groupId>io.opentelemetry</groupId>
162+
<artifactId>opentelemetry-api</artifactId>
163+
</dependency>
164+
<dependency>
165+
<groupId>io.opentelemetry</groupId>
166+
<artifactId>opentelemetry-context</artifactId>
167+
</dependency>
168+
159169
<!-- Test dependencies -->
160170
<dependency>
161171
<groupId>com.google.api</groupId>
@@ -208,6 +218,23 @@
208218
<artifactId>proto-google-cloud-datacatalog-v1</artifactId>
209219
<scope>test</scope>
210220
</dependency>
221+
222+
<!-- OpenTelemetry -->
223+
<dependency>
224+
<groupId>io.opentelemetry</groupId>
225+
<artifactId>opentelemetry-sdk</artifactId>
226+
<scope>test</scope>
227+
</dependency>
228+
<dependency>
229+
<groupId>io.opentelemetry</groupId>
230+
<artifactId>opentelemetry-sdk-common</artifactId>
231+
<scope>test</scope>
232+
</dependency>
233+
<dependency>
234+
<groupId>io.opentelemetry</groupId>
235+
<artifactId>opentelemetry-sdk-trace</artifactId>
236+
<scope>test</scope>
237+
</dependency>
211238
</dependencies>
212239

213240
<build>

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

Lines changed: 818 additions & 194 deletions
Large diffs are not rendered by default.

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.bigquery;
1818

19+
import com.google.api.core.BetaApi;
1920
import com.google.cloud.ServiceDefaults;
2021
import com.google.cloud.ServiceOptions;
2122
import com.google.cloud.ServiceRpc;
@@ -25,6 +26,7 @@
2526
import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc;
2627
import com.google.cloud.http.HttpTransportOptions;
2728
import com.google.common.collect.ImmutableSet;
29+
import io.opentelemetry.api.trace.Tracer;
2830
import java.util.Set;
2931

3032
public class BigQueryOptions extends ServiceOptions<BigQuery, BigQueryOptions> {
@@ -39,6 +41,8 @@ public class BigQueryOptions extends ServiceOptions<BigQuery, BigQueryOptions> {
3941
private boolean setThrowNotFound;
4042
private boolean useInt64Timestamps;
4143
private JobCreationMode defaultJobCreationMode = JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED;
44+
private boolean enableOpenTelemetryTracing;
45+
private Tracer openTelemetryTracer;
4246

4347
public static class DefaultBigQueryFactory implements BigQueryFactory {
4448

@@ -64,6 +68,8 @@ public static class Builder extends ServiceOptions.Builder<BigQuery, BigQueryOpt
6468

6569
private String location;
6670
private boolean useInt64Timestamps;
71+
private boolean enableOpenTelemetryTracing;
72+
private Tracer openTelemetryTracer;
6773

6874
private Builder() {}
6975

@@ -90,6 +96,28 @@ public Builder setUseInt64Timestamps(boolean useInt64Timestamps) {
9096
return this;
9197
}
9298

99+
/**
100+
* Enables OpenTelemetry tracing functionality for this BigQuery instance
101+
*
102+
* @param enableOpenTelemetryTracing enables OpenTelemetry tracing if true
103+
*/
104+
@BetaApi
105+
public Builder setEnableOpenTelemetryTracing(boolean enableOpenTelemetryTracing) {
106+
this.enableOpenTelemetryTracing = enableOpenTelemetryTracing;
107+
return this;
108+
}
109+
110+
/**
111+
* Sets the OpenTelemetry tracer for this BigQuery instance to be tracer.
112+
*
113+
* @param tracer OpenTelemetry tracer to be used
114+
*/
115+
@BetaApi
116+
public Builder setOpenTelemetryTracer(Tracer tracer) {
117+
this.openTelemetryTracer = tracer;
118+
return this;
119+
}
120+
93121
@Override
94122
public BigQueryOptions build() {
95123
return new BigQueryOptions(this);
@@ -100,6 +128,8 @@ private BigQueryOptions(Builder builder) {
100128
super(BigQueryFactory.class, BigQueryRpcFactory.class, builder, new BigQueryDefaults());
101129
this.location = builder.location;
102130
this.useInt64Timestamps = builder.useInt64Timestamps;
131+
this.enableOpenTelemetryTracing = builder.enableOpenTelemetryTracing;
132+
this.openTelemetryTracer = builder.openTelemetryTracer;
103133
}
104134

105135
private static class BigQueryDefaults implements ServiceDefaults<BigQuery, BigQueryOptions> {
@@ -171,6 +201,26 @@ public JobCreationMode getDefaultJobCreationMode() {
171201
return defaultJobCreationMode;
172202
}
173203

204+
/**
205+
* Returns whether this BigQuery instance has OpenTelemetry tracing enabled
206+
*
207+
* @return true if tracing is enabled, false if not
208+
*/
209+
@BetaApi("Span names and attributes are subject to change without notice")
210+
public boolean isOpenTelemetryTracingEnabled() {
211+
return enableOpenTelemetryTracing;
212+
}
213+
214+
/**
215+
* Returns the OpenTelemetry tracer used by this BigQuery instance
216+
*
217+
* @return OpenTelemetry tracer object or {@code null} if not set
218+
*/
219+
@BetaApi("Span names and attributes are subject to change without notice")
220+
public Tracer getOpenTelemetryTracer() {
221+
return openTelemetryTracer;
222+
}
223+
174224
@SuppressWarnings("unchecked")
175225
@Override
176226
public Builder toBuilder() {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.base.Strings.isNullOrEmpty;
2121

2222
import com.google.api.services.bigquery.model.DatasetReference;
23+
import io.opentelemetry.api.common.Attributes;
2324
import java.io.Serializable;
2425
import java.util.Objects;
2526

@@ -84,4 +85,11 @@ DatasetReference toPb() {
8485
static DatasetId fromPb(DatasetReference datasetRef) {
8586
return new DatasetId(datasetRef.getProjectId(), datasetRef.getDatasetId());
8687
}
88+
89+
protected Attributes getOtelAttributes() {
90+
return Attributes.builder()
91+
.put("bq.dataset.project", this.getProject())
92+
.put("bq.dataset.id", this.getDataset())
93+
.build();
94+
}
8795
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.common.base.MoreObjects;
2828
import com.google.common.collect.ImmutableList;
2929
import com.google.common.collect.Lists;
30+
import io.opentelemetry.api.common.Attributes;
3031
import java.io.Serializable;
3132
import java.util.List;
3233
import java.util.Map;
@@ -746,6 +747,18 @@ public static DatasetInfo of(String datasetId) {
746747
return newBuilder(datasetId).build();
747748
}
748749

750+
private static String getFieldAsString(Object field) {
751+
return field == null ? "null" : field.toString();
752+
}
753+
754+
protected Attributes getOtelAttributes() {
755+
return Attributes.builder()
756+
.putAll(this.getDatasetId().getOtelAttributes())
757+
.put("bq.dataset.last_modified", getFieldAsString(this.getLastModified()))
758+
.put("bq.dataset.location", getFieldAsString(this.getLocation()))
759+
.build();
760+
}
761+
749762
static DatasetInfo fromPb(Dataset datasetPb) {
750763
return new BuilderImpl(datasetPb).build();
751764
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.ImmutableList;
2323
import com.google.common.collect.ImmutableMap;
2424
import com.google.common.collect.Lists;
25+
import io.opentelemetry.api.common.Attributes;
2526
import java.io.Serializable;
2627
import java.util.Collections;
2728
import java.util.HashMap;
@@ -479,4 +480,15 @@ public boolean equals(Object obj) {
479480
&& Objects.equals(skipInvalidRows, other.skipInvalidRows)
480481
&& Objects.equals(templateSuffix, other.templateSuffix);
481482
}
483+
484+
private static String getFieldAsString(Object field) {
485+
return field == null ? "null" : field.toString();
486+
}
487+
488+
public Attributes getOtelAttributes() {
489+
return Attributes.builder()
490+
.put("bq.insert_all.table", getFieldAsString(this.getTable().getTable()))
491+
.put("bq.insert_all.template_suffix", getFieldAsString(this.getTemplateSuffix()))
492+
.build();
493+
}
482494
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.api.services.bigquery.model.JobReference;
2323
import com.google.auto.value.AutoValue;
24+
import io.opentelemetry.api.common.Attributes;
2425
import java.io.Serializable;
2526
import java.util.UUID;
2627
import javax.annotation.Nullable;
@@ -123,4 +124,16 @@ static JobId fromPb(JobReference jobRef) {
123124
.setLocation(jobRef.getLocation())
124125
.build();
125126
}
127+
128+
private static String getFieldAsString(Object field) {
129+
return field == null ? "null" : field.toString();
130+
}
131+
132+
protected Attributes getOtelAttributes() {
133+
return Attributes.builder()
134+
.put("bq.job.id", getFieldAsString(this.getJob()))
135+
.put("bq.job.location", getFieldAsString(this.getLocation()))
136+
.put("bq.job.project", getFieldAsString(this.getProject()))
137+
.build();
138+
}
126139
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.api.services.bigquery.model.ModelReference;
2323
import com.google.common.base.Function;
24+
import io.opentelemetry.api.common.Attributes;
2425
import java.io.Serializable;
2526
import java.util.Objects;
2627

@@ -105,4 +106,12 @@ ModelReference toPb() {
105106
static ModelId fromPb(ModelReference modelRef) {
106107
return new ModelId(modelRef.getProjectId(), modelRef.getDatasetId(), modelRef.getModelId());
107108
}
109+
110+
protected Attributes getOtelAttributes() {
111+
return Attributes.builder()
112+
.put("bq.model.project", this.getProject())
113+
.put("bq.model.dataset", this.getDataset())
114+
.put("bq.model.id", this.getModel())
115+
.build();
116+
}
108117
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.common.base.Strings;
2727
import com.google.common.collect.ImmutableList;
2828
import com.google.common.collect.Lists;
29+
import io.opentelemetry.api.common.Attributes;
2930
import java.io.Serializable;
3031
import java.util.Collections;
3132
import java.util.List;
@@ -453,4 +454,19 @@ Model toPb() {
453454
static ModelInfo fromPb(Model modelPb) {
454455
return new BuilderImpl(modelPb).build();
455456
}
457+
458+
private static String getFieldAsString(Object field) {
459+
return field == null ? "null" : field.toString();
460+
}
461+
462+
protected Attributes getOtelAttributes() {
463+
return Attributes.builder()
464+
.putAll(this.getModelId().getOtelAttributes())
465+
.put("bq.model.type", getFieldAsString(this.getModelType()))
466+
.put("bq.model.creation_time", getFieldAsString(this.getCreationTime()))
467+
.put("bq.model.last_modified_time", getFieldAsString(this.getLastModifiedTime()))
468+
.put("bq.model.expiration_time", getFieldAsString(this.getExpirationTime()))
469+
.put("bq.model.location", getFieldAsString(this.getLocation()))
470+
.build();
471+
}
456472
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.api.services.bigquery.model.RoutineReference;
2323
import com.google.common.base.Function;
24+
import io.opentelemetry.api.common.Attributes;
2425
import java.io.Serializable;
2526
import java.util.Objects;
2627

@@ -108,4 +109,12 @@ static RoutineId fromPb(RoutineReference routineRef) {
108109
return new RoutineId(
109110
routineRef.getProjectId(), routineRef.getDatasetId(), routineRef.getRoutineId());
110111
}
112+
113+
protected Attributes getOtelAttributes() {
114+
return Attributes.builder()
115+
.put("bq.routine.project", this.getProject())
116+
.put("bq.routine.dataset", this.getDataset())
117+
.put("bq.routine.id", this.getRoutine())
118+
.build();
119+
}
111120
}

0 commit comments

Comments
 (0)