-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16c101a
commit a7b315c
Showing
2 changed files
with
7 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters