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

Commit 39a92f6

Browse files
committed
wip
1 parent 672b78f commit 39a92f6

7 files changed

Lines changed: 310 additions & 24 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static BigtableClientContext create(EnhancedBigtableStubSettings settings
106106
errorCountPerConnectionMetricTracker =
107107
setupPerConnectionErrorTracer(builder, transportProvider, internalOtel);
108108

109-
outstandingRpcsMetricTracker = new OutstandingRpcsMetricTracker(internalOtel, "LB_POLICY");
109+
outstandingRpcsMetricTracker = new OutstandingRpcsMetricTracker(internalOtel);
110110

111111
// Configure grpc metrics
112112
configureGrpcOtel(transportProvider, internalOtel);

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

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.METER_NAME;
1919
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.OUTSTANDING_RPCS_PER_CHANNEL_NAME;
2020

21-
import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelInsight;
22-
import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelInsightsProvider;
21+
import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelObserver;
22+
import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelPoolObserver;
2323
import io.opentelemetry.api.OpenTelemetry;
2424
import io.opentelemetry.api.common.Attributes;
2525
import io.opentelemetry.api.metrics.LongHistogram;
@@ -29,15 +29,24 @@
2929
import java.util.concurrent.ScheduledFuture;
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.concurrent.atomic.AtomicReference;
32+
import javax.annotation.Nullable;
3233

3334
public class OutstandingRpcsMetricTracker implements Runnable {
3435
private static final int SAMPLING_PERIOD_SECONDS = 60;
3536
private final LongHistogram outstandingRpcsHistogram;
36-
private final Attributes baseAttributes;
37-
private final AtomicReference<BigtableChannelInsightsProvider>
37+
private final AtomicReference<BigtableChannelPoolObserver>
3838
bigtableChannelInsightsProviderRef = new AtomicReference<>();
39+
private final AtomicReference<String> lbPolicyRef = new AtomicReference<>();
3940

40-
public OutstandingRpcsMetricTracker(OpenTelemetry openTelemetry, String lbPolicy) {
41+
// Base attributes common to all recordings
42+
private static final Attributes TRANSPORT_GRPC_ATTR =
43+
Attributes.builder().put("transport_type", "grpc").build();
44+
45+
// Attributes for unary and streaming RPCs, built on demand in run()
46+
@Nullable private Attributes unaryAttributes;
47+
@Nullable private Attributes streamingAttributes;
48+
49+
public OutstandingRpcsMetricTracker(OpenTelemetry openTelemetry) {
4150
Meter meter = openTelemetry.getMeter(METER_NAME);
4251
this.outstandingRpcsHistogram =
4352
meter
@@ -47,20 +56,22 @@ public OutstandingRpcsMetricTracker(OpenTelemetry openTelemetry, String lbPolicy
4756
"A distribution of the number of outstanding RPCs per connection in the client pool, sampled periodically.")
4857
.setUnit("1")
4958
.build();
50-
51-
this.baseAttributes =
52-
Attributes.builder().put("transport_type", "grpc").put("lb_policy", lbPolicy).build();
5359
}
5460

5561
/**
5662
* Registers the provider for the channel pool entries. This should be called by the component
5763
* that creates the BigtableChannelPool.
5864
*/
5965
public void registerChannelInsightsProvider(
60-
BigtableChannelInsightsProvider channelInsightsProvider) {
66+
BigtableChannelPoolObserver channelInsightsProvider) {
6167
this.bigtableChannelInsightsProviderRef.set(channelInsightsProvider);
6268
}
6369

70+
/** Register the current lb policy * */
71+
public void registerLoadBalancingStrategy(String lbPolicy) {
72+
this.lbPolicyRef.set(lbPolicy);
73+
}
74+
6475
/** Starts the periodic collection. */
6576
public ScheduledFuture<?> start(ScheduledExecutorService scheduler) {
6677
return scheduler.scheduleAtFixedRate(
@@ -69,25 +80,33 @@ public ScheduledFuture<?> start(ScheduledExecutorService scheduler) {
6980

7081
@Override
7182
public void run() {
72-
BigtableChannelInsightsProvider channelInsightsProvider =
83+
BigtableChannelPoolObserver channelInsightsProvider =
7384
bigtableChannelInsightsProviderRef.get();
7485
if (channelInsightsProvider == null) {
7586
return; // Not registered yet
7687
}
77-
List<? extends BigtableChannelInsight> channelInsights =
88+
String lbPolicy = lbPolicyRef.get();
89+
if (lbPolicy == null) {
90+
lbPolicy = "ROUND_ROBIN";
91+
}
92+
93+
// Build attributes if they haven't been built yet or were invalidated
94+
if (unaryAttributes == null || streamingAttributes == null) {
95+
Attributes baseAttrs = TRANSPORT_GRPC_ATTR.toBuilder().put("lb_policy", lbPolicy).build();
96+
this.unaryAttributes = baseAttrs.toBuilder().put("streaming", false).build();
97+
this.streamingAttributes = baseAttrs.toBuilder().put("streaming", true).build();
98+
}
99+
List<? extends BigtableChannelObserver> channelInsights =
78100
channelInsightsProvider.getChannelInfos();
79101
if (channelInsights == null || channelInsights.isEmpty()) {
80102
return;
81103
}
82-
for (BigtableChannelInsight info : channelInsights) {
83-
long currentOutstandingUnaryRpcs = info.getOutstandingStreamingRpcs();
104+
for (BigtableChannelObserver info : channelInsights) {
105+
long currentOutstandingUnaryRpcs = info.getOutstandingUnaryRpcs();
84106
long currentOutstandingStreamingRpcs = info.getOutstandingStreamingRpcs();
85107
// Record outstanding unary RPCs with streaming=false
86-
Attributes unaryAttributes = baseAttributes.toBuilder().put("streaming", false).build();
87108
outstandingRpcsHistogram.record(currentOutstandingUnaryRpcs, unaryAttributes);
88-
89109
// Record outstanding streaming RPCs with streaming=true
90-
Attributes streamingAttributes = baseAttributes.toBuilder().put("streaming", true).build();
91110
outstandingRpcsHistogram.record(currentOutstandingStreamingRpcs, streamingAttributes);
92111
}
93112
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableChannelInsight.java renamed to google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableChannelObserver.java

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

1818
import com.google.api.core.InternalApi;
1919

20-
/** Provides insights about a single channel in the channel pool. */
20+
/** Provides observability about a single channel in the channel pool. */
2121
@InternalApi
22-
public interface BigtableChannelInsight {
22+
public interface BigtableChannelObserver {
2323
/** Gets the current number of outstanding Unary RPCs on this channel. */
2424
int getOutstandingUnaryRpcs();
2525

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableChannelPool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* <p>Internal API
5858
*/
5959
@InternalApi
60-
public class BigtableChannelPool extends ManagedChannel implements BigtableChannelInsightsProvider {
60+
public class BigtableChannelPool extends ManagedChannel implements BigtableChannelPoolObserver {
6161
@VisibleForTesting
6262
static final Logger LOG = Logger.getLogger(BigtableChannelPool.class.getName());
6363

@@ -509,12 +509,12 @@ private Entry getEntry(int affinity) {
509509

510510
/** Gets the current list of BigtableChannelInsight objects. */
511511
@Override
512-
public List<? extends BigtableChannelInsight> getChannelInfos() {
512+
public List<? extends BigtableChannelObserver> getChannelInfos() {
513513
return entries.get();
514514
}
515515

516516
/** Bundles a gRPC {@link ManagedChannel} with some usage accounting. */
517-
static class Entry implements BigtableChannelInsight {
517+
static class Entry implements BigtableChannelObserver {
518518
private final ManagedChannel channel;
519519

520520
/**

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableChannelInsightsProvider.java renamed to google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableChannelPoolObserver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@InternalApi
2222
@FunctionalInterface
23-
public interface BigtableChannelInsightsProvider {
23+
public interface BigtableChannelPoolObserver {
2424
/** Gets the current list of BigtableChannelInsight objects. */
25-
List<? extends BigtableChannelInsight> getChannelInfos();
25+
List<? extends BigtableChannelObserver> getChannelInfos();
2626
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableTransportChannelProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public TransportChannel getTransportChannel() throws IOException {
147147

148148
if (outstandingRpcsMetricTracker != null) {
149149
outstandingRpcsMetricTracker.registerChannelInsightsProvider(btChannelPool::getChannelInfos);
150+
outstandingRpcsMetricTracker.registerLoadBalancingStrategy(
151+
btPoolSettings.getLoadBalancingStrategy().name());
150152
}
151153

152154
return GrpcTransportChannel.create(btChannelPool);

0 commit comments

Comments
 (0)