@@ -159,38 +159,50 @@ private void processHeader(
159159 // updated to handle multiple metrics gracefully.
160160
161161 Map <String , Float > serverTimingMetrics = parseServerTimingHeader (serverTiming );
162- if (serverTimingMetrics .containsKey (GFE_TIMING_HEADER )) {
163- float gfeLatency = serverTimingMetrics .get (GFE_TIMING_HEADER );
162+ Float gfeLatency = serverTimingMetrics .get (GFE_TIMING_HEADER );
163+ boolean isAfeEnabled = GapicSpannerRpc .isEnableAFEServerTiming ();
164+ Float afeLatency = isAfeEnabled ? serverTimingMetrics .get (AFE_TIMING_HEADER ) : null ;
164165
165- measureMap .put (SPANNER_GFE_LATENCY , (long ) gfeLatency );
166+ // Record OpenCensus and Custom OpenTelemetry Metrics
167+ if (gfeLatency != null ) {
168+ long gfeVal = gfeLatency .longValue ();
169+ measureMap .put (SPANNER_GFE_LATENCY , gfeVal );
166170 measureMap .put (SPANNER_GFE_HEADER_MISSING_COUNT , 0L );
167- measureMap .record (tagContext );
168-
169- spannerRpcMetrics .recordGfeLatency ((long ) gfeLatency , attributes );
171+ spannerRpcMetrics .recordGfeLatency (gfeVal , attributes );
170172 spannerRpcMetrics .recordGfeHeaderMissingCount (0L , attributes );
171- if (compositeTracer != null && !isDirectPathUsed ) {
172- compositeTracer .recordGFELatency (gfeLatency );
173- }
174- if (span != null ) {
175- span .setAttribute ("gfe_latency" , String .valueOf (gfeLatency ));
176- }
177173 } else {
178- measureMap .put (SPANNER_GFE_HEADER_MISSING_COUNT , 1L ). record ( tagContext ) ;
174+ measureMap .put (SPANNER_GFE_HEADER_MISSING_COUNT , 1L );
179175 spannerRpcMetrics .recordGfeHeaderMissingCount (1L , attributes );
180- if (compositeTracer != null && !isDirectPathUsed ) {
181- compositeTracer .recordGfeHeaderMissingCount (1L );
176+ }
177+ measureMap .record (tagContext );
178+
179+ // Record Built-in Metrics
180+ if (compositeTracer != null ) {
181+ // GFE Latency Metrics
182+ if (!isDirectPathUsed ) {
183+ if (gfeLatency != null ) {
184+ compositeTracer .recordGFELatency (gfeLatency );
185+ } else {
186+ compositeTracer .recordGfeHeaderMissingCount (1L );
187+ }
188+ }
189+ // AFE Tracing
190+ if (isAfeEnabled ) {
191+ if (afeLatency != null ) {
192+ compositeTracer .recordAFELatency (afeLatency );
193+ } else {
194+ compositeTracer .recordAfeHeaderMissingCount (1L );
195+ }
182196 }
183197 }
184198
185- // Record AFE metrics
186- if (compositeTracer != null && GapicSpannerRpc .isEnableAFEServerTiming ()) {
187- if (serverTimingMetrics .containsKey (AFE_TIMING_HEADER )) {
188- float afeLatency = serverTimingMetrics .get (AFE_TIMING_HEADER );
189- compositeTracer .recordAFELatency (afeLatency );
190- } else {
191- // Disable afe_connectivity_error_count metric as AFE header is disabled in backend
192- // currently.
193- // compositeTracer.recordAfeHeaderMissingCount(1L);
199+ // Record Span Attributes
200+ if (span != null ) {
201+ if (gfeLatency != null ) {
202+ span .setAttribute ("gfe_latency" , gfeLatency .toString ());
203+ }
204+ if (afeLatency != null ) {
205+ span .setAttribute ("afe_latency" , afeLatency .toString ());
194206 }
195207 }
196208 } catch (NumberFormatException e ) {
0 commit comments