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

Commit ba09467

Browse files
committed
comments
1 parent 7c43031 commit ba09467

13 files changed

Lines changed: 564 additions & 639 deletions

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

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials;
2727
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
2828
import com.google.cloud.bigtable.data.v2.internal.JwtCredentialsWithAudience;
29-
import com.google.cloud.bigtable.data.v2.stub.metrics.*;
29+
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants;
30+
import com.google.cloud.bigtable.data.v2.stub.metrics.ChannelPoolMetricsTracker;
31+
import com.google.cloud.bigtable.data.v2.stub.metrics.CustomOpenTelemetryMetricsProvider;
32+
import com.google.cloud.bigtable.data.v2.stub.metrics.DefaultMetricsProvider;
33+
import com.google.cloud.bigtable.data.v2.stub.metrics.MetricsProvider;
34+
import com.google.cloud.bigtable.data.v2.stub.metrics.NoopMetricsProvider;
3035
import com.google.cloud.bigtable.gaxx.grpc.BigtableTransportChannelProvider;
3136
import com.google.cloud.bigtable.gaxx.grpc.ChannelPrimer;
3237
import io.grpc.ManagedChannelBuilder;
@@ -92,21 +97,17 @@ public static BigtableClientContext create(EnhancedBigtableStubSettings settings
9297
: null;
9398

9499
@Nullable OpenTelemetrySdk internalOtel = null;
95-
@Nullable ErrorCountPerConnectionMetricTracker errorCountPerConnectionMetricTracker = null;
96-
@Nullable OutstandingRpcsMetricTracker outstandingRpcsMetricTracker = null;
100+
@Nullable ChannelPoolMetricsTracker channelPoolMetricsTracker = null;
97101
// Internal metrics are scoped to the connections, so we need a mutable transportProvider,
98102
// otherwise there is
99103
// no reason to build the internal OtelProvider
100104
if (transportProvider != null) {
101105
internalOtel =
102106
settings.getInternalMetricsProvider().createOtelProvider(settings, credentials);
103107
if (internalOtel != null) {
104-
// Set up per connection error count tracker if all dependencies are met:
105-
// a configurable transport provider + otel
106-
errorCountPerConnectionMetricTracker =
107-
setupPerConnectionErrorTracer(builder, transportProvider, internalOtel);
108-
109-
outstandingRpcsMetricTracker = new OutstandingRpcsMetricTracker(internalOtel);
108+
channelPoolMetricsTracker =
109+
new ChannelPoolMetricsTracker(
110+
internalOtel, EnhancedBigtableStub.createBuiltinAttributes(builder.build()));
110111

111112
// Configure grpc metrics
112113
configureGrpcOtel(transportProvider, internalOtel);
@@ -136,20 +137,14 @@ public static BigtableClientContext create(EnhancedBigtableStubSettings settings
136137
BigtableTransportChannelProvider.create(
137138
(InstantiatingGrpcChannelProvider) transportProvider.build(),
138139
channelPrimer,
139-
outstandingRpcsMetricTracker);
140+
channelPoolMetricsTracker);
140141

141142
builder.setTransportChannelProvider(btTransportProvider);
142143
}
143144

144145
ClientContext clientContext = ClientContext.create(builder.build());
145-
146-
if (errorCountPerConnectionMetricTracker != null) {
147-
errorCountPerConnectionMetricTracker.startConnectionErrorCountTracker(
148-
clientContext.getExecutor());
149-
}
150-
151-
if (outstandingRpcsMetricTracker != null) {
152-
outstandingRpcsMetricTracker.start(clientContext.getExecutor());
146+
if (channelPoolMetricsTracker != null) {
147+
channelPoolMetricsTracker.start(clientContext.getExecutor());
153148
}
154149

155150
return new BigtableClientContext(
@@ -267,27 +262,6 @@ private static void patchCredentials(EnhancedBigtableStubSettings.Builder settin
267262
settings.setCredentialsProvider(FixedCredentialsProvider.create(patchedCreds));
268263
}
269264

270-
private static ErrorCountPerConnectionMetricTracker setupPerConnectionErrorTracer(
271-
EnhancedBigtableStubSettings.Builder builder,
272-
InstantiatingGrpcChannelProvider.Builder transportProvider,
273-
OpenTelemetry openTelemetry) {
274-
ErrorCountPerConnectionMetricTracker errorCountPerConnectionMetricTracker =
275-
new ErrorCountPerConnectionMetricTracker(
276-
openTelemetry, EnhancedBigtableStub.createBuiltinAttributes(builder.build()));
277-
ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> oldChannelConfigurator =
278-
transportProvider.getChannelConfigurator();
279-
transportProvider.setChannelConfigurator(
280-
managedChannelBuilder -> {
281-
managedChannelBuilder.intercept(errorCountPerConnectionMetricTracker.getInterceptor());
282-
283-
if (oldChannelConfigurator != null) {
284-
managedChannelBuilder = oldChannelConfigurator.apply(managedChannelBuilder);
285-
}
286-
return managedChannelBuilder;
287-
});
288-
return errorCountPerConnectionMetricTracker;
289-
}
290-
291265
private static void setupCookieHolder(
292266
InstantiatingGrpcChannelProvider.Builder transportProvider) {
293267
ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> oldChannelConfigurator =

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ public class BuiltinMetricsConstants {
141141
500_000.0,
142142
1_000_000.0));
143143

144+
// Buckets for outstanding RPCs per channel, max ~100
145+
private static final Aggregation AGGREGATION_OUTSTANDING_RPCS_HISTOGRAM =
146+
Aggregation.explicitBucketHistogram(
147+
ImmutableList.of(
148+
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0,
149+
25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 110.0)); // Added
150+
144151
static final Set<AttributeKey> COMMON_ATTRIBUTES =
145152
ImmutableSet.of(
146153
BIGTABLE_PROJECT_ID_KEY,
@@ -182,6 +189,7 @@ static void defineView(
182189
viewMap.put(selector, view);
183190
}
184191

192+
// uses cloud.BigtableClient schema
185193
public static Map<InstrumentSelector, View> getInternalViews() {
186194
ImmutableMap.Builder<InstrumentSelector, View> views = ImmutableMap.builder();
187195
defineView(
@@ -193,6 +201,15 @@ public static Map<InstrumentSelector, View> getInternalViews() {
193201
ImmutableSet.<AttributeKey>builder()
194202
.add(BIGTABLE_PROJECT_ID_KEY, INSTANCE_ID_KEY, APP_PROFILE_KEY, CLIENT_NAME_KEY)
195203
.build());
204+
defineView(
205+
views,
206+
OUTSTANDING_RPCS_PER_CHANNEL_NAME,
207+
AGGREGATION_OUTSTANDING_RPCS_HISTOGRAM,
208+
InstrumentType.HISTOGRAM,
209+
"1",
210+
ImmutableSet.<AttributeKey>builder()
211+
.add(BIGTABLE_PROJECT_ID_KEY, INSTANCE_ID_KEY, APP_PROFILE_KEY, CLIENT_NAME_KEY)
212+
.build());
196213
return views.build();
197214
}
198215

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/OutstandingRpcsMetricTracker.java renamed to google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/ChannelPoolMetricsTracker.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package com.google.cloud.bigtable.data.v2.stub.metrics;
1717

18-
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.METER_NAME;
19-
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.OUTSTANDING_RPCS_PER_CHANNEL_NAME;
18+
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.*;
2019

20+
import com.google.api.core.InternalApi;
2121
import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelObserver;
2222
import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelPoolObserver;
2323
import io.opentelemetry.api.OpenTelemetry;
@@ -29,25 +29,29 @@
2929
import java.util.concurrent.ScheduledFuture;
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.concurrent.atomic.AtomicReference;
32+
import java.util.logging.Logger;
3233
import javax.annotation.Nullable;
3334

34-
public class OutstandingRpcsMetricTracker implements Runnable {
35+
@InternalApi("For internal use only")
36+
public class ChannelPoolMetricsTracker implements Runnable {
37+
private static final Logger logger = Logger.getLogger(ChannelPoolMetricsTracker.class.getName());
38+
3539
private static final int SAMPLING_PERIOD_SECONDS = 60;
3640
private final LongHistogram outstandingRpcsHistogram;
41+
private final LongHistogram perConnectionErrorCountHistogram;
42+
3743
private final AtomicReference<BigtableChannelPoolObserver> bigtableChannelInsightsProviderRef =
3844
new AtomicReference<>();
39-
private final AtomicReference<String> lbPolicyRef = new AtomicReference<>();
40-
41-
// Base attributes common to all recordings
42-
private static final Attributes TRANSPORT_GRPC_ATTR =
43-
Attributes.builder().put("transport_type", "grpc").build();
45+
private final AtomicReference<String> lbPolicyRef = new AtomicReference<>("ROUND_ROBIN");
46+
private final Attributes commonAttrs;
4447

4548
// Attributes for unary and streaming RPCs, built on demand in run()
4649
@Nullable private Attributes unaryAttributes;
4750
@Nullable private Attributes streamingAttributes;
4851

49-
public OutstandingRpcsMetricTracker(OpenTelemetry openTelemetry) {
52+
public ChannelPoolMetricsTracker(OpenTelemetry openTelemetry, Attributes commonAttrs) {
5053
Meter meter = openTelemetry.getMeter(METER_NAME);
54+
this.commonAttrs = commonAttrs;
5155
this.outstandingRpcsHistogram =
5256
meter
5357
.histogramBuilder(OUTSTANDING_RPCS_PER_CHANNEL_NAME)
@@ -56,6 +60,14 @@ public OutstandingRpcsMetricTracker(OpenTelemetry openTelemetry) {
5660
"A distribution of the number of outstanding RPCs per connection in the client pool, sampled periodically.")
5761
.setUnit("1")
5862
.build();
63+
64+
this.perConnectionErrorCountHistogram =
65+
meter
66+
.histogramBuilder(PER_CONNECTION_ERROR_COUNT_NAME)
67+
.ofLongs()
68+
.setDescription("Distribution of counts of channels per 'error count per minute'.")
69+
.setUnit("1")
70+
.build();
5971
}
6072

6173
/**
@@ -81,16 +93,14 @@ public ScheduledFuture<?> start(ScheduledExecutorService scheduler) {
8193
public void run() {
8294
BigtableChannelPoolObserver channelInsightsProvider = bigtableChannelInsightsProviderRef.get();
8395
if (channelInsightsProvider == null) {
96+
logger.warning("No Bigtable ChannelPoolObserver available");
8497
return; // Not registered yet
8598
}
8699
String lbPolicy = lbPolicyRef.get();
87-
if (lbPolicy == null) {
88-
lbPolicy = "ROUND_ROBIN";
89-
}
90100

91-
// Build attributes if they haven't been built yet or were invalidated
101+
// Build attributes if they haven't been built yet.
92102
if (unaryAttributes == null || streamingAttributes == null) {
93-
Attributes baseAttrs = TRANSPORT_GRPC_ATTR.toBuilder().put("lb_policy", lbPolicy).build();
103+
Attributes baseAttrs = commonAttrs.toBuilder().put("lb_policy", lbPolicy).build();
94104
this.unaryAttributes = baseAttrs.toBuilder().put("streaming", false).build();
95105
this.streamingAttributes = baseAttrs.toBuilder().put("streaming", true).build();
96106
}
@@ -100,12 +110,21 @@ public void run() {
100110
return;
101111
}
102112
for (BigtableChannelObserver info : channelInsights) {
113+
String transportTypeValue = info.isAltsChannel() ? "DIRECTPATH" : "CLOUDPATH";
114+
this.unaryAttributes =
115+
this.unaryAttributes.toBuilder().put("transport_type", transportTypeValue).build();
116+
this.streamingAttributes =
117+
this.streamingAttributes.toBuilder().put("transport_type", transportTypeValue).build();
118+
103119
long currentOutstandingUnaryRpcs = info.getOutstandingUnaryRpcs();
104120
long currentOutstandingStreamingRpcs = info.getOutstandingStreamingRpcs();
105121
// Record outstanding unary RPCs with streaming=false
106122
outstandingRpcsHistogram.record(currentOutstandingUnaryRpcs, unaryAttributes);
107123
// Record outstanding streaming RPCs with streaming=true
108124
outstandingRpcsHistogram.record(currentOutstandingStreamingRpcs, streamingAttributes);
125+
126+
long errors = info.getAndResetErrorCount();
127+
perConnectionErrorCountHistogram.record(errors, commonAttrs);
109128
}
110129
}
111130
}

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

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)