1515 */
1616package 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 ;
2121import com .google .cloud .bigtable .gaxx .grpc .BigtableChannelObserver ;
2222import com .google .cloud .bigtable .gaxx .grpc .BigtableChannelPoolObserver ;
2323import io .opentelemetry .api .OpenTelemetry ;
2929import java .util .concurrent .ScheduledFuture ;
3030import java .util .concurrent .TimeUnit ;
3131import java .util .concurrent .atomic .AtomicReference ;
32+ import java .util .logging .Logger ;
3233import 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}
0 commit comments