1818import static com .google .cloud .bigtable .data .v2 .stub .metrics .BuiltinMetricsConstants .METER_NAME ;
1919import 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 ;
2323import io .opentelemetry .api .OpenTelemetry ;
2424import io .opentelemetry .api .common .Attributes ;
2525import io .opentelemetry .api .metrics .LongHistogram ;
2929import java .util .concurrent .ScheduledFuture ;
3030import java .util .concurrent .TimeUnit ;
3131import java .util .concurrent .atomic .AtomicReference ;
32+ import javax .annotation .Nullable ;
3233
3334public 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 }
0 commit comments