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

Commit 2d4d575

Browse files
chore: clean up Util to return Status.Code instead of string
This in prep for migrating to the strongly typed metrics Change-Id: Ibc255f7bbb9ecf02fedcc44c6a31f652c97b69fd
1 parent 175760c commit 2d4d575

4 files changed

Lines changed: 35 additions & 38 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: 17 additions & 21 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;
@@ -39,18 +38,19 @@
3938
import com.google.bigtable.v2.TableName;
4039
import com.google.cloud.bigtable.Version;
4140
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
41+
import com.google.cloud.bigtable.data.v2.internal.csm.MetricRegistry;
42+
import com.google.cloud.bigtable.data.v2.internal.csm.MetricRegistry.RecorderRegistry;
4243
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.ClientInfo;
4344
import com.google.cloud.bigtable.data.v2.stub.MetadataExtractorInterceptor;
4445
import com.google.common.collect.ImmutableMap;
4546
import io.grpc.Metadata;
4647
import io.grpc.Status;
47-
import io.grpc.StatusException;
48-
import io.grpc.StatusRuntimeException;
4948
import io.opencensus.stats.StatsRecorder;
5049
import io.opencensus.tags.TagKey;
5150
import io.opencensus.tags.TagValue;
5251
import io.opencensus.tags.Tagger;
5352
import io.opentelemetry.api.OpenTelemetry;
53+
import io.opentelemetry.api.metrics.Meter;
5454
import io.opentelemetry.sdk.OpenTelemetrySdk;
5555
import io.opentelemetry.sdk.metrics.InstrumentSelector;
5656
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
@@ -67,7 +67,6 @@
6767
import java.util.Locale;
6868
import java.util.Map;
6969
import java.util.Optional;
70-
import java.util.concurrent.CancellationException;
7170
import java.util.concurrent.ScheduledExecutorService;
7271
import javax.annotation.Nullable;
7372

@@ -79,25 +78,22 @@ public class Util {
7978
static final Metadata.Key<String> ATTEMPT_EPOCH_KEY =
8079
Metadata.Key.of("bigtable-client-attempt-epoch-usec", Metadata.ASCII_STRING_MARSHALLER);
8180

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-
81+
public static Status.Code extractStatus(@Nullable Throwable error) {
8682
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();
83+
return Status.Code.OK;
84+
}
85+
if (error instanceof ApiException) {
86+
ApiException apiException = (ApiException) error;
87+
if (apiException.getStatusCode() instanceof GrpcStatusCode) {
88+
return ((GrpcStatusCode) apiException.getStatusCode()).getTransportCode();
89+
}
9890
}
9991

100-
return statusString;
92+
Status s = Status.fromThrowable(error);
93+
if (s != null) {
94+
return s.getCode();
95+
}
96+
return Status.Code.UNKNOWN;
10197
}
10298

10399
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)