@@ -56,7 +56,7 @@ class MetricAttemptTracer:
5656 direct_path_used : bool
5757 status : str
5858
59- def __init__ (self ):
59+ def __init__ (self ) -> None :
6060 """
6161 Initialize a MetricAttemptTracer instance with default values.
6262
@@ -181,8 +181,11 @@ class should not have any knowledge about the observability framework used for m
181181 _instrument_attempt_latency : "Histogram"
182182 _instrument_operation_counter : "Counter"
183183 _instrument_operation_latency : "Histogram"
184+ _instrument_gfe_latency : "Histogram"
185+ _instrument_gfe_missing_header_count : "Counter"
184186 current_op : MetricOpTracer
185187 enabled : bool
188+ gfe_enabled : bool
186189 method : str
187190
188191 def __init__ (
@@ -193,21 +196,23 @@ def __init__(
193196 instrument_operation_latency : "Histogram" ,
194197 instrument_operation_counter : "Counter" ,
195198 client_attributes : Dict [str , str ],
199+ gfe_enabled : bool = False ,
196200 ):
197201 """
198202 Initialize a MetricsTracer instance with the given parameters.
199203
200- This constructor initializes a MetricsTracer instance with the provided method name, enabled status, direct path enabled status,
201- instrumented metrics for attempt latency, attempt counter, operation latency, operation counter, and client attributes.
202- It sets up the necessary metrics tracing infrastructure for recording metrics related to RPC operations.
204+ This constructor sets up a MetricsTracer instance with the specified parameters, including the enabled status,
205+ instruments for measuring and counting attempt and operation metrics, and client attributes. It prepares the
206+ infrastructure needed for recording metrics related to RPC operations.
203207
204208 Args:
205- enabled (bool): A flag indicating if metrics tracing is enabled.
206- instrument_attempt_latency (Histogram): The instrument for measuring attempt latency.
207- instrument_attempt_counter (Counter): The instrument for counting attempts.
208- instrument_operation_latency (Histogram): The instrument for measuring operation latency.
209- instrument_operation_counter (Counter): The instrument for counting operations.
210- client_attributes (dict[str, str]): A dictionary of client attributes used for metrics tracing.
209+ enabled (bool): Indicates if metrics tracing is enabled.
210+ instrument_attempt_latency (Histogram): Instrument for measuring attempt latency.
211+ instrument_attempt_counter (Counter): Instrument for counting attempts.
212+ instrument_operation_latency (Histogram): Instrument for measuring operation latency.
213+ instrument_operation_counter (Counter): Instrument for counting operations.
214+ client_attributes (Dict[str, str]): Dictionary of client attributes used for metrics tracing.
215+ gfe_enabled (bool, optional): Indicates if GFE metrics are enabled. Defaults to False.
211216 """
212217 self .current_op = MetricOpTracer ()
213218 self ._client_attributes = client_attributes
@@ -216,6 +221,7 @@ def __init__(
216221 self ._instrument_operation_latency = instrument_operation_latency
217222 self ._instrument_operation_counter = instrument_operation_counter
218223 self .enabled = enabled
224+ self .gfe_enabled = gfe_enabled
219225
220226 @staticmethod
221227 def _get_ms_time_diff (start : datetime , end : datetime ) -> float :
@@ -322,7 +328,7 @@ def record_attempt_completion(self, status: str = StatusCode.OK.name) -> None:
322328
323329 If metrics tracing is not enabled, this method does not perform any operations.
324330 """
325- if not self .enabled :
331+ if not self .enabled or not HAS_OPENTELEMETRY_INSTALLED :
326332 return
327333 self .current_op .current_attempt .status = status
328334
@@ -347,7 +353,7 @@ def record_operation_start(self) -> None:
347353 It is used to track the start time of an operation, which is essential for calculating operation latency and other metrics.
348354 If metrics tracing is not enabled, this method does not perform any operations.
349355 """
350- if not self .enabled :
356+ if not self .enabled or not HAS_OPENTELEMETRY_INSTALLED :
351357 return
352358 self .current_op .start ()
353359
@@ -360,7 +366,7 @@ def record_operation_completion(self) -> None:
360366 Additionally, it increments the operation count and records the attempt count for the operation.
361367 If metrics tracing is not enabled, this method does not perform any operations.
362368 """
363- if not self .enabled :
369+ if not self .enabled or not HAS_OPENTELEMETRY_INSTALLED :
364370 return
365371 end_time = datetime .now ()
366372 # Build Attributes
@@ -385,14 +391,37 @@ def record_operation_completion(self) -> None:
385391 self .current_op .attempt_count , attributes = attempt_attributes
386392 )
387393
394+ def record_gfe_latency (self , latency : int ) -> None :
395+ """
396+ Records the GFE latency using the Histogram instrument.
397+
398+ Args:
399+ latency (int): The latency duration to be recorded.
400+ """
401+ if not self .enabled or not HAS_OPENTELEMETRY_INSTALLED or not self .gfe_enabled :
402+ return
403+ self ._instrument_gfe_latency .record (
404+ amount = latency , attributes = self .client_attributes
405+ )
406+
407+ def record_gfe_missing_header_count (self ) -> None :
408+ """
409+ Increments the counter for missing GFE headers.
410+ """
411+ if not self .enabled or not HAS_OPENTELEMETRY_INSTALLED or not self .gfe_enabled :
412+ return
413+ self ._instrument_gfe_missing_header_count .add (
414+ amount = 1 , attributes = self .client_attributes
415+ )
416+
388417 def _create_operation_otel_attributes (self ) -> dict :
389418 """
390419 Create additional attributes for operation metrics tracing.
391420
392421 This method populates the client attributes dictionary with the operation status if metrics tracing is enabled.
393422 It returns the updated client attributes dictionary.
394423 """
395- if not self .enabled :
424+ if not self .enabled or not HAS_OPENTELEMETRY_INSTALLED :
396425 return {}
397426 attributes = self ._client_attributes .copy ()
398427 attributes [METRIC_LABEL_KEY_STATUS ] = self .current_op .status
@@ -405,7 +434,7 @@ def _create_attempt_otel_attributes(self) -> dict:
405434 This method populates the attributes dictionary with the attempt status if metrics tracing is enabled and an attempt exists.
406435 It returns the updated attributes dictionary.
407436 """
408- if not self .enabled :
437+ if not self .enabled or not HAS_OPENTELEMETRY_INSTALLED :
409438 return {}
410439
411440 attributes = self ._client_attributes .copy ()
0 commit comments