-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Observability via Prometheus Metrics (#92)
Closes #57 --------- Co-authored-by: Benjamin Smith <[email protected]>
- Loading branch information
1 parent
d9a7e65
commit 224de6e
Showing
13 changed files
with
229 additions
and
11 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
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 |
---|---|---|
|
@@ -10,3 +10,6 @@ __pycache__/ | |
.mypy_cache | ||
.coverage | ||
coverage.xml | ||
|
||
# Metrics | ||
grafana |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
global: | ||
scrape_interval: 15s | ||
evaluation_interval: 15s | ||
|
||
scrape_configs: | ||
- job_name: 'prometheus' | ||
static_configs: | ||
- targets: ['localhost:9090'] | ||
|
||
- job_name: 'pushgateway' | ||
static_configs: | ||
- targets: ['pushgateway:9091'] |
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
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,81 @@ | ||
"""Handle submitting metrics, logs and other interesting details about jobs.""" | ||
|
||
import uuid | ||
from collections.abc import Awaitable, Callable, Iterable, Mapping | ||
from functools import wraps | ||
from os import getenv as env | ||
from time import perf_counter | ||
from typing import Any | ||
|
||
from prometheus_client import CollectorRegistry, Counter, Gauge, push_to_gateway | ||
|
||
from src.interfaces import Named | ||
from src.logger import log | ||
|
||
|
||
def log_job_metrics(prometheus_url: str, job_metrics: dict[str, Any]) -> None: | ||
"""Log metrics about a job to a prometheus pushgateway.""" | ||
registry = CollectorRegistry() | ||
log.info("Pushing metrics to Prometheus") | ||
|
||
job_success_timestamp = Gauge( | ||
name="job_last_success_unixtime", | ||
documentation="Unix timestamp of job end", | ||
registry=registry, | ||
) | ||
job_success_timestamp.set_to_current_time() | ||
|
||
job_failure_counter = Counter( | ||
name="job_failure_count", | ||
documentation="Number of failed jobs", | ||
registry=registry, | ||
) | ||
job_failure_counter.inc(int(not job_metrics["success"])) | ||
|
||
job_duration_metric = Gauge( | ||
name="job_last_success_duration", | ||
documentation="How long did the job take to run (in seconds)", | ||
registry=registry, | ||
) | ||
job_duration_metric.set(job_metrics["duration"]) | ||
push_to_gateway( | ||
gateway=prometheus_url, | ||
job=f'dune-sync-{job_metrics["name"]}', | ||
registry=registry, | ||
) | ||
|
||
|
||
def collect_metrics( | ||
func: Callable[..., Awaitable[Any]], | ||
) -> Callable[..., Awaitable[Any]]: | ||
"""Collect and submit metrics about a Job if a pushgateway is configured.""" | ||
|
||
@wraps(func) | ||
async def wrapper( | ||
self: Named, *args: Iterable[Any], **kwargs: Mapping[Any, Any] | ||
) -> Any: | ||
if not (prometheus_url := env("PROMETHEUS_PUSHGATEWAY_URL")): | ||
return await func(self, *args, **kwargs) | ||
|
||
run_id = uuid.uuid4().hex | ||
start = perf_counter() | ||
success = False | ||
|
||
try: | ||
result = await func(self, *args, **kwargs) | ||
success = True | ||
return result | ||
except Exception: | ||
success = False | ||
raise | ||
finally: | ||
duration = perf_counter() - start | ||
metrics = { | ||
"duration": duration, | ||
"name": self.name, | ||
"run_id": run_id, | ||
"success": success, | ||
} | ||
log_job_metrics(prometheus_url, metrics) | ||
|
||
return wrapper |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
|
||
from src.metrics import log_job_metrics | ||
|
||
|
||
class TestMetrics(unittest.TestCase): | ||
@patch("src.metrics.push_to_gateway") | ||
def test_log_job_metrics(self, mock_push): | ||
job = MagicMock() | ||
job.name = "mock-job" | ||
|
||
log_job_metrics( | ||
"https://localhost:9090", | ||
{"duration": 1, "job": job, "success": False, "name": job.name}, | ||
) | ||
self.assertEqual(1, mock_push.call_count) | ||
self.assertEqual( | ||
"https://localhost:9090", mock_push.mock_calls[0].kwargs["gateway"] | ||
) | ||
self.assertEqual("dune-sync-mock-job", mock_push.mock_calls[0].kwargs["job"]) |