-
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.
Add Monitoring Metrics with aioprometheus for ResourcePools
- Loading branch information
1 parent
6d45e84
commit 5631d11
Showing
9 changed files
with
223 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .app_metrics import AppMetrics # noqa F401 | ||
from .metrics_manager import MetricsManager # noqa F401 | ||
from .metrics_service import MetricsService # noqa F401 | ||
from .resourcepool_metrics import ResourcePoolMetrics # noqa F401 |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
import time | ||
from functools import wraps | ||
from aioprometheus import Summary | ||
|
||
|
||
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 |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from __future__ import annotations | ||
from aioprometheus import REGISTRY, Counter, Gauge | ||
|
||
from .app_metrics import AppMetrics | ||
from .resourcepool_metrics import ResourcePoolMetrics | ||
|
||
|
||
class MetricsManager: | ||
metrics_classes = [AppMetrics, ResourcePoolMetrics] | ||
|
||
@classmethod | ||
def register(cls): | ||
for metrics_class in cls.metrics_classes: | ||
for attr_name in dir(metrics_class): | ||
attr = getattr(metrics_class, attr_name) | ||
if isinstance(attr, (Counter, Gauge)): | ||
if attr.name not in REGISTRY.collectors: | ||
REGISTRY.register(attr) |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
import os | ||
import logging | ||
from aioprometheus.service import Service | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class MetricsService: | ||
service = Service() | ||
|
||
@classmethod | ||
async def start(cls, addr="0.0.0.0", port=8000) -> None: | ||
port = int(os.environ.get("METRICS_PORT", port)) | ||
await cls.service.start(addr=addr, port=port, metrics_url="/metrics") | ||
logger.info(f"Serving metrics on: {cls.service.metrics_url}") | ||
|
||
@classmethod | ||
async def stop(cls) -> None: | ||
logger.info("Stopping metrics service") | ||
await cls.service.stop() |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
from aioprometheus import Counter, Gauge | ||
|
||
from metrics import AppMetrics | ||
|
||
|
||
class ResourcePoolMetrics(AppMetrics): | ||
resource_pool_labels = {'name': "Resource pool name", | ||
'namespace': "Resource pool namespace" | ||
} | ||
|
||
resource_pool_min_available = Gauge( | ||
'resource_pool_min_available', | ||
'Minimum number of available environments in each resource pool', | ||
resource_pool_labels | ||
) | ||
|
||
resource_pool_available = Gauge( | ||
'resource_pool_available', | ||
'Number of available environments in each resource pool', | ||
resource_pool_labels | ||
) | ||
|
||
resource_pool_used_total = Counter( | ||
'resource_pool_used_total', | ||
'Total number of environments used in each resource pool', | ||
resource_pool_labels | ||
) | ||
|
||
resource_pool_state_labels = {'name': "Resource pool name", | ||
'namespace': "Resource pool namespace", | ||
'state': "State of the resource pool" | ||
} | ||
resource_pool_state = Gauge( | ||
'resource_pool_state', | ||
'State of each resource pool, including available and used resources', | ||
resource_pool_state_labels | ||
) |
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
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
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
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