-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add initial span tracing to http calls #12089
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe373da
feat: add initial span tracing to http calls
ldetmer 695649d
formatted
ldetmer 207ca76
naming/comments/extra white space clean up
ldetmer 8977110
fix port type to int
ldetmer cc08308
added issue tracking and updated strings to use hardcoded vars
ldetmer 1456fb7
fixed test to use correct attribute
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
73 changes: 73 additions & 0 deletions
73
...d-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.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,73 @@ | ||
| /* | ||
| * 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 com.google.api.core.BetaApi; | ||
| import com.google.api.core.InternalApi; | ||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.trace.Span; | ||
|
|
||
| /** BigQuery Telemetry class that stores generic telemetry attributes and values */ | ||
| @BetaApi | ||
| @InternalApi | ||
| public final class BigQueryTelemetryTracer { | ||
|
|
||
| private BigQueryTelemetryTracer() {} | ||
|
|
||
| public static final String BQ_GCP_CLIENT_SERVICE = "bigquery"; | ||
| public static final String BQ_GCP_CLIENT_REPO = "googleapis/java-bigquery"; | ||
| public static final String BQ_GCP_CLIENT_ARTIFACT = "google-cloud-bigquery"; | ||
| public static final String BQ_GCP_CLIENT_LANGUAGE = "java"; | ||
|
|
||
| // TODO: migrate to use gax attributes keys | ||
| // https://github.com/googleapis/google-cloud-java/issues/12099 | ||
| // Common GCP Attributes | ||
| public static final AttributeKey<String> GCP_CLIENT_SERVICE = | ||
| AttributeKey.stringKey("gcp.client.service"); | ||
| public static final AttributeKey<String> GCP_CLIENT_VERSION = | ||
| AttributeKey.stringKey("gcp.client.version"); | ||
| public static final AttributeKey<String> GCP_CLIENT_REPO = | ||
| AttributeKey.stringKey("gcp.client.repo"); | ||
| public static final AttributeKey<String> GCP_CLIENT_ARTIFACT = | ||
| AttributeKey.stringKey("gcp.client.artifact"); | ||
| public static final AttributeKey<String> GCP_CLIENT_LANGUAGE = | ||
| AttributeKey.stringKey("gcp.client.language"); | ||
| public static final AttributeKey<String> GCP_RESOURCE_ID = | ||
| AttributeKey.stringKey("gcp.resource.destination.id"); | ||
| public static final AttributeKey<String> RPC_SYSTEM_NAME = | ||
| AttributeKey.stringKey("rpc.system.name"); | ||
|
|
||
| // Common Error Attributes | ||
| public static final AttributeKey<String> ERROR_TYPE = AttributeKey.stringKey("error.type"); | ||
| public static final AttributeKey<String> EXCEPTION_TYPE = | ||
| AttributeKey.stringKey("exception.type"); | ||
| public static final AttributeKey<String> STATUS_MESSAGE = | ||
| AttributeKey.stringKey("status.message"); | ||
|
|
||
| // Common Server Attributes | ||
| public static final AttributeKey<String> SERVER_ADDRESS = | ||
| AttributeKey.stringKey("server.address"); | ||
| public static final AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port"); | ||
|
|
||
| public static void addCommonAttributeToSpan(Span span) { | ||
| span.setAttribute(GCP_CLIENT_SERVICE, BQ_GCP_CLIENT_SERVICE) | ||
| .setAttribute(GCP_CLIENT_REPO, BQ_GCP_CLIENT_REPO) | ||
| .setAttribute(GCP_CLIENT_ARTIFACT, BQ_GCP_CLIENT_ARTIFACT) | ||
| .setAttribute(GCP_CLIENT_LANGUAGE, BQ_GCP_CLIENT_LANGUAGE); | ||
| // TODO: add version | ||
| } | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
...uery/src/main/java/com/google/cloud/bigquery/telemetry/HttpTracingRequestInitializer.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,92 @@ | ||
| /* | ||
| * 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 com.google.api.client.http.*; | ||
| import com.google.api.core.BetaApi; | ||
| import com.google.api.core.InternalApi; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * HttpRequestInitializer that wraps a delegate initializer, intercepts all HTTP requests, adds | ||
| * OpenTelemetry tracing and then invokes delegate interceptor. | ||
| */ | ||
| @BetaApi | ||
| @InternalApi | ||
| public class HttpTracingRequestInitializer implements HttpRequestInitializer { | ||
|
|
||
| // TODO: migrate to use gax attributes keys | ||
| // https://github.com/googleapis/google-cloud-java/issues/12099 | ||
| // HTTP Specific Telemetry Attributes | ||
| public static final AttributeKey<String> HTTP_REQUEST_METHOD = | ||
| AttributeKey.stringKey("http.request.method"); | ||
| public static final AttributeKey<String> URL_FULL = AttributeKey.stringKey("url.full"); | ||
| public static final AttributeKey<String> URL_TEMPLATE = AttributeKey.stringKey("url.template"); | ||
| public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain"); | ||
| public static final AttributeKey<Long> HTTP_RESPONSE_STATUS_CODE = | ||
| AttributeKey.longKey("http.response.status_code"); | ||
| public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT = | ||
| AttributeKey.longKey("http.request.resend_count"); | ||
| public static final AttributeKey<Long> HTTP_REQUEST_BODY_SIZE = | ||
| AttributeKey.longKey("http.request.body.size"); | ||
| public static final AttributeKey<Long> HTTP_RESPONSE_BODY_SIZE = | ||
| AttributeKey.longKey("http.response.body.size"); | ||
|
|
||
| @VisibleForTesting static final String HTTP_RPC_SYSTEM_NAME = "http"; | ||
|
|
||
| private final HttpRequestInitializer delegate; | ||
| private final Tracer tracer; | ||
|
|
||
| public HttpTracingRequestInitializer(HttpRequestInitializer delegate, Tracer tracer) { | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.delegate = delegate; | ||
| this.tracer = tracer; | ||
| } | ||
|
|
||
| @Override | ||
| public void initialize(HttpRequest request) throws IOException { | ||
| if (delegate != null) { | ||
| delegate.initialize(request); | ||
| } | ||
| if (tracer == null) { | ||
| return; | ||
| } | ||
| // Get the current active span (created by HttpBigQueryRpc) and add HTTP attributes to it | ||
| Span span = Span.current(); | ||
| if (!span.getSpanContext().isValid()) { | ||
| // No active span to exists, skip instrumentation | ||
| return; | ||
| } | ||
| String host = request.getUrl().getHost(); | ||
| int port = request.getUrl().getPort(); | ||
| addInitialHttpAttributesToSpan(span, host, port); | ||
| } | ||
|
|
||
| /** Add initial HTTP attributes to the existing active span */ | ||
| private void addInitialHttpAttributesToSpan(Span span, String host, Integer port) { | ||
| BigQueryTelemetryTracer.addCommonAttributeToSpan(span); | ||
| span.setAttribute(BigQueryTelemetryTracer.RPC_SYSTEM_NAME, HTTP_RPC_SYSTEM_NAME); | ||
| span.setAttribute(BigQueryTelemetryTracer.SERVER_ADDRESS, host); | ||
| if (port != null && port > 0) { | ||
| span.setAttribute(BigQueryTelemetryTracer.SERVER_PORT, port.longValue()); | ||
| } | ||
| // TODO add full sanitized url, url domain, request method | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.