Skip to content

Commit

Permalink
Cleanup unused static method
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosmamorim committed Apr 3, 2024
1 parent 16c101a commit a7b315c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
31 changes: 6 additions & 25 deletions operator/metrics/app_metrics.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
from __future__ import annotations
import time
from functools import wraps
from aioprometheus import Summary
from aioprometheus import Summary, Counter


class AppMetrics:
# Summary: Similar to a histogram, a summary samples observations
# (usually things like request durations and response sizes).
# While it also provides a total count of observations and a sum of all observed values,
# it calculates configurable quantiles over a sliding time window.
response_time_seconds = Summary("response_time_seconds",
"Response time in seconds",
{"method": "Method used for the request",
"resource_type": "Type of resource requested"
}
)

@staticmethod
def measure_execution_time(metric_name, **labels):

def decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
metric = getattr(AppMetrics, metric_name, None)
start_time = time.time()
result = await func(*args, **kwargs)
end_time = time.time()
duration = end_time - start_time
if metric and callable(getattr(metric, 'observe', None)):
metric.observe(labels=labels, value=duration)
else:
print(f"Metric {metric_name} not found or doesn't support observe()")
return result

return wrapper

return decorator
request_handler_exceptions = Counter(
"request_handler_exceptions",
"Count of exceptions by handler function.",
{"handler": "Handler function name"}
)
6 changes: 1 addition & 5 deletions operator/resourcepool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from metrics import ResourcePoolMetrics


from datetime import timedelta
from typing import List, Mapping, Optional, TypeVar

Expand Down Expand Up @@ -213,11 +214,6 @@ async def handle_metrics(self, logger: kopf.ObjectLogger, resource_handles):
async def handle_delete(self, logger: kopf.ObjectLogger):
await resourcehandle.ResourceHandle.delete_unbound_handles_for_pool(logger=logger, resource_pool=self)

@ResourcePoolMetrics.measure_execution_time(
'response_time_seconds',
method='manage',
resource_type='resourcepool'
)
async def manage(self, logger: kopf.ObjectLogger):
async with self.lock:
resource_handles = await resourcehandle.ResourceHandle.get_unbound_handles_for_pool(resource_pool=self, logger=logger)
Expand Down

0 comments on commit a7b315c

Please sign in to comment.