Skip to content

Commit dc74796

Browse files
blakeli0suztomo
authored andcommitted
refactor(bigquery): reuse gax ErrorTypeUtil and ObservabilityAttributes in bigquery (googleapis#12909)
This PR removes the duplicated ErrorTypeUtil from java-bigquery and uses the one from gax. It also updates BigQueryTelemetryTracer to use attribute keys from ObservabilityAttributes in gax.
1 parent 55316c0 commit dc74796

6 files changed

Lines changed: 39 additions & 159 deletions

File tree

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.api.client.googleapis.json.GoogleJsonError;
2020
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
2121
import com.google.api.core.InternalApi;
22+
import com.google.api.gax.tracing.ErrorTypeUtil;
23+
import com.google.api.gax.tracing.ObservabilityAttributes;
2224
import io.opentelemetry.api.common.AttributeKey;
2325
import io.opentelemetry.api.trace.Span;
2426
import io.opentelemetry.api.trace.StatusCode;
@@ -37,27 +39,30 @@ private BigQueryTelemetryTracer() {}
3739
// https://github.com/googleapis/google-cloud-java/issues/12099
3840
// Common GCP Attributes
3941
public static final AttributeKey<String> GCP_CLIENT_SERVICE =
40-
AttributeKey.stringKey("gcp.client.service");
42+
AttributeKey.stringKey(ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE);
4143
public static final AttributeKey<String> GCP_CLIENT_VERSION =
4244
AttributeKey.stringKey("gcp.client.version");
4345
public static final AttributeKey<String> GCP_CLIENT_REPO =
44-
AttributeKey.stringKey("gcp.client.repo");
46+
AttributeKey.stringKey(ObservabilityAttributes.REPO_ATTRIBUTE);
4547
public static final AttributeKey<String> GCP_CLIENT_ARTIFACT =
4648
AttributeKey.stringKey("gcp.client.artifact");
4749
public static final AttributeKey<String> GCP_RESOURCE_DESTINATION_ID =
48-
AttributeKey.stringKey("gcp.resource.destination.id");
50+
AttributeKey.stringKey(ObservabilityAttributes.DESTINATION_RESOURCE_ID_ATTRIBUTE);
4951
public static final AttributeKey<String> RPC_SYSTEM_NAME =
50-
AttributeKey.stringKey("rpc.system.name");
52+
AttributeKey.stringKey(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE);
5153

5254
// Common Error Attributes
53-
public static final AttributeKey<String> ERROR_TYPE = AttributeKey.stringKey("error.type");
55+
public static final AttributeKey<String> ERROR_TYPE =
56+
AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE);
5457
public static final AttributeKey<String> EXCEPTION_TYPE =
55-
AttributeKey.stringKey("exception.type");
58+
AttributeKey.stringKey(ObservabilityAttributes.EXCEPTION_TYPE_ATTRIBUTE);
5659
public static final AttributeKey<String> STATUS_MESSAGE =
57-
AttributeKey.stringKey("status.message");
60+
AttributeKey.stringKey(ObservabilityAttributes.STATUS_MESSAGE_ATTRIBUTE);
5861

59-
public static final AttributeKey<String> URL_TEMPLATE = AttributeKey.stringKey("url.template");
60-
public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain");
62+
public static final AttributeKey<String> URL_TEMPLATE =
63+
AttributeKey.stringKey(ObservabilityAttributes.URL_TEMPLATE_ATTRIBUTE);
64+
public static final AttributeKey<String> URL_DOMAIN =
65+
AttributeKey.stringKey(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE);
6166

