Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 68704bd

Browse files
committed
simplified logic
1 parent de0ee8a commit 68704bd

2 files changed

Lines changed: 36 additions & 42 deletions

File tree

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public HttpBigQueryRpc(BigQueryOptions options) {
109109
HttpRequestInitializer initializer = transportOptions.getHttpRequestInitializer(options);
110110

111111
String resolvedBigQueryRootUrl = options.getResolvedApiaryHost("bigquery");
112-
// Wrap with tracing initializer if OpenTelemetry is enabled
112+
113113
if (options.isOpenTelemetryTracingEnabled() && options.getOpenTelemetryTracer() != null) {
114114
initializer =
115115
new HttpTracingRequestInitializer(

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpTracingRequestInitializer.java

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,20 @@ public class HttpTracingRequestInitializer implements HttpRequestInitializer {
5555
AttributeKey.longKey("http.response.body.size");
5656

5757
@VisibleForTesting static final String HTTP_RPC_SYSTEM_NAME = "http";
58+
5859
private static final String REDACTED_VALUE = "REDACTED";
60+
// Required by OpenTelemetry semantic conventions:
61+
// https://opentelemetry.io/docs/specs/semconv/registry/attributes/url/#url-full
5962
private static final Set<String> SENSITIVE_QUERY_KEYS =
6063
Collections.unmodifiableSet(
61-
new HashSet<>(Arrays.asList("AWSAccessKeyId", "Signature", "sig", "X-Goog-Signature")));
64+
new HashSet<>(
65+
Arrays.asList(
66+
"AWSAccessKeyId",
67+
"Signature",
68+
"sig",
69+
"X-Goog-Signature",
70+
// Google uses this as a key in resumable uploads.
71+
"upload_id")));
6272

6373
private final HttpRequestInitializer delegate;
6474
private final Tracer tracer;
@@ -88,27 +98,25 @@ public void initialize(HttpRequest request) throws IOException {
8898

8999
Span span = createHttpTraceSpan(httpMethod, url, host, port);
90100

91-
// Wrap the existing response interceptor
92101
HttpResponseInterceptor originalInterceptor = request.getResponseInterceptor();
93102
request.setResponseInterceptor(
94103
response -> {
95-
if (span.isRecording()) {
96-
try {
97-
int statusCode = response.getStatusCode();
98-
addCommonResponseAttributesToSpan(request, response, span, httpMethod, statusCode);
99-
addSuccessResponseToSpan(response, span, statusCode);
100-
if (originalInterceptor != null) {
101-
originalInterceptor.interceptResponse(response);
102-
}
103-
} finally {
104-
span.end();
104+
addCommonResponseAttributesToSpan(
105+
request, response, span, httpMethod, response.getStatusCode());
106+
span.setStatus(StatusCode.OK);
107+
108+
try {
109+
if (originalInterceptor != null) {
110+
originalInterceptor.interceptResponse(response);
105111
}
106-
} else if (originalInterceptor != null) {
107-
originalInterceptor.interceptResponse(response);
112+
} catch (IOException e) {
113+
addExceptionToSpan(e, span);
114+
throw e;
115+
} finally {
116+
span.end();
108117
}
109118
});
110119

111-
// Wrap the existing unsuccessful response handler
112120
HttpUnsuccessfulResponseHandler originalHandler = request.getUnsuccessfulResponseHandler();
113121
request.setUnsuccessfulResponseHandler(
114122
(request1, response, supportsRetry) -> {
@@ -124,16 +132,15 @@ public void initialize(HttpRequest request) throws IOException {
124132
addExceptionToSpan(e, span);
125133
throw e;
126134
} finally {
127-
if (span.isRecording()) {
128-
span.end();
129-
}
135+
span.end();
130136
}
131137
});
132138
}
133139

134140
/** Initial HTTP trace span creation with basic attributes from request */
135141
private Span createHttpTraceSpan(String httpMethod, String url, String host, Integer port) {
136-
// TODO: Determine span name: {method} {url.template} or {method}
142+
// TODO: add url template && resource name
143+
// TODO: appropriately determine span name using: {method} {url.template} or {method}
137144
Span span =
138145
BigQueryTelemetryTracer.newSpanBuilder(tracer, httpMethod)
139146
.setAttribute(HTTP_REQUEST_METHOD, httpMethod)
@@ -142,8 +149,6 @@ private Span createHttpTraceSpan(String httpMethod, String url, String host, Int
142149
.setAttribute(URL_DOMAIN, resolveUrlDomain(host))
143150
.setAttribute(BigQueryTelemetryTracer.RPC_SYSTEM_NAME, HTTP_RPC_SYSTEM_NAME)
144151
.startSpan();
145-
146-
// TODO: add url template && resource name
147152
if (port != null && port > 0) {
148153
span.setAttribute(BigQueryTelemetryTracer.SERVER_PORT, port.longValue());
149154
}
@@ -166,20 +171,14 @@ private String resolveUrlDomain(String requestHost) {
166171

167172
private static void addCommonResponseAttributesToSpan(
168173
HttpRequest request, HttpResponse response, Span span, String httpMethod, int statusCode) {
169-
// This is called after we get a response as sometimes the request body size isn't available
170-
// before the response is received.
174+
// We add request body size/update request method after we receive response as they sometimes
175+
// the data is
176+
// not available until after the http request execution
171177
addRequestBodySizeToSpan(request, span);
172178
checkForUpdatedRequestMethod(response, httpMethod, span);
173-
setResponseBodySize(response, span);
174-
span.setAttribute(HTTP_RESPONSE_STATUS_CODE, statusCode);
175-
}
176179

177-
private static void addSuccessResponseToSpan(HttpResponse response, Span span, int statusCode) {
178-
if (statusCode >= 400) {
179-
addErrorResponseToSpan(response, span, statusCode);
180-
} else {
181-
span.setStatus(StatusCode.OK);
182-
}
180+
addResponseBodySizeToSpan(response, span);
181+
span.setAttribute(HTTP_RESPONSE_STATUS_CODE, statusCode);
183182
}
184183

185184
private static void addExceptionToSpan(IOException e, Span span) {
@@ -208,22 +207,17 @@ private static void addErrorResponseToSpan(HttpResponse response, Span span, int
208207
}
209208

210209
private static void addRequestBodySizeToSpan(HttpRequest request, Span span) {
211-
Long requestBodySize = null;
212210
try {
213-
HttpContent content = request.getContent();
214-
215-
if (content != null) {
216-
requestBodySize = content.getLength();
211+
long contentLength = request.getContent().getLength();
212+
if (contentLength > 0) {
213+
span.setAttribute(HTTP_REQUEST_BODY_SIZE, contentLength);
217214
}
218215
} catch (Exception e) {
219216
// Ignore - body size not available
220217
}
221-
if (requestBodySize != null) {
222-
span.setAttribute(HTTP_REQUEST_BODY_SIZE, requestBodySize);
223-
}
224218
}
225219

226-
private static void setResponseBodySize(HttpResponse response, Span span) {
220+
private static void addResponseBodySizeToSpan(HttpResponse response, Span span) {
227221
try {
228222
long contentLength = response.getHeaders().getContentLength();
229223
if (contentLength > 0) {

0 commit comments

Comments
 (0)