3232import java .util .concurrent .TimeUnit ;
3333import java .util .concurrent .atomic .AtomicReference ;
3434import java .util .logging .Logger ;
35- import javax .annotation .Nullable ;
3635
3736@ InternalApi ("For internal use only" )
3837public class ChannelPoolMetricsTracer implements Runnable {
@@ -45,15 +44,11 @@ public class ChannelPoolMetricsTracer implements Runnable {
4544 private final AtomicReference <BigtableChannelPoolObserver > bigtableChannelInsightsProviderRef =
4645 new AtomicReference <>();
4746 private final AtomicReference <String > lbPolicyRef = new AtomicReference <>("ROUND_ROBIN" );
48- private final Attributes commonAttrs ;
4947
5048 // Attributes for unary and streaming RPCs, built on demand in run()
51- @ Nullable private Attributes unaryAttributes ;
52- @ Nullable private Attributes streamingAttributes ;
5349
54- public ChannelPoolMetricsTracer (OpenTelemetry openTelemetry , Attributes commonAttrs ) {
50+ public ChannelPoolMetricsTracer (OpenTelemetry openTelemetry ) {
5551 Meter meter = openTelemetry .getMeter (METER_NAME );
56- this .commonAttrs = commonAttrs ;
5752 this .outstandingRpcsHistogram =
5853 meter
5954 .histogramBuilder (OUTSTANDING_RPCS_PER_CHANNEL_NAME )
@@ -98,35 +93,54 @@ public void run() {
9893 logger .warning ("No Bigtable ChannelPoolObserver available" );
9994 return ; // Not registered yet
10095 }
101- String lbPolicy = lbPolicyRef .get ();
102-
103- // Build attributes if they haven't been built yet.
104- if (unaryAttributes == null || streamingAttributes == null ) {
105- Attributes baseAttrs = commonAttrs .toBuilder ().put ("lb_policy" , lbPolicy ).build ();
106- this .unaryAttributes = baseAttrs .toBuilder ().put ("streaming" , false ).build ();
107- this .streamingAttributes = baseAttrs .toBuilder ().put ("streaming" , true ).build ();
108- }
10996 List <? extends BigtableChannelObserver > channelInsights =
11097 channelInsightsProvider .getChannelInfos ();
11198 if (channelInsights == null || channelInsights .isEmpty ()) {
11299 return ;
113100 }
101+
102+ String lbPolicy = lbPolicyRef .get ();
103+
104+ // Build the four permutations once per run to avoid allocations in the channel loop
105+ Attributes dpUnaryAttrs =
106+ Attributes .builder ()
107+ .put ("transport_type" , "directpath" )
108+ .put ("streaming" , false )
109+ .put ("lb_policy" , lbPolicy )
110+ .build ();
111+ Attributes dpStreamingAttrs =
112+ Attributes .builder ()
113+ .put ("transport_type" , "directpath" )
114+ .put ("streaming" , true )
115+ .put ("lb_policy" , lbPolicy )
116+ .build ();
117+ Attributes cpUnaryAttrs =
118+ Attributes .builder ()
119+ .put ("transport_type" , "cloudpath" )
120+ .put ("streaming" , false )
121+ .put ("lb_policy" , lbPolicy )
122+ .build ();
123+ Attributes cpStreamingAttrs =
124+ Attributes .builder ()
125+ .put ("transport_type" , "cloudpath" )
126+ .put ("streaming" , true )
127+ .put ("lb_policy" , lbPolicy )
128+ .build ();
129+
114130 for (BigtableChannelObserver info : channelInsights ) {
115- String transportTypeValue = info .isAltsChannel () ? "DIRECTPATH" : "CLOUDPATH" ;
116- this .unaryAttributes =
117- this .unaryAttributes .toBuilder ().put ("transport_type" , transportTypeValue ).build ();
118- this .streamingAttributes =
119- this .streamingAttributes .toBuilder ().put ("transport_type" , transportTypeValue ).build ();
131+ Attributes unaryAttrs = info .isAltsChannel () ? dpUnaryAttrs : cpUnaryAttrs ;
132+ Attributes streamingAttrs = info .isAltsChannel () ? dpStreamingAttrs : cpStreamingAttrs ;
120133
121134 long currentOutstandingUnaryRpcs = info .getOutstandingUnaryRpcs ();
122135 long currentOutstandingStreamingRpcs = info .getOutstandingStreamingRpcs ();
123- // Record outstanding unary RPCs with streaming=false
124- outstandingRpcsHistogram . record ( currentOutstandingUnaryRpcs , unaryAttributes );
125- // Record outstanding streaming RPCs with streaming=true
126- outstandingRpcsHistogram .record (currentOutstandingStreamingRpcs , streamingAttributes );
136+
137+ // Record outstanding RPCs with the transport_type, streaming, and lb_policy attributes
138+ outstandingRpcsHistogram . record ( currentOutstandingUnaryRpcs , unaryAttrs );
139+ outstandingRpcsHistogram .record (currentOutstandingStreamingRpcs , streamingAttrs );
127140
128141 long errors = info .getAndResetErrorCount ();
129- perConnectionErrorCountHistogram .record (errors , commonAttrs );
142+ // Record errors with empty attributes as requested
143+ perConnectionErrorCountHistogram .record (errors , Attributes .empty ());
130144 }
131145 }
132146}
0 commit comments