Skip to content

Commit 75d4105

Browse files
authored
feat: add internal "deadline remaining" client side metric #2341 (#2370)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigtable/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Rollback plan is reviewed and LGTMed - [ ] All new data plane features have a completed end to end testing plan Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 6685aa3 commit 75d4105

File tree

10 files changed

+232
-13
lines changed

10 files changed

+232
-13
lines changed

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

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import com.google.cloud.bigtable.data.v2.stub.changestream.GenerateInitialChangeStreamPartitionsUserCallable;
106106
import com.google.cloud.bigtable.data.v2.stub.changestream.ReadChangeStreamResumptionStrategy;
107107
import com.google.cloud.bigtable.data.v2.stub.changestream.ReadChangeStreamUserCallable;
108+
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracer;
108109
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracerStreamingCallable;
109110
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracerUnaryCallable;
110111
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsTracerFactory;
@@ -545,7 +546,12 @@ public <RowT> ServerStreamingCallable<Query, RowT> createReadRowsCallable(
545546
new TracedServerStreamingCallable<>(
546547
readRowsUserCallable, clientContext.getTracerFactory(), span);
547548

548-
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
549+
return traced.withDefaultCallContext(
550+
clientContext
551+
.getDefaultCallContext()
552+
.withOption(
553+
BigtableTracer.OPERATION_TIMEOUT_KEY,
554+
settings.readRowsSettings().getRetrySettings().getTotalTimeout()));
549555
}
550556

551557
/**
@@ -579,7 +585,12 @@ public <RowT> UnaryCallable<Query, RowT> createReadRowCallable(RowAdapter<RowT>
579585
UnaryCallable<Query, RowT> traced =
580586
new TracedUnaryCallable<>(
581587
firstRow, clientContext.getTracerFactory(), getSpanName("ReadRow"));
582-
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
588+
return traced.withDefaultCallContext(
589+
clientContext
590+
.getDefaultCallContext()
591+
.withOption(
592+
BigtableTracer.OPERATION_TIMEOUT_KEY,
593+
settings.readRowSettings().getRetrySettings().getTotalTimeout()));
583594
} else {
584595
ServerStreamingCallable<ReadRowsRequest, RowT> readRowsCallable =
585596
createReadRowsBaseCallable(
@@ -599,7 +610,11 @@ public <RowT> UnaryCallable<Query, RowT> createReadRowCallable(RowAdapter<RowT>
599610

600611
return new BigtableUnaryOperationCallable<>(
601612
readRowCallable,
602-
clientContext.getDefaultCallContext(),
613+
clientContext
614+
.getDefaultCallContext()
615+
.withOption(
616+
BigtableTracer.OPERATION_TIMEOUT_KEY,
617+
settings.readRowSettings().getRetrySettings().getTotalTimeout()),
603618
clientContext.getTracerFactory(),
604619
getSpanName("ReadRow"),
605620
/*allowNoResponses=*/ true);
@@ -715,7 +730,12 @@ private <RowT> UnaryCallable<Query, List<RowT>> createBulkReadRowsCallable(
715730
UnaryCallable<Query, List<RowT>> traced =
716731
new TracedUnaryCallable<>(tracedBatcher, clientContext.getTracerFactory(), span);
717732

718-
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
733+
return traced.withDefaultCallContext(
734+
clientContext
735+
.getDefaultCallContext()
736+
.withOption(
737+
BigtableTracer.OPERATION_TIMEOUT_KEY,
738+
settings.bulkReadRowsSettings().getRetrySettings().getTotalTimeout()));
719739
}
720740

721741
/**
@@ -780,7 +800,14 @@ public ApiFuture<List<KeyOffset>> futureCall(String s, ApiCallContext apiCallCon
780800
retryable = withRetries(withBigtableTracer, settings.sampleRowKeysSettings());
781801

782802
return createUserFacingUnaryCallable(
783-
methodName, new SampleRowKeysCallableWithRequest(retryable, requestContext));
803+
methodName,
804+
new SampleRowKeysCallableWithRequest(retryable, requestContext)
805+
.withDefaultCallContext(
806+
clientContext
807+
.getDefaultCallContext()
808+
.withOption(
809+
BigtableTracer.OPERATION_TIMEOUT_KEY,
810+
settings.sampleRowKeysSettings().getRetrySettings().getTotalTimeout())));
784811
}
785812

786813
/**
@@ -903,7 +930,12 @@ private UnaryCallable<BulkMutation, MutateRowsAttemptResult> createMutateRowsBas
903930
new TracedUnaryCallable<>(
904931
tracedBatcherUnaryCallable, clientContext.getTracerFactory(), spanName);
905932

906-
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
933+
return traced.withDefaultCallContext(
934+
clientContext
935+
.getDefaultCallContext()
936+
.withOption(
937+
BigtableTracer.OPERATION_TIMEOUT_KEY,
938+
settings.bulkMutateRowsSettings().getRetrySettings().getTotalTimeout()));
907939
}
908940

909941
/**
@@ -1108,7 +1140,15 @@ private UnaryCallable<ReadModifyWriteRow, Row> createReadModifyWriteRowCallable(
11081140
ServerStreamingCallable<String, ByteStringRange> traced =
11091141
new TracedServerStreamingCallable<>(retrying, clientContext.getTracerFactory(), span);
11101142

1111-
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
1143+
return traced.withDefaultCallContext(
1144+
clientContext
1145+
.getDefaultCallContext()
1146+
.withOption(
1147+
BigtableTracer.OPERATION_TIMEOUT_KEY,
1148+
settings
1149+
.generateInitialChangeStreamPartitionsSettings()
1150+
.getRetrySettings()
1151+
.getTotalTimeout()));
11121152
}
11131153

11141154
/**
@@ -1180,7 +1220,12 @@ private UnaryCallable<ReadModifyWriteRow, Row> createReadModifyWriteRowCallable(
11801220
new TracedServerStreamingCallable<>(
11811221
readChangeStreamUserCallable, clientContext.getTracerFactory(), span);
11821222

1183-
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
1223+
return traced.withDefaultCallContext(
1224+
clientContext
1225+
.getDefaultCallContext()
1226+
.withOption(
1227+
BigtableTracer.OPERATION_TIMEOUT_KEY,
1228+
settings.readChangeStreamSettings().getRetrySettings().getTotalTimeout()));
11841229
}
11851230

11861231
/**
@@ -1266,7 +1311,13 @@ public Map<String, String> extract(ExecuteQueryRequest executeQueryRequest) {
12661311
new TracedServerStreamingCallable<>(retries, clientContext.getTracerFactory(), span);
12671312

12681313
return new ExecuteQueryCallable(
1269-
traced.withDefaultCallContext(clientContext.getDefaultCallContext()), requestContext);
1314+
traced.withDefaultCallContext(
1315+
clientContext
1316+
.getDefaultCallContext()
1317+
.withOption(
1318+
BigtableTracer.OPERATION_TIMEOUT_KEY,
1319+
settings.executeQuerySettings().getRetrySettings().getTotalTimeout())),
1320+
requestContext);
12701321
}
12711322

12721323
/**
@@ -1344,7 +1395,12 @@ public ApiFuture<RespT> futureCall(ReqT reqT, ApiCallContext apiCallContext) {
13441395
clientContext.getTracerFactory(),
13451396
getSpanName(methodDescriptor.getBareMethodName()));
13461397

1347-
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
1398+
return traced.withDefaultCallContext(
1399+
clientContext
1400+
.getDefaultCallContext()
1401+
.withOption(
1402+
BigtableTracer.OPERATION_TIMEOUT_KEY,
1403+
callSettings.getRetrySettings().getTotalTimeout()));
13481404
}
13491405

13501406
private <BaseReqT, BaseRespT, ReqT, RespT> UnaryCallable<ReqT, RespT> createUnaryCallableNew(
@@ -1373,7 +1429,11 @@ private <BaseReqT, BaseRespT, ReqT, RespT> UnaryCallable<ReqT, RespT> createUnar
13731429

13741430
return new BigtableUnaryOperationCallable<>(
13751431
transformed,
1376-
clientContext.getDefaultCallContext(),
1432+
clientContext
1433+
.getDefaultCallContext()
1434+
.withOption(
1435+
BigtableTracer.OPERATION_TIMEOUT_KEY,
1436+
callSettings.getRetrySettings().getTotalTimeout()),
13771437
clientContext.getTracerFactory(),
13781438
getSpanName(methodDescriptor.getBareMethodName()),
13791439
/* allowNoResponse= */ false);
@@ -1407,7 +1467,12 @@ public Map<String, String> extract(PingAndWarmRequest request) {
14071467
})
14081468
.build(),
14091469
Collections.emptySet());
1410-
return pingAndWarm.withDefaultCallContext(clientContext.getDefaultCallContext());
1470+
return pingAndWarm.withDefaultCallContext(
1471+
clientContext
1472+
.getDefaultCallContext()
1473+
.withOption(
1474+
BigtableTracer.OPERATION_TIMEOUT_KEY,
1475+
settings.pingAndWarmSettings().getRetrySettings().getTotalTimeout()));
14111476
}
14121477

