-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bigquery): add resend attribute to span tracing + integration tests #12313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ldetmer
wants to merge
4
commits into
main
Choose a base branch
from
resend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e124bfb
chore(bigquery): add resend attribute + integration tests
ldetmer 4399a85
remove redundant null check
ldetmer a7fded2
use ThreadLocal so that metrics can re-use this solution if span is t…
ldetmer 395263f
Revert "use ThreadLocal so that metrics can re-use this solution if s…
ldetmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
286 changes: 286 additions & 0 deletions
286
...google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITOpenTelemetryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| /* | ||
| * 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.it; | ||
|
|
||
| import static com.google.cloud.bigquery.telemetry.ErrorTypeUtil.ErrorType; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import com.google.api.gax.retrying.RetrySettings; | ||
| import com.google.cloud.bigquery.BigQuery; | ||
| import com.google.cloud.bigquery.BigQueryException; | ||
| import com.google.cloud.bigquery.telemetry.BigQueryTelemetryTracer; | ||
| import com.google.cloud.bigquery.telemetry.HttpTracingRequestInitializer; | ||
| import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ITOpenTelemetryTest { | ||
|
|
||
| private static RemoteBigQueryHelper bigqueryHelper; | ||
| private InMemorySpanExporter memoryExporter; | ||
| private Tracer tracer; | ||
|
|
||
| @BeforeAll | ||
| public static void setUpClass() throws IOException { | ||
| System.setProperty("com.google.cloud.bigquery.http.tracing.dev.enabled", "true"); | ||
| bigqueryHelper = RemoteBigQueryHelper.create(); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| memoryExporter = InMemorySpanExporter.create(); | ||
| SdkTracerProvider tracerProvider = | ||
| SdkTracerProvider.builder() | ||
| .addSpanProcessor(SimpleSpanProcessor.create(memoryExporter)) | ||
| .build(); | ||
| OpenTelemetry openTelemetry = | ||
| OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build(); | ||
| tracer = openTelemetry.getTracer("it-otel-test"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testListDatasetsTraced() { | ||
| BigQuery bq = | ||
| bigqueryHelper.getOptions().toBuilder() | ||
| .setEnableOpenTelemetryTracing(true) | ||
| .setOpenTelemetryTracer(tracer) | ||
| .build() | ||
| .getService(); | ||
|
|
||
| bq.listDatasets(); | ||
|
|
||
| List<SpanData> spans = memoryExporter.getFinishedSpanItems(); | ||
| assertNotNull(spans); | ||
| assertFalse(spans.isEmpty(), "Expected at least one span collected"); | ||
|
|
||
| boolean foundRpcSpan = false; | ||
| for (SpanData span : spans) { | ||
| if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.listDatasets")) { | ||
| foundRpcSpan = true; | ||
| Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap(); | ||
| checkGeneralAttributes(attrs); | ||
| assertEquals("GET", attrs.get(HttpTracingRequestInitializer.HTTP_REQUEST_METHOD)); | ||
| assertEquals("DatasetService", attrs.get(AttributeKey.stringKey("bq.rpc.service"))); | ||
| assertEquals("ListDatasets", attrs.get(AttributeKey.stringKey("bq.rpc.method"))); | ||
| assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.SERVER_ADDRESS)); | ||
| 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", | ||
| attrs.get(HttpTracingRequestInitializer.URL_FULL)); | ||
| assertEquals( | ||
| "//bigquery.googleapis.com/projects/gcloud-devel/datasets", | ||
| attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID)); | ||
| assertEquals( | ||
| "projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE)); | ||
| } | ||
| } | ||
| assertTrue(foundRpcSpan, "Expected to find BigQueryRpc.listDatasets span"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDatasetNotFoundTraced() { | ||
| BigQuery bq = | ||
| bigqueryHelper.getOptions().toBuilder() | ||
| .setEnableOpenTelemetryTracing(true) | ||
| .setOpenTelemetryTracer(tracer) | ||
| .build() | ||
| .getService(); | ||
|
|
||
| bq.getDataset("non_existent_dataset"); | ||
|
|
||
| List<SpanData> spans = memoryExporter.getFinishedSpanItems(); | ||
| assertNotNull(spans); | ||
| assertFalse(spans.isEmpty(), "Expected at least one span collected"); | ||
|
|
||
| boolean foundRpcSpan = false; | ||
| for (SpanData span : spans) { | ||
| if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.getDataset")) { | ||
| foundRpcSpan = true; | ||
| Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap(); | ||
| checkGeneralAttributes(attrs); | ||
| assertEquals("GET", attrs.get(HttpTracingRequestInitializer.HTTP_REQUEST_METHOD)); | ||
| assertEquals("DatasetService", attrs.get(AttributeKey.stringKey("bq.rpc.service"))); | ||
| assertEquals("GetDataset", attrs.get(AttributeKey.stringKey("bq.rpc.method"))); | ||
| assertEquals(404L, attrs.get(HttpTracingRequestInitializer.HTTP_RESPONSE_STATUS_CODE)); | ||
| assertEquals( | ||
| "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", | ||
| attrs.get(HttpTracingRequestInitializer.URL_FULL)); | ||
| assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.SERVER_ADDRESS)); | ||
| assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN)); | ||
| assertEquals( | ||
| "//bigquery.googleapis.com/projects/gcloud-devel/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", | ||
| attrs.get(BigQueryTelemetryTracer.STATUS_MESSAGE)); | ||
| } | ||
| } | ||
| assertTrue(foundRpcSpan, "Expected to find BigQueryRpc.getDataset span"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testConnectionErrorRetriesTraced() { | ||
| // Pass invalid host to force connection error and retries | ||
| BigQuery bq = | ||
| bigqueryHelper.getOptions().toBuilder() | ||
| .setRetrySettings(RetrySettings.newBuilder().setMaxAttempts(5).build()) | ||
| .setEnableOpenTelemetryTracing(true) | ||
| .setOpenTelemetryTracer(tracer) | ||
| .setHost("https://invalid-host-name-12345.com:8080") | ||
| .build() | ||
| .getService(); | ||
|
|
||
| try { | ||
| bq.listDatasets(); | ||
| fail("Expected BigQueryException due to invalid host"); | ||
| } catch (BigQueryException e) { | ||
| // Expected | ||
| } | ||
|
|
||
| List<SpanData> spans = memoryExporter.getFinishedSpanItems(); | ||
| assertNotNull(spans); | ||
| assertFalse(spans.isEmpty(), "Expected at least one span collected"); | ||
|
|
||
| int rpcSpanCount = 0; | ||
| for (SpanData span : spans) { | ||
| if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.listDatasets")) { | ||
| rpcSpanCount++; | ||
| Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap(); | ||
| checkGeneralAttributes(attrs); | ||
| assertEquals( | ||
| "https://invalid-host-name-12345.com:8080/bigquery/v2/projects/gcloud-devel/datasets?prettyPrint=false", | ||
| (String) attrs.get(HttpTracingRequestInitializer.URL_FULL)); | ||
| assertEquals( | ||
| "invalid-host-name-12345.com", attrs.get(BigQueryTelemetryTracer.SERVER_ADDRESS)); | ||
| assertEquals(8080L, attrs.get(BigQueryTelemetryTracer.SERVER_PORT)); | ||
| assertEquals("invalid-host-name-12345.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN)); | ||
| assertEquals( | ||
| "projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE)); | ||
| assertEquals( | ||
| "//bigquery.googleapis.com/projects/gcloud-devel/datasets", | ||
| attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID)); | ||
| checkRetryAttribute(span, rpcSpanCount); | ||
|
|
||
| // Error attributes | ||
| assertEquals( | ||
| "java.net.UnknownHostException", attrs.get(BigQueryTelemetryTracer.EXCEPTION_TYPE)); | ||
| assertEquals( | ||
| ErrorType.CLIENT_CONNECTION_ERROR.toString(), | ||
| attrs.get(BigQueryTelemetryTracer.ERROR_TYPE)); | ||
| assertEquals( | ||
| "UnknownHostException: invalid-host-name-12345.com", | ||
| attrs.get(BigQueryTelemetryTracer.STATUS_MESSAGE)); | ||
| } | ||
| } | ||
| assertEquals(5, rpcSpanCount, "Expected 5 attempts total"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimultaneousCallsDoNotAffectResendCountForEachother() { | ||
| BigQuery bq = | ||
| bigqueryHelper.getOptions().toBuilder() | ||
| .setRetrySettings(RetrySettings.newBuilder().setMaxAttempts(5).build()) | ||
| .setEnableOpenTelemetryTracing(true) | ||
| .setOpenTelemetryTracer(tracer) | ||
| .setHost("https://invalid-host-name-123456.com") | ||
| .build() | ||
| .getService(); | ||
|
|
||
| try { | ||
| bq.listDatasets(); | ||
| fail("Expected BigQueryException due to invalid host"); | ||
| } catch (BigQueryException e) { | ||
| // Expected | ||
| } | ||
| try { | ||
| bq.cancel("test-job-id"); | ||
| fail("Expected BigQueryException due to invalid host"); | ||
| } catch (BigQueryException e) { | ||
| // Expected | ||
| } | ||
|
|
||
| List<SpanData> spans = memoryExporter.getFinishedSpanItems(); | ||
| assertNotNull(spans); | ||
| assertFalse(spans.isEmpty(), "Expected at least one span collected"); | ||
|
|
||
| int listDataSpanCount = 0; | ||
| int cancelJobSpanCount = 0; | ||
| for (SpanData span : spans) { | ||
| if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.listDatasets")) { | ||
| listDataSpanCount++; | ||
| checkRetryAttribute(span, listDataSpanCount); | ||
| } else if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.cancelJob")) { | ||
| cancelJobSpanCount++; | ||
| checkRetryAttribute(span, cancelJobSpanCount); | ||
| } | ||
| } | ||
| assertEquals(5, listDataSpanCount, "Expected 5 attempts total for listDatasets call"); | ||
| assertEquals(5, cancelJobSpanCount, "Expected 5 attempts total for cancelJob call"); | ||
| } | ||
|
|
||
| private static void checkRetryAttribute(SpanData span, int listDataSpanCount) { | ||
| Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap(); | ||
| Long resendCount = (Long) attrs.get(HttpTracingRequestInitializer.HTTP_REQUEST_RESEND_COUNT); | ||
| if (listDataSpanCount == 1) { | ||
| assertTrue(resendCount == null || resendCount == 0); | ||
| } else { | ||
| assertNotNull(resendCount, "Expected resend count for retry attempt " + listDataSpanCount); | ||
| assertEquals((long) (listDataSpanCount - 1), resendCount.longValue()); | ||
| } | ||
| } | ||
|
|
||
| private void checkGeneralAttributes(Map<AttributeKey<?>, Object> attrs) { | ||
| assertEquals( | ||
| HttpTracingRequestInitializer.HTTP_RPC_SYSTEM_NAME, | ||
| attrs.get(BigQueryTelemetryTracer.RPC_SYSTEM_NAME)); | ||
| assertEquals( | ||
| BigQueryTelemetryTracer.BQ_GCP_CLIENT_SERVICE, | ||
| attrs.get(BigQueryTelemetryTracer.GCP_CLIENT_SERVICE)); | ||
| assertEquals( | ||
| BigQueryTelemetryTracer.BQ_GCP_CLIENT_REPO, | ||
| attrs.get(BigQueryTelemetryTracer.GCP_CLIENT_REPO)); | ||
| assertEquals( | ||
| BigQueryTelemetryTracer.BQ_GCP_CLIENT_LANGUAGE, | ||
| attrs.get(BigQueryTelemetryTracer.GCP_CLIENT_LANGUAGE)); | ||
| assertEquals( | ||
| BigQueryTelemetryTracer.BQ_GCP_CLIENT_ARTIFACT, | ||
| attrs.get(BigQueryTelemetryTracer.GCP_CLIENT_ARTIFACT)); | ||
| assertNotNull(attrs.get(BigQueryTelemetryTracer.GCP_CLIENT_VERSION)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic looks good to me. However, is there a way to implement it without using OpenTelemetry Context so it can be reused for other things?
For example, metrics may also need this attribute in the future. We can call
Context.current()as well if traces are enabled, but it is also possible that customers only enable metrics but not traces.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have 3 options for how to implement this:
Add a retry counter object per API request. This would require modifying all API calls, as currently retries are handled via static method call toBigQueryRetryHelper.runWithRetries(. This is not scalable and I would prefer not to implement this solution.
The solution proposed in this PR: use the OpenTelemetry Context (which uses its own ThreadLocal object for storage)
create our own ThreadLocal object in BigQueryRetryHelper that stores this value that can be also accessed by metrics.
Let me know if you are okay with implementing choice no. 3
cc @lqiu96 , in case you have some input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too familiar with Otel's Context so I wouldn't know when/ when not to use it. If there are concerns with it, then we don't have to go this route.
Option 3 isn't my favorite (using static methods and thread locals to track retries), but to Blake's point, it can be re-used for both metrics and traces (assuming that Context is a trace specific thing).
Since metrics are in scope, a route forward can be to use Option 3 and then migrate to Option 1 in the future. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I implemented option 3, I think we should consider thoughtfully before moving to option 1. It adds repetitive code to each client call and also adds another configuration point for metrics which can easily be missed when implementing new api calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted back to option 2 as we are not comfortable with potential leakage using ThreadLocal. Looking for @westarle if he thinks its worth it to move forward with this implementation, or if we should skip for this initial launch?