diff --git a/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.java b/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.java index bf2567f1e2b2..1a0a2d41631b 100644 --- a/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.java +++ b/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.java @@ -19,6 +19,8 @@ import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.core.InternalApi; +import com.google.api.gax.tracing.ErrorTypeUtil; +import com.google.api.gax.tracing.ObservabilityAttributes; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.StatusCode; @@ -37,27 +39,30 @@ private BigQueryTelemetryTracer() {} // https://github.com/googleapis/google-cloud-java/issues/12099 // Common GCP Attributes public static final AttributeKey GCP_CLIENT_SERVICE = - AttributeKey.stringKey("gcp.client.service"); + AttributeKey.stringKey(ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE); public static final AttributeKey GCP_CLIENT_VERSION = AttributeKey.stringKey("gcp.client.version"); public static final AttributeKey GCP_CLIENT_REPO = - AttributeKey.stringKey("gcp.client.repo"); + AttributeKey.stringKey(ObservabilityAttributes.REPO_ATTRIBUTE); public static final AttributeKey GCP_CLIENT_ARTIFACT = AttributeKey.stringKey("gcp.client.artifact"); public static final AttributeKey GCP_RESOURCE_DESTINATION_ID = - AttributeKey.stringKey("gcp.resource.destination.id"); + AttributeKey.stringKey(ObservabilityAttributes.DESTINATION_RESOURCE_ID_ATTRIBUTE); public static final AttributeKey RPC_SYSTEM_NAME = - AttributeKey.stringKey("rpc.system.name"); + AttributeKey.stringKey(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE); // Common Error Attributes - public static final AttributeKey ERROR_TYPE = AttributeKey.stringKey("error.type"); + public static final AttributeKey ERROR_TYPE = + AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE); public static final AttributeKey EXCEPTION_TYPE = - AttributeKey.stringKey("exception.type"); + AttributeKey.stringKey(ObservabilityAttributes.EXCEPTION_TYPE_ATTRIBUTE); public static final AttributeKey STATUS_MESSAGE = - AttributeKey.stringKey("status.message"); + AttributeKey.stringKey(ObservabilityAttributes.STATUS_MESSAGE_ATTRIBUTE); - public static final AttributeKey URL_TEMPLATE = AttributeKey.stringKey("url.template"); - public static final AttributeKey URL_DOMAIN = AttributeKey.stringKey("url.domain"); + public static final AttributeKey URL_TEMPLATE = + AttributeKey.stringKey(ObservabilityAttributes.URL_TEMPLATE_ATTRIBUTE); + public static final AttributeKey URL_DOMAIN = + AttributeKey.stringKey(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE); public static void addCommonAttributeToSpan(Span span) { span.setAttribute(GCP_CLIENT_SERVICE, BQ_GCP_CLIENT_SERVICE) @@ -77,8 +82,7 @@ public static void addExceptionToSpan(Exception e, Span span) { String simpleName = e.getClass().getSimpleName(); String statusMessage = simpleName + (message != null ? ": " + message : ""); span.setAttribute(BigQueryTelemetryTracer.EXCEPTION_TYPE, e.getClass().getName()); - span.setAttribute( - BigQueryTelemetryTracer.ERROR_TYPE, ErrorTypeUtil.getClientErrorType(e).toString()); + span.setAttribute(BigQueryTelemetryTracer.ERROR_TYPE, ErrorTypeUtil.extractErrorType(e)); span.setAttribute(BigQueryTelemetryTracer.STATUS_MESSAGE, statusMessage); span.setStatus(StatusCode.ERROR, statusMessage); } diff --git a/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtil.java b/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtil.java deleted file mode 100644 index 48cc4b2a3d7e..000000000000 --- a/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtil.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2026 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.cloud.bigquery.telemetry; - -/** - * Utility class for identifying exception types for telemetry tracking. TODO: this class should get - * replaced with gax version when ready work tracked in - * https://github.com/googleapis/google-cloud-java/issues/12105 - */ -class ErrorTypeUtil { - - enum ErrorType { - CLIENT_TIMEOUT, - CLIENT_CONNECTION_ERROR, - CLIENT_REQUEST_ERROR, - CLIENT_RESPONSE_DECODE_ERROR, - CLIENT_UNKNOWN_ERROR; - - @Override - public String toString() { - return name(); - } - } - - static boolean isClientTimeout(Exception e) { - return e instanceof java.net.SocketTimeoutException; - } - - static boolean isClientConnectionError(Exception e) { - return e instanceof java.net.ConnectException - || e instanceof java.net.UnknownHostException - || e instanceof javax.net.ssl.SSLHandshakeException - || e instanceof java.nio.channels.UnresolvedAddressException; - } - - static boolean isClientResponseDecodeError(Exception e) { - return e instanceof com.google.gson.JsonParseException; - } - - static boolean isClientRequestError(Exception e) { - return e instanceof java.lang.IllegalArgumentException; - } - - static ErrorType getClientErrorType(Exception e) { - if (isClientTimeout(e)) { - return ErrorType.CLIENT_TIMEOUT; - } else if (isClientConnectionError(e)) { - return ErrorType.CLIENT_CONNECTION_ERROR; - } else if (isClientResponseDecodeError(e)) { - return ErrorType.CLIENT_RESPONSE_DECODE_ERROR; - } else if (isClientRequestError(e)) { - return ErrorType.CLIENT_REQUEST_ERROR; - } else { - return ErrorType.CLIENT_UNKNOWN_ERROR; - } - } -} diff --git a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITOpenTelemetryTest.java b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITOpenTelemetryTest.java index 34b0ed2cff8d..2b477754b531 100644 --- a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITOpenTelemetryTest.java +++ b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITOpenTelemetryTest.java @@ -99,10 +99,14 @@ public void testListDatasetsTraced() { assertEquals(200L, attrs.get(HttpTracingRequestInitializer.HTTP_RESPONSE_STATUS_CODE)); assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN)); assertEquals( - "https://bigquery.googleapis.com/bigquery/v2/projects/gcloud-devel/datasets?prettyPrint=false", + "https://bigquery.googleapis.com/bigquery/v2/projects/" + + bigqueryHelper.getOptions().getProjectId() + + "/datasets?prettyPrint=false", attrs.get(HttpTracingRequestInitializer.URL_FULL)); assertEquals( - "//bigquery.googleapis.com/projects/gcloud-devel/datasets", + "//bigquery.googleapis.com/projects/" + + bigqueryHelper.getOptions().getProjectId() + + "/datasets", attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID)); assertEquals( "projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE)); @@ -146,19 +150,25 @@ public void testGetDatasetNotFoundTraced() { "projects/{+projectId}/datasets/{+datasetId}", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE)); assertEquals( - "https://bigquery.googleapis.com/bigquery/v2/projects/gcloud-devel/datasets/non_existent_dataset?prettyPrint=false", + "https://bigquery.googleapis.com/bigquery/v2/projects/" + + bigqueryHelper.getOptions().getProjectId() + + "/datasets/non_existent_dataset?prettyPrint=false", attrs.get(HttpTracingRequestInitializer.URL_FULL)); assertEquals( "bigquery.googleapis.com", attrs.get(HttpTracingRequestInitializer.SERVER_ADDRESS)); assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN)); assertEquals( - "//bigquery.googleapis.com/projects/gcloud-devel/datasets/non_existent_dataset", + "//bigquery.googleapis.com/projects/" + + bigqueryHelper.getOptions().getProjectId() + + "/datasets/non_existent_dataset", attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID)); // Error attributes assertEquals("notFound", attrs.get(BigQueryTelemetryTracer.ERROR_TYPE)); assertEquals( - "Not found: Dataset gcloud-devel:non_existent_dataset", + "Not found: Dataset " + + bigqueryHelper.getOptions().getProjectId() + + ":non_existent_dataset", attrs.get(BigQueryTelemetryTracer.STATUS_MESSAGE)); } } @@ -197,7 +207,9 @@ public void testClientErrorAndRetriesTraced() { Map, Object> attrs = span.getAttributes().asMap(); checkGeneralAttributes(attrs); assertEquals( - "https://invalid-host-name-12345.com:8080/bigquery/v2/projects/gcloud-devel/datasets?prettyPrint=false", + "https://invalid-host-name-12345.com:8080/bigquery/v2/projects/" + + bigqueryHelper.getOptions().getProjectId() + + "/datasets?prettyPrint=false", (String) attrs.get(HttpTracingRequestInitializer.URL_FULL)); assertEquals( "invalid-host-name-12345.com", attrs.get(HttpTracingRequestInitializer.SERVER_ADDRESS)); @@ -206,7 +218,9 @@ public void testClientErrorAndRetriesTraced() { assertEquals( "projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE)); assertEquals( - "//bigquery.googleapis.com/projects/gcloud-devel/datasets", + "//bigquery.googleapis.com/projects/" + + bigqueryHelper.getOptions().getProjectId() + + "/datasets", attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID)); checkRetryAttribute(span, rpcSpanCount); diff --git a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java index 33716417ed09..b01515a5639d 100644 --- a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java +++ b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java @@ -1079,8 +1079,7 @@ public void testHttpTracingEnabled_GenericException_SetsAttributes() throws Exce assertEquals( "java.io.IOException", rpcSpan.getAttributes().get(BigQueryTelemetryTracer.EXCEPTION_TYPE)); - assertEquals( - "CLIENT_UNKNOWN_ERROR", rpcSpan.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE)); + assertEquals("IOException", rpcSpan.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE)); } @Test diff --git a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracerTest.java b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracerTest.java index 91ef36fe3110..6ee68930ad87 100644 --- a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracerTest.java +++ b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracerTest.java @@ -143,7 +143,7 @@ public void testAddExceptionToSpan_WithMessage() { assertEquals( "java.lang.Exception", spanData.getAttributes().get(BigQueryTelemetryTracer.EXCEPTION_TYPE)); - assertErrorSpanAttributes("CLIENT_UNKNOWN_ERROR", "Exception: Test error message"); + assertErrorSpanAttributes("Exception", "Exception: Test error message"); } @Test @@ -160,8 +160,7 @@ public void testAddExceptionToSpan_NoMessage() { "java.lang.Exception", spanData.getAttributes().get(BigQueryTelemetryTracer.EXCEPTION_TYPE)); assertEquals("Exception", spanData.getAttributes().get(BigQueryTelemetryTracer.STATUS_MESSAGE)); - assertEquals( - "CLIENT_UNKNOWN_ERROR", spanData.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE)); + assertEquals("Exception", spanData.getAttributes().get(BigQueryTelemetryTracer.ERROR_TYPE)); } @Test diff --git a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtilTest.java b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtilTest.java deleted file mode 100644 index 933790437a4c..000000000000 --- a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/ErrorTypeUtilTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2026 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud.bigquery.telemetry; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import com.google.cloud.bigquery.telemetry.ErrorTypeUtil.ErrorType; -import com.google.gson.JsonParseException; -import java.net.ConnectException; -import java.net.SocketTimeoutException; -import org.junit.jupiter.api.Test; - -public class ErrorTypeUtilTest { - - @Test - public void testGetClientErrorType_Timeout() { - assertEquals( - ErrorType.CLIENT_TIMEOUT, ErrorTypeUtil.getClientErrorType(new SocketTimeoutException())); - } - - @Test - public void testGetClientErrorType_ConnectionError() { - assertEquals( - ErrorType.CLIENT_CONNECTION_ERROR, - ErrorTypeUtil.getClientErrorType(new ConnectException())); - } - - @Test - public void testGetClientErrorType_ResponseDecodeError() { - assertEquals( - ErrorType.CLIENT_RESPONSE_DECODE_ERROR, - ErrorTypeUtil.getClientErrorType(new JsonParseException(""))); - } - - @Test - public void testGetClientErrorType_RequestError() { - assertEquals( - ErrorType.CLIENT_REQUEST_ERROR, - ErrorTypeUtil.getClientErrorType(new IllegalArgumentException())); - } - - @Test - public void testGetClientErrorType_UnknownError() { - assertEquals(ErrorType.CLIENT_UNKNOWN_ERROR, ErrorTypeUtil.getClientErrorType(new Exception())); - } - - @Test - public void testErrorTypeToString() { - assertEquals("CLIENT_TIMEOUT", ErrorType.CLIENT_TIMEOUT.toString()); - assertEquals("CLIENT_CONNECTION_ERROR", ErrorType.CLIENT_CONNECTION_ERROR.toString()); - } -}