diff --git a/datareservoirio/client.py b/datareservoirio/client.py index 079d37f7..22c7ca4c 100644 --- a/datareservoirio/client.py +++ b/datareservoirio/client.py @@ -316,40 +316,49 @@ def delete(self, series_id): timeout=_TIMEOUT_DEAULT, ) - def _timer(func): - """Decorator used to log latency of the ``get`` and ``get_samples_aggregate`` method""" - - @wraps(func) - def wrapper(self, series_id, start=None, end=None, **kwargs): - start_time = time.perf_counter() - result = func(self, series_id, start=start, end=end, **kwargs) - end_time = time.perf_counter() - elapsed_time = end_time - start_time - start_date_as_str = None - end_date_as_str = None - if start: - start_date_as_str = pd.to_datetime( - start, dayfirst=True, unit="ns", utc=True - ).isoformat() - if end: - end_date_as_str = pd.to_datetime( - end, dayfirst=True, unit="ns", utc=True - ).isoformat() - number_of_samples = len(result) - properties = { - "series_id": series_id, - "start": start_date_as_str, - "end": end_date_as_str, - "elapsed": elapsed_time, - "number-of-samples": number_of_samples, - } - metric().info("Timer", extra=properties) - return result - - return wrapper + def _timer(metric_name="Timer"): + """Decorator factory used to log latency for the ``get`` and ``get_samples_aggregate`` methods. + + Parameters + ---------- + metric_name : str + Metric name to emit (use a stable, low-cardinality value). + """ + + def decorator(func): + @wraps(func) + def wrapper(self, series_id, start=None, end=None, **kwargs): + start_time = time.perf_counter() + result = func(self, series_id, start=start, end=end, **kwargs) + end_time = time.perf_counter() + elapsed_time = end_time - start_time + start_date_as_str = None + end_date_as_str = None + if start: + start_date_as_str = pd.to_datetime( + start, dayfirst=True, unit="ns", utc=True + ).isoformat() + if end: + end_date_as_str = pd.to_datetime( + end, dayfirst=True, unit="ns", utc=True + ).isoformat() + number_of_samples = len(result) + properties = { + "series_id": series_id, + "start": start_date_as_str, + "end": end_date_as_str, + "elapsed": elapsed_time, + "number-of-samples": number_of_samples, + } + metric().info(metric_name, extra=properties) + return result + + return wrapper + + return decorator @log_decorator("exception") - @_timer + @_timer("Timer_get") @retry( stop=stop_after_attempt( 4 @@ -455,7 +464,7 @@ def get( return series @log_decorator("exception") - @_timer + @_timer("Timer_get_samples_aggregate") @log_decorator("warning") def get_samples_aggregate( self,