Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions datareservoirio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: another suggestion - put the function name to custom properties. So that the trace name in App Insights will be the same, but the exact function could be extracted from the properties.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a valid point, I'd definitely go that route if there were other "Timer"s but since it's just these two anyway, I'm inclined to keep it as is for now :)

return result

return wrapper

return decorator

@log_decorator("exception")
@_timer
@_timer("Timer_get")
Comment thread
branislav-jenco-4ss marked this conversation as resolved.
@retry(
stop=stop_after_attempt(
4
Expand Down Expand Up @@ -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,
Expand Down
Loading