6267
public static void addCommonAttributeToSpan(Span span) {
6368
span.setAttribute(GCP_CLIENT_SERVICE, BQ_GCP_CLIENT_SERVICE)
@@ -77,8 +82,7 @@ public static void addExceptionToSpan(Exception e, Span span) {
7782
String simpleName = e.getClass().getSimpleName();
7883
String statusMessage = simpleName + (message != null ? ": " + message : "");
7984
span.setAttribute(BigQueryTelemetryTracer.EXCEPTION_TYPE, e.getClass().getName());
80-
span.setAttribute(
81-
BigQueryTelemetryTracer.ERROR_TYPE, ErrorTypeUtil.getClientErrorType(e).toString());
85+
span.setAttribute(BigQueryTelemetryTracer.ERROR_TYPE, ErrorTypeUtil.extractErrorType(e));
8286
span.setAttribute(BigQueryTelemetryTracer.STATUS_MESSAGE, statusMessage);
8387
span.setStatus(StatusCode.ERROR, statusMessage);
8488
}

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtil.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITOpenTelemetryTest.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ public void testListDatasetsTraced() {
9999
assertEquals(200L, attrs.get(HttpTracingRequestInitializer.HTTP_RESPONSE_STATUS_CODE));
100100
assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN));
101101
assertEquals(
102-
"https://bigquery.googleapis.com/bigquery/v2/projects/gcloud-devel/datasets?prettyPrint=false",
102+
"https://bigquery.googleapis.com/bigquery/v2/projects/"
103+
+ bigqueryHelper.getOptions().getProjectId()
104+
+ "/datasets?prettyPrint=false",
103105
attrs.get(HttpTracingRequestInitializer.URL_FULL));
104106
assertEquals(
105-
"//bigquery.googleapis.com/projects/gcloud-devel/datasets",
107+
"//bigquery.googleapis.com/projects/"
108+
+ bigqueryHelper.getOptions().getProjectId()
109+
+ "/datasets",
106110
attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID));
107111
assertEquals(
108112
"projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE));
@@ -146,19 +150,25 @@ public void testGetDatasetNotFoundTraced() {
146150
"projects/{+projectId}/datasets/{+datasetId}",
147151
attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE));
148152
assertEquals(
149-
"https://bigquery.googleapis.com/bigquery/v2/projects/gcloud-devel/datasets/non_existent_dataset?prettyPrint=false",
153+
"https://bigquery.googleapis.com/bigquery/v2/projects/"
154+
+ bigqueryHelper.getOptions().getProjectId()
155+
+ "/datasets/non_existent_dataset?prettyPrint=false",
150156
attrs.get(HttpTracingRequestInitializer.URL_FULL));
151157
assertEquals(
152158
"bigquery.googleapis.com", attrs.get(HttpTracingRequestInitializer.SERVER_ADDRESS));
153159
assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN));
154160
assertEquals(
155-
"//bigquery.googleapis.com/projects/gcloud-devel/datasets/non_existent_dataset",
161+
"//bigquery.googleapis.com/projects/"
162+
+ bigqueryHelper.getOptions().getProjectId()
163+
+ "/datasets/non_existent_dataset",
156164
attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID));
157165

158166
// Error attributes
159167
assertEquals("notFound", attrs.get(BigQueryTelemetryTracer.ERROR_TYPE));
160168
assertEquals(
161-
"Not found: Dataset gcloud-devel:non_existent_dataset",
169+
"Not found: Dataset "
170+
+ bigqueryHelper.getOptions().getProjectId()
171+
+ ":non_existent_dataset",
162172
attrs.get(BigQueryTelemetryTracer.STATUS_MESSAGE));
163173
}
164174
}
@@ -197,7 +207,9 @@ public void testClientErrorAndRetriesTraced() {
197207
Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap();
198208
checkGeneralAttributes(attrs);
199209
assertEquals(
200-
"https://invalid-host-name-12345.com:8080/bigquery/v2/projects/gcloud-devel/datasets?prettyPrint=false",
210+
"https://invalid-host-name-12345.com:8080/bigquery/v2/projects/"
211+
+ bigqueryHelper.getOptions().getProjectId()
212+
+ "/datasets?prettyPrint=false",
201213
(String) attrs.get(HttpTracingRequestInitializer.URL_FULL));
202214
assertEquals(
203215
"invalid-host-name-12345.com", attrs.get(HttpTracingRequestInitializer.SERVER_ADDRESS));
@@ -206,7 +218,9 @@ public void testClientErrorAndRetriesTraced() {
206218
assertEquals(
207219
"projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE));
208220
assertEquals(
209-
"//bigquery.googleapis.com/projects/gcloud-devel/datasets",
221+
"//bigquery.googleapis.com/projects/"
222+
+ bigqueryHelper.getOptions().getProjectId()
223+
+ "/datasets",
210224
attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID));
211225
checkRetryAttribute(span, rpcSpanCount);
212226

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,7 @@ public void testHttpTracingEnabled_GenericException_SetsAttributes() throws Exce
10791079
assertEquals(
10801080
"java.io.IOException",
10811081
rpcSpan.getAttributes().get(BigQueryTelemetryTracer.EXCEPTION_TYPE));
1082-
assertEquals(
1083-
"CLIENT_UNKNOWN_ERROR", rpcSpan.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE));
1082+
assertEquals("IOException", rpcSpan.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE));
10841083
}
10851084

10861085
@Test

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracerTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void testAddExceptionToSpan_WithMessage() {
143143
assertEquals(
144144
"java.lang.Exception",
145145
spanData.getAttributes().get(BigQueryTelemetryTracer.EXCEPTION_TYPE));
146-
assertErrorSpanAttributes("CLIENT_UNKNOWN_ERROR", "Exception: Test error message");
146+
assertErrorSpanAttributes("Exception", "Exception: Test error message");
147147
}
148148

149149
@Test
@@ -160,8 +160,7 @@ public void testAddExceptionToSpan_NoMessage() {
160160
"java.lang.Exception",
161161
spanData.getAttributes().get(BigQueryTelemetryTracer.EXCEPTION_TYPE));
162162
assertEquals("Exception", spanData.getAttributes().get(BigQueryTelemetryTracer.STATUS_MESSAGE));
163-
assertEquals(
164-
"CLIENT_UNKNOWN_ERROR", spanData.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE));
163+
assertEquals("Exception", spanData.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE));
165164
}
166165

167166
@Test

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtilTest.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)