14131478
private <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> withRetries(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.METER_NAME;
2424
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.OPERATION_LATENCIES_NAME;
2525
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.PER_CONNECTION_ERROR_COUNT_NAME;
26+
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.REMAINING_DEADLINE_NAME;
2627
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.RETRY_COUNT_NAME;
2728
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.SERVER_LATENCIES_NAME;
2829

@@ -115,7 +116,8 @@ public final class BigtableCloudMonitoringExporter implements MetricExporter {
115116
CLIENT_BLOCKING_LATENCIES_NAME,
116117
APPLICATION_BLOCKING_LATENCIES_NAME,
117118
RETRY_COUNT_NAME,
118-
CONNECTIVITY_ERROR_COUNT_NAME)
119+
CONNECTIVITY_ERROR_COUNT_NAME,
120+
REMAINING_DEADLINE_NAME)
119121
.stream()
120122
.map(m -> METER_NAME + m)
121123
.collect(ImmutableList.toImmutableList());

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
package com.google.cloud.bigtable.data.v2.stub.metrics;
1717

1818
import com.google.api.core.BetaApi;
19+
import com.google.api.core.InternalApi;
1920
import com.google.api.gax.rpc.ApiCallContext;
2021
import com.google.api.gax.tracing.ApiTracer;
2122
import com.google.api.gax.tracing.BaseApiTracer;
2223
import javax.annotation.Nullable;
24+
import org.threeten.bp.Duration;
2325

2426
/**
2527
* A Bigtable specific {@link ApiTracer} that includes additional contexts. This class is a base
@@ -30,6 +32,10 @@ public class BigtableTracer extends BaseApiTracer {
3032

3133
private volatile int attempt = 0;
3234

35+
@InternalApi("for internal use only")
36+
public static final ApiCallContext.Key<Duration> OPERATION_TIMEOUT_KEY =
37+
ApiCallContext.Key.create("OPERATION_TIMEOUT");
38+
3339
@Override
3440
public void attemptStarted(int attemptNumber) {
3541
this.attempt = attemptNumber;
@@ -93,4 +99,12 @@ public void grpcChannelQueuedLatencies(long queuedTimeMs) {
9399
public void grpcMessageSent() {
94100
// noop
95101
}
102+
103+
/**
104+
* Record the operation timeout from user settings for calculating remaining deadline. This will
105+
* be called in BuiltinMetricsTracer.
106+
*/
107+
public void setOperationTimeout(Duration operationTimeout) {
108+
// noop
109+
}
96110
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.cloud.bigtable.data.v2.stub.metrics;
1717

1818
import com.google.api.core.InternalApi;
19+
import com.google.api.gax.grpc.GrpcCallContext;
1920
import com.google.api.gax.grpc.GrpcResponseMetadata;
2021
import com.google.api.gax.rpc.ApiCallContext;
2122
import com.google.api.gax.rpc.ResponseObserver;
@@ -26,6 +27,7 @@
2627
import com.google.common.base.Stopwatch;
2728
import java.util.concurrent.TimeUnit;
2829
import javax.annotation.Nonnull;
30+
import org.threeten.bp.Duration;
2931

3032
/**
3133
* This callable will
@@ -62,6 +64,11 @@ public void call(
6264
BigtableTracerResponseObserver<ResponseT> innerObserver =
6365
new BigtableTracerResponseObserver<>(
6466
responseObserver, (BigtableTracer) context.getTracer(), responseMetadata);
67+
GrpcCallContext callContext = (GrpcCallContext) context;
68+
Duration deadline = callContext.getOption(BigtableTracer.OPERATION_TIMEOUT_KEY);
69+
if (deadline != null) {
70+
((BigtableTracer) context.getTracer()).setOperationTimeout(deadline);
71+
}
6572
innerCallable.call(
6673
request,
6774
innerObserver,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import com.google.api.core.ApiFutureCallback;
2020
import com.google.api.core.ApiFutures;
2121
import com.google.api.core.InternalApi;
22+
import com.google.api.gax.grpc.GrpcCallContext;
2223
import com.google.api.gax.grpc.GrpcResponseMetadata;
2324
import com.google.api.gax.rpc.ApiCallContext;
2425
import com.google.api.gax.rpc.UnaryCallable;
2526
import com.google.common.base.Preconditions;
2627
import com.google.common.util.concurrent.MoreExecutors;
2728
import javax.annotation.Nonnull;
29+
import org.threeten.bp.Duration;
2830

2931
/**
3032
* This callable will:
@@ -58,6 +60,11 @@ public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext context)
5860
BigtableTracerUnaryCallback<ResponseT> callback =
5961
new BigtableTracerUnaryCallback<ResponseT>(
6062
(BigtableTracer) context.getTracer(), responseMetadata);
63+
GrpcCallContext callContext = (GrpcCallContext) context;
64+
Duration deadline = callContext.getOption(BigtableTracer.OPERATION_TIMEOUT_KEY);
65+
if (deadline != null) {
66+
((BigtableTracer) context.getTracer()).setOperationTimeout(deadline);
67+
}
6168
ApiFuture<ResponseT> future =
6269
innerCallable.futureCall(
6370
request,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class BuiltinMetricsConstants {
5858
static final String SERVER_LATENCIES_NAME = "server_latencies";
5959
static final String FIRST_RESPONSE_LATENCIES_NAME = "first_response_latencies";
6060
static final String APPLICATION_BLOCKING_LATENCIES_NAME = "application_latencies";
61+
static final String REMAINING_DEADLINE_NAME = "remaining_deadline";
6162
static final String CLIENT_BLOCKING_LATENCIES_NAME = "throttling_latencies";
6263
static final String PER_CONNECTION_ERROR_COUNT_NAME = "per_connection_error_count";
6364

@@ -214,6 +215,16 @@ public static Map<InstrumentSelector, View> getAllViews() {
214215
ImmutableSet.<AttributeKey>builder()
215216
.add(BIGTABLE_PROJECT_ID_KEY, INSTANCE_ID_KEY, APP_PROFILE_KEY, CLIENT_NAME_KEY)
216217
.build());
218+
defineView(
219+
views,
220+
REMAINING_DEADLINE_NAME,
221+
AGGREGATION_WITH_MILLIS_HISTOGRAM,
222+
InstrumentType.HISTOGRAM,
223+
"ms",
224+
ImmutableSet.<AttributeKey>builder()
225+
.addAll(COMMON_ATTRIBUTES)
226+
.add(STREAMING_KEY, STATUS_KEY)
227+
.build());
217228

218229
return views.build();
219230
}

0 commit comments

Comments
 (0)