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

Commit be51db4

Browse files
chore: clean up Util to return Status.Code instead of string (#2805)
This in prep for migrating to the strongly typed metrics
1 parent a915fb7 commit be51db4

4 files changed

Lines changed: 36 additions & 37 deletions

File tree

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.common.base.Stopwatch;
4141
import com.google.common.math.IntMath;
4242
import io.grpc.Deadline;
43+
import io.grpc.Status;
4344
import io.opentelemetry.api.common.Attributes;
4445
import io.opentelemetry.api.metrics.DoubleGauge;
4546
import io.opentelemetry.api.metrics.DoubleHistogram;
@@ -336,9 +337,9 @@ public void disableFlowControl() {
336337
flowControlIsDisabled = true;
337338
}
338339

339-
private void recordOperationCompletion(@Nullable Throwable status) {
340+
private void recordOperationCompletion(@Nullable Throwable throwable) {
340341
if (operationFinishedEarly.get()) {
341-
status = null; // force an ok
342+
throwable = null; // force an ok
342343
}
343344

344345
if (!opFinished.compareAndSet(false, true)) {
@@ -347,7 +348,7 @@ private void recordOperationCompletion(@Nullable Throwable status) {
347348
long operationLatencyNano = operationTimer.elapsed(TimeUnit.NANOSECONDS);
348349

349350
boolean isStreaming = operationType == OperationType.ServerStreaming;
350-
String statusStr = extractStatus(status);
351+
Status.Code code = extractStatus(throwable);
351352

352353
// Publish metric data with all the attributes. The attributes get filtered in
353354
// BuiltinMetricsConstants when we construct the views.
@@ -359,7 +360,7 @@ private void recordOperationCompletion(@Nullable Throwable status) {
359360
.put(METHOD_KEY, spanName.toString())
360361
.put(CLIENT_NAME_KEY, NAME)
361362
.put(STREAMING_KEY, isStreaming)
362-
.put(STATUS_KEY, statusStr)
363+
.put(STATUS_KEY, code.name())
363364
.build();
364365

365366
// Only record when retry count is greater than 0 so the retry
@@ -381,9 +382,9 @@ private void recordOperationCompletion(@Nullable Throwable status) {
381382
}
382383
}
383384

384-
private void recordAttemptCompletion(@Nullable Throwable status) {
385+
private void recordAttemptCompletion(@Nullable Throwable throwable) {
385386
if (operationFinishedEarly.get()) {
386-
status = null; // force an ok
387+
throwable = null; // force an ok
387388
}
388389
// If the attempt failed, the time spent in retry should be counted in application latency.
389390
// Stop the stopwatch and decrement requestLeft.
@@ -397,14 +398,14 @@ private void recordAttemptCompletion(@Nullable Throwable status) {
397398

398399
boolean isStreaming = operationType == OperationType.ServerStreaming;
399400

400-
// Patch the status until it's fixed in gax. When an attempt failed,
401+
// Patch the throwable until it's fixed in gax. When an attempt failed,
401402
// it'll throw a ServerStreamingAttemptException. Unwrap the exception
402403
// so it could get processed by extractStatus
403-
if (status instanceof ServerStreamingAttemptException) {
404-
status = status.getCause();
404+
if (throwable instanceof ServerStreamingAttemptException) {
405+
throwable = throwable.getCause();
405406
}
406407

407-
String statusStr = extractStatus(status);
408+
Status.Code code = extractStatus(throwable);
408409

409410
Attributes attributes =
410411
baseAttributes.toBuilder()
@@ -414,7 +415,7 @@ private void recordAttemptCompletion(@Nullable Throwable status) {
414415
.put(METHOD_KEY, spanName.toString())
415416
.put(CLIENT_NAME_KEY, NAME)
416417
.put(STREAMING_KEY, isStreaming)
417-
.put(STATUS_KEY, statusStr)
418+
.put(STATUS_KEY, code.name())
418419
.build();
419420

420421
totalClientBlockingTime.addAndGet(grpcMessageSentDelay.get());
@@ -477,11 +478,11 @@ public void setBatchWriteFlowControlTargetQps(double targetQps) {
477478

478479
@Override
479480
public void addBatchWriteFlowControlFactor(
480-
double factor, @Nullable Throwable status, boolean applied) {
481+
double factor, @Nullable Throwable throwable, boolean applied) {
481482
Attributes attributes =
482483
baseAttributes.toBuilder()
483484
.put(METHOD_KEY, spanName.toString())
484-
.put(STATUS_KEY, extractStatus(status))
485+
.put(STATUS_KEY, extractStatus(throwable).name())
485486
.put(APPLIED_KEY, applied)
486487
.build();
487488

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/MetricsTracer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private void recordOperationCompletion(@Nullable Throwable throwable) {
133133
newTagCtxBuilder()
134134
.putLocal(
135135
RpcMeasureConstants.BIGTABLE_STATUS,
136-
TagValue.create(Util.extractStatus(throwable)));
136+
TagValue.create(Util.extractStatus(throwable).name()));
137137

138138
measures.record(tagCtx.build());
139139
}
@@ -216,7 +216,7 @@ private void recordAttemptCompletion(@Nullable Throwable throwable) {
216216
newTagCtxBuilder()
217217
.putLocal(
218218
RpcMeasureConstants.BIGTABLE_STATUS,
219-
TagValue.create(Util.extractStatus(throwable)));
219+
TagValue.create(Util.extractStatus(throwable).name()));
220220

221221
measures.record(tagCtx.build());
222222
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
import com.google.api.core.InternalApi;
1919
import com.google.api.gax.grpc.GaxGrpcProperties;
20+
import com.google.api.gax.grpc.GrpcStatusCode;
2021
import com.google.api.gax.rpc.ApiCallContext;
2122
import com.google.api.gax.rpc.ApiException;
22-
import com.google.api.gax.rpc.StatusCode;
23-
import com.google.api.gax.rpc.StatusCode.Code;
2423
import com.google.api.gax.tracing.ApiTracerFactory;
2524
import com.google.api.gax.tracing.OpencensusTracerFactory;
2625
import com.google.auth.Credentials;
@@ -44,8 +43,6 @@
4443
import com.google.common.collect.ImmutableMap;
4544
import io.grpc.Metadata;
4645
import io.grpc.Status;
47-
import io.grpc.StatusException;
48-
import io.grpc.StatusRuntimeException;
4946
import io.opencensus.stats.StatsRecorder;
5047
import io.opencensus.tags.TagKey;
5148
import io.opencensus.tags.TagValue;
@@ -79,25 +76,26 @@ public class Util {
7976
static final Metadata.Key<String> ATTEMPT_EPOCH_KEY =
8077
Metadata.Key.of("bigtable-client-attempt-epoch-usec", Metadata.ASCII_STRING_MARSHALLER);
8178

82-
/** Convert an exception into a value that can be used to create an OpenCensus tag value. */
83-
public static String extractStatus(@Nullable Throwable error) {
84-
final String statusString;
85-
79+
public static Status.Code extractStatus(@Nullable Throwable error) {
8680
if (error == null) {
87-
return StatusCode.Code.OK.toString();
88-
} else if (error instanceof CancellationException) {
89-
statusString = Status.Code.CANCELLED.toString();
90-
} else if (error instanceof ApiException) {
91-
statusString = ((ApiException) error).getStatusCode().getCode().toString();
92-
} else if (error instanceof StatusRuntimeException) {
93-
statusString = ((StatusRuntimeException) error).getStatus().getCode().toString();
94-
} else if (error instanceof StatusException) {
95-
statusString = ((StatusException) error).getStatus().getCode().toString();
96-
} else {
97-
statusString = Code.UNKNOWN.toString();
81+
return Status.Code.OK;
82+
}
83+
// Handle java CancellationException as if it was a gax CancelledException
84+
if (error instanceof CancellationException) {
85+
return Status.Code.CANCELLED;
86+
}
87+
if (error instanceof ApiException) {
88+
ApiException apiException = (ApiException) error;
89+
if (apiException.getStatusCode() instanceof GrpcStatusCode) {
90+
return ((GrpcStatusCode) apiException.getStatusCode()).getTransportCode();
91+
}
9892
}
9993

100-
return statusString;
94+
Status s = Status.fromThrowable(error);
95+
if (s != null) {
96+
return s.getCode();
97+
}
98+
return Status.Code.UNKNOWN;
10199
}
102100

103101
static String extractTableId(Object request) {

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/UtilTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public class UtilTest {
3030
@Test
3131
public void testOk() {
32-
TagValue tagValue = TagValue.create(Util.extractStatus((Throwable) null));
32+
TagValue tagValue = TagValue.create(Util.extractStatus(null).name());
3333
assertThat(tagValue.asString()).isEqualTo("OK");
3434
}
3535

@@ -38,7 +38,7 @@ public void testError() {
3838
DeadlineExceededException error =
3939
new DeadlineExceededException(
4040
"Deadline exceeded", null, GrpcStatusCode.of(Status.Code.DEADLINE_EXCEEDED), true);
41-
TagValue tagValue = TagValue.create(Util.extractStatus(error));
41+
TagValue tagValue = TagValue.create(Util.extractStatus(error).name());
4242
assertThat(tagValue.asString()).isEqualTo("DEADLINE_EXCEEDED");
4343
}
4444
}

0 commit comments

Comments
 (0)