Skip to content

Commit 5b5dbaf

Browse files
committed
refactor to move attribute to HttpBigQueryRpc to easily handle reset the retry count in case of error
1 parent c40063e commit 5b5dbaf

5 files changed

Lines changed: 12 additions & 35 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import com.google.cloud.Tuple;
6767
import com.google.cloud.bigquery.BigQueryException;
6868
import com.google.cloud.bigquery.BigQueryOptions;
69+
import com.google.cloud.bigquery.BigQueryRetryAlgorithm;
6970
import com.google.cloud.bigquery.telemetry.BigQueryTelemetryTracer;
7071
import com.google.cloud.bigquery.telemetry.HttpTracingRequestInitializer;
7172
import com.google.cloud.http.HttpTransportOptions;
@@ -2150,6 +2151,11 @@ private Span createRpcTracingSpan(
21502151
.setAttribute(
21512152
BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID, gcpResourceDestinationId)
21522153
.setAttribute(BigQueryTelemetryTracer.URL_TEMPLATE, urlTemplate);
2154+
int retryAttempt = BigQueryRetryAlgorithm.getCurrentAttempt();
2155+
if (retryAttempt > 0) {
2156+
builder.setAttribute(
2157+
BigQueryTelemetryTracer.HTTP_REQUEST_RESEND_COUNT, (long) retryAttempt);
2158+
}
21532159
}
21542160

21552161
if (options != null) {
@@ -2182,6 +2188,8 @@ private <T> T executeWithSpan(Span span, SpanOperation<T> operation) throws IOEx
21822188
}
21832189
throw e;
21842190
} finally {
2191+
// Reset attempt count to 0 to avoid carrying over state across requests on the same thread
2192+
BigQueryRetryAlgorithm.setCurrentAttempt(0);
21852193
span.end();
21862194
}
21872195
}

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ private BigQueryTelemetryTracer() {}
6666
AttributeKey.stringKey("server.address");
6767
public static final AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port");
6868
public static final AttributeKey<String> URL_TEMPLATE = AttributeKey.stringKey("url.template");
69+
public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT =
70+
AttributeKey.longKey("http.request.resend_count");
6971

7072
public static void addCommonAttributeToSpan(Span span) {
7173
span.setAttribute(GCP_CLIENT_SERVICE, BQ_GCP_CLIENT_SERVICE)

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/HttpTracingRequestInitializer.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.api.client.http.*;
2020
import com.google.api.core.BetaApi;
2121
import com.google.api.core.InternalApi;
22-
import com.google.cloud.bigquery.BigQueryRetryAlgorithm;
2322
import com.google.common.annotations.VisibleForTesting;
2423
import io.opentelemetry.api.common.AttributeKey;
2524
import io.opentelemetry.api.trace.Span;
@@ -43,8 +42,6 @@ public class HttpTracingRequestInitializer implements HttpRequestInitializer {
4342
public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain");
4443
public static final AttributeKey<Long> HTTP_RESPONSE_STATUS_CODE =
4544
AttributeKey.longKey("http.response.status_code");
46-
public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT =
47-
AttributeKey.longKey("http.request.resend_count");
4845
public static final AttributeKey<Long> HTTP_REQUEST_BODY_SIZE =
4946
AttributeKey.longKey("http.request.body.size");
5047
public static final AttributeKey<Long> HTTP_RESPONSE_BODY_SIZE =
@@ -111,12 +108,6 @@ private void addInitialHttpAttributesToSpan(Span span, HttpRequest request) {
111108
span.setAttribute(BigQueryTelemetryTracer.SERVER_PORT, (long) port);
112109
}
113110
span.setAttribute(URL_FULL, getSanitizedUrl(request));
114-
int retryAttempt = BigQueryRetryAlgorithm.getCurrentAttempt();
115-
if (retryAttempt > 0) {
116-
span.setAttribute(HTTP_REQUEST_RESEND_COUNT, (long) retryAttempt);
117-
}
118-
// Reset attempt count to 0 to avoid carrying over state across requests on the same thread
119-
BigQueryRetryAlgorithm.setCurrentAttempt(0);
120111
}
121112

122113
private static void addCommonResponseAttributesToSpan(

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ public void testResendCountOnRetry() throws Exception {
11721172
rpcSpan
11731173
.getAttributes()
11741174
.get(
1175-
com.google.cloud.bigquery.telemetry.HttpTracingRequestInitializer
1175+
BigQueryTelemetryTracer
11761176
.HTTP_REQUEST_RESEND_COUNT));
11771177
} finally {
11781178
com.google.cloud.bigquery.BigQueryRetryAlgorithm.setCurrentAttempt(0);
@@ -1312,7 +1312,7 @@ public void testResendCountNotSetWhenDisabled() throws Exception {
13121312
rpcSpan
13131313
.getAttributes()
13141314
.get(
1315-
com.google.cloud.bigquery.telemetry.HttpTracingRequestInitializer
1315+
BigQueryTelemetryTracer
13161316
.HTTP_REQUEST_RESEND_COUNT));
13171317
} finally {
13181318
com.google.cloud.bigquery.BigQueryRetryAlgorithm.setCurrentAttempt(0);

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/HttpTracingRequestInitializerTest.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -335,29 +335,6 @@ public void testAddResponseBodySizeToSpan_NullLength() throws IOException {
335335
assertNull(span.getAttributes().get(HttpTracingRequestInitializer.HTTP_RESPONSE_BODY_SIZE));
336336
}
337337

338-
@Test
339-
public void testResendCountIsSetFromBigQueryRetryAlgorithm() throws IOException {
340-
com.google.cloud.bigquery.BigQueryRetryAlgorithm.setCurrentAttempt(3);
341-
try {
342-
HttpTransport transport = createTransport();
343-
HttpRequest request = buildGetRequest(transport, initializer, BASE_URL);
344-
345-
HttpResponse response = request.execute();
346-
response.disconnect();
347-
348-
spanScope.close();
349-
parentSpan.end();
350-
351-
List<SpanData> spans = spanExporter.getFinishedSpanItems();
352-
assertEquals(1, spans.size());
353-
SpanData span = spans.get(0);
354-
assertEquals(
355-
3L, span.getAttributes().get(HttpTracingRequestInitializer.HTTP_REQUEST_RESEND_COUNT));
356-
} finally {
357-
com.google.cloud.bigquery.BigQueryRetryAlgorithm.setCurrentAttempt(0);
358-
}
359-
}
360-
361338
private static HttpTransport createTransport() {
362339
return createTransport(200, null);
363340
}
@@ -452,6 +429,5 @@ private void closeAndVerifySpanData(
452429
} else {
453430
assertNull(span.getAttributes().get(HttpTracingRequestInitializer.HTTP_RESPONSE_BODY_SIZE));
454431
}
455-
assertNull(span.getAttributes().get(HttpTracingRequestInitializer.HTTP_REQUEST_RESEND_COUNT));
456432
}
457433
}

0 commit comments

Comments
 (0)