-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bigquery): add response body tracking for compressed response #12201
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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 com.google.cloud.bigquery.telemetry.HttpTracingRequestInitializer.HTTP_RESPONSE_BODY_SIZE; | ||
|
|
||
| import com.google.api.client.http.LowLevelHttpResponse; | ||
| import com.google.common.io.CountingInputStream; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import java.io.FilterInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| /** | ||
| * Class that wraps a LowLevelHttpResponse to be able to inject wrapper on the delegate InputStream | ||
| * that allows us to capture response size for telemetry purposes. | ||
| */ | ||
| class HttpTracingLowLevelHttpResponse extends LowLevelHttpResponse { | ||
| private final LowLevelHttpResponse delegate; | ||
| private InputStream countingInputStream; | ||
| private final Span span; | ||
|
|
||
| public HttpTracingLowLevelHttpResponse(LowLevelHttpResponse delegate, Span span) { | ||
| this.delegate = delegate; | ||
| this.span = span; | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream getContent() throws IOException { | ||
| if (countingInputStream == null) { | ||
| InputStream originalContent = delegate.getContent(); | ||
| if (originalContent != null && span != null && span.getSpanContext().isValid()) { | ||
| countingInputStream = getWrappedInputStream(span, originalContent); | ||
| } else { | ||
| countingInputStream = originalContent; | ||
| } | ||
| } | ||
| return countingInputStream; | ||
| } | ||
|
|
||
| @Override | ||
| public String getContentEncoding() throws IOException { | ||
| return delegate.getContentEncoding(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getContentLength() throws IOException { | ||
| return delegate.getContentLength(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getContentType() throws IOException { | ||
| return delegate.getContentType(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getStatusLine() throws IOException { | ||
| return delegate.getStatusLine(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getStatusCode() throws IOException { | ||
| return delegate.getStatusCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getReasonPhrase() throws IOException { | ||
| return delegate.getReasonPhrase(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getHeaderCount() throws IOException { | ||
| return delegate.getHeaderCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getHeaderName(int index) throws IOException { | ||
| return delegate.getHeaderName(index); | ||
| } | ||
|
|
||
| @Override | ||
| public String getHeaderValue(int index) throws IOException { | ||
| return delegate.getHeaderValue(index); | ||
| } | ||
|
|
||
| @Override | ||
| public void disconnect() throws IOException { | ||
| delegate.disconnect(); | ||
| } | ||
|
|
||
| private InputStream getWrappedInputStream(Span span, InputStream content) { | ||
| CountingInputStream counter = new CountingInputStream(content); | ||
| return new FilterInputStream(counter) { | ||
| @Override | ||
| public void close() throws IOException { | ||
| super.close(); | ||
| span.setAttribute(HTTP_RESPONSE_BODY_SIZE, counter.getCount()); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import java.io.IOException; | ||
| import java.lang.reflect.Field; | ||
|
|
||
| /** | ||
| * HttpRequestInitializer that wraps a delegate initializer, intercepts all HTTP requests, adds | ||
|
|
@@ -50,6 +51,7 @@ public class HttpTracingRequestInitializer implements HttpRequestInitializer { | |
| AttributeKey.longKey("http.response.body.size"); | ||
|
|
||
| @VisibleForTesting static final String HTTP_RPC_SYSTEM_NAME = "http"; | ||
| @VisibleForTesting static final String GZIP_ENCODING = "gzip"; | ||
|
|
||
| private static final java.util.Set<String> REDACTED_QUERY_PARAMETERS = | ||
| com.google.common.collect.ImmutableSet.of( | ||
|
|
@@ -137,7 +139,29 @@ static void addResponseBodySizeToSpan(HttpResponse response, Span span) { | |
| if (contentLength != null && contentLength > 0) { | ||
| span.setAttribute(HTTP_RESPONSE_BODY_SIZE, contentLength); | ||
| } | ||
| // TODO handle chunked responses | ||
| // For compressed responses without Content-Length, we need to wrap the response to get the | ||
| // actual size | ||
| if (GZIP_ENCODING.equals(response.getContentEncoding())) { | ||
| getResponseBodySizeForCompressedResponse(response, span); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wraps the LowLevelHttpResponse with a HttpTracingLowLevelHttpResponse to give us access to get | ||
| * response body size for compressed responses. | ||
| */ | ||
| private static void getResponseBodySizeForCompressedResponse(HttpResponse response, Span span) { | ||
| try { | ||
| Field lowlevelField = HttpResponse.class.getDeclaredField("response"); | ||
| lowlevelField.setAccessible(true); | ||
| LowLevelHttpResponse lowLevelHttpResponse = | ||
| (LowLevelHttpResponse) lowlevelField.get(response); | ||
| HttpTracingLowLevelHttpResponse wrappedResponse = | ||
| new HttpTracingLowLevelHttpResponse(lowLevelHttpResponse, span); | ||
| lowlevelField.set(response, wrappedResponse); | ||
| } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) { | ||
| // Ignore - stream wrapping failed, we will not track response size | ||
| } | ||
|
Comment on lines
+162
to
+164
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using reflection to access and modify private fields of a library class ( While I understand the rationale for this approach from the PR description, silently ignoring the exceptions makes it difficult to diagnose issues if this mechanism fails. I recommend logging the exception at a You could add a private static final java.util.logging.Logger logger =
java.util.logging.Logger.getLogger(HttpTracingRequestInitializer.class.getName());And then change the catch block to: } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
logger.log(
java.util.logging.Level.WARNING,
"Failed to wrap response for compressed size tracking due to reflection issue. "
+ "This may happen if the underlying http-client library is updated.",
e);
// stream wrapping failed, we will not track response size
} |
||
| } | ||
|
|
||
| /** Removes credentials from URL. */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.