@@ -89,28 +89,23 @@ class MetricOpTracer:
8989 _start_time : datetime
9090 _current_attempt : MetricAttemptTracer
9191 status : str
92- direct_path_enabled : bool
9392
9493 def __init__ (self , is_direct_path_enabled : bool = False ):
9594 """
9695 Initialize a MetricOpTracer instance with the given parameters.
9796
98- This constructor sets up a MetricOpTracer instance with the provided metric name, instrumentations for attempt latency,
99- attempt counter, operation latency, operation counter, and a flag indicating whether the direct path is enabled.
100- It initializes the method name, start time, attempt count, current attempt, direct path enabled status, and status of the metric operation.
97+ This constructor sets up a MetricOpTracer instance with the provided instrumentations for attempt latency,
98+ attempt counter, operation latency and operation counter.
10199
102100 Args:
103- metric_name (str): The name of the metric operation.
104101 instrument_attempt_latency (Histogram): The instrumentation for measuring attempt latency.
105102 instrument_attempt_counter (Counter): The instrumentation for counting attempts.
106103 instrument_operation_latency (Histogram): The instrumentation for measuring operation latency.
107104 instrument_operation_counter (Counter): The instrumentation for counting operations.
108- is_direct_path_enabled (bool, optional): A flag indicating whether the direct path is enabled. Defaults to False.
109105 """
110106 self ._attempt_count = 0
111107 self ._start_time = datetime .now ()
112108 self ._current_attempt = None
113- self .direct_path_enabled = is_direct_path_enabled
114109 self .status = ""
115110
116111 @property
@@ -159,15 +154,15 @@ def increment_attempt_count(self):
159154
160155 def start (self ):
161156 """
162- Sets the start time of the metric operation to the current time.
157+ Set the start time of the metric operation to the current time.
163158
164159 This method updates the start time of the metric operation to the current time, indicating the operation has started.
165160 """
166161 self ._start_time = datetime .now ()
167162
168163 def new_attempt (self ):
169164 """
170- Initializes a new MetricAttemptTracer instance for the current metric operation.
165+ Initialize a new MetricAttemptTracer instance for the current metric operation.
171166
172167 This method sets up a new MetricAttemptTracer instance, indicating a new attempt is being made within the metric operation.
173168 """
@@ -193,9 +188,7 @@ class should not have any knowledge about the observability framework used for m
193188
194189 def __init__ (
195190 self ,
196- method : str ,
197191 enabled : bool ,
198- is_direct_path_enabled : bool ,
199192 instrument_attempt_latency : Histogram ,
200193 instrument_attempt_counter : Counter ,
201194 instrument_operation_latency : Histogram ,
@@ -210,17 +203,14 @@ def __init__(
210203 It sets up the necessary metrics tracing infrastructure for recording metrics related to RPC operations.
211204
212205 Args:
213- method (str): The name of the method for which metrics are being traced.
214206 enabled (bool): A flag indicating if metrics tracing is enabled.
215- is_direct_path_enabled (bool): A flag indicating if the direct path is enabled for metrics tracing.
216207 instrument_attempt_latency (Histogram): The instrument for measuring attempt latency.
217208 instrument_attempt_counter (Counter): The instrument for counting attempts.
218209 instrument_operation_latency (Histogram): The instrument for measuring operation latency.
219210 instrument_operation_counter (Counter): The instrument for counting operations.
220211 client_attributes (dict[str, str]): A dictionary of client attributes used for metrics tracing.
221212 """
222- self .method = method
223- self .current_op = MetricOpTracer (is_direct_path_enabled = is_direct_path_enabled )
213+ self .current_op = MetricOpTracer ()
224214 self ._client_attributes = client_attributes
225215 self ._instrument_attempt_latency = instrument_attempt_latency
226216 self ._instrument_attempt_counter = instrument_attempt_counter
@@ -396,63 +386,31 @@ def record_operation_completion(self) -> None:
396386 self .current_op .attempt_count , attributes = attempt_attributes
397387 )
398388
399- def _create_otel_attributes (self ) -> Dict [ str , str ] :
389+ def _create_operation_otel_attributes (self ) -> dict :
400390 """
401- Create a dictionary of attributes for OpenTelemetry metrics tracing.
391+ Create additional attributes for operation metrics tracing.
402392
403- This method initializes a copy of the client attributes and adds specific operation attributes
404- such as the method name, direct path enabled status, and direct path used status.
405- These attributes are used to provide context to the metrics being traced.
406- If metrics tracing is not enabled, this method does not perform any operations.
407- Returns:
408- dict[str, str]: A dictionary of attributes for OpenTelemetry metrics tracing.
393+ This method populates the client attributes dictionary with the operation status if metrics tracing is enabled.
394+ It returns the updated client attributes dictionary.
409395 """
410396 if not self .enabled :
411- return
412-
413- self .client_attributes [METRIC_LABEL_KEY_METHOD ] = self .method
414- self .client_attributes [METRIC_LABEL_KEY_DIRECT_PATH_ENABLED ] = str (
415- self .current_op .direct_path_enabled
416- )
417- if self .current_op .current_attempt is not None :
418- self .client_attributes [METRIC_LABEL_KEY_DIRECT_PATH_USED ] = str (
419- self .current_op .current_attempt .direct_path_used
420- )
421-
422- return self .client_attributes
397+ return {}
423398
424- def _create_operation_otel_attributes (self ) -> Dict [str , str ]:
425- """
426- Create a dictionary of attributes specific to an operation for OpenTelemetry metrics tracing.
427-
428- This method builds upon the base attributes created by `_create_otel_attributes` and adds the operation status.
429- These attributes are used to provide context to the metrics being traced for a specific operation.
430- If metrics tracing is not enabled, this method does not perform any operations.
431-
432- Returns:
433- dict[str, str]: A dictionary of attributes specific to an operation for OpenTelemetry metrics tracing.
434- """
435- if not self .enabled :
436- return
399+ self ._client_attributes [METRIC_LABEL_KEY_STATUS ] = self .current_op .status
400+ return self ._client_attributes
437401
438- attributes = self ._create_otel_attributes ()
439- attributes [METRIC_LABEL_KEY_STATUS ] = self .current_op .status
440- return attributes
441402
442- def _create_attempt_otel_attributes (self ) -> Dict [ str , str ] :
403+ def _create_attempt_otel_attributes (self ) -> dict :
443404 """
444- Create a dictionary of attributes specific to an attempt within an operation for OpenTelemetry metrics tracing.
405+ Create additional attributes for attempt metrics tracing.
445406
446- This method builds upon the operation attributes created by `_create_operation_otel_attributes` and adds the attempt status.
447- These attributes are used to provide context to the metrics being traced for a specific attempt within an operation.
448- If metrics tracing is not enabled, this method does not perform any operations.
449- Returns:
450- dict[str, str]: A dictionary of attributes specific to an attempt within an operation for OpenTelemetry metrics tracing.
407+ This method populates the attributes dictionary with the attempt status if metrics tracing is enabled and an attempt exists.
408+ It returns the updated attributes dictionary.
451409 """
452410 if not self .enabled :
453- return
411+ return {}
454412
455- attributes = self . _create_operation_otel_attributes ()
413+ attributes = {}
456414 # Short circuit out if we don't have an attempt
457415 if self .current_op .current_attempt is not None :
458416 attributes [METRIC_LABEL_KEY_STATUS ] = self .current_op .current_attempt .status
@@ -573,6 +531,20 @@ def set_database(self, database: str) -> "MetricsTracer":
573531 self ._client_attributes [METRIC_LABEL_KEY_DATABASE ] = database
574532 return self
575533
534+ def set_method (self , method : str ) -> "MetricsTracer" :
535+ """
536+ Set the method attribute for metrics tracing.
537+
538+ This method updates the method attribute in the client attributes dictionary for metrics tracing purposes.
539+ If the database attribute already has a value, this method does nothing and returns.
540+
541+ :param method: The method name to set.
542+ :return: This instance of MetricsTracer for method chaining.
543+ """
544+ if METRIC_LABEL_KEY_METHOD not in self ._client_attributes :
545+ self .client_attributes [METRIC_LABEL_KEY_METHOD ] = method
546+ return self
547+
576548 def enable_direct_path (self , enable : bool = False ) -> "MetricsTracer" :
577549 """
578550 Enable or disable the direct path for metrics tracing.
0 commit comments