-
Notifications
You must be signed in to change notification settings - Fork 2
Improve metrics #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rcvalerio
wants to merge
3
commits into
dev
Choose a base branch
from
rv-add-prometheus-grafana
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Improve metrics #234
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| """Prometheus metrics server for file-tracker service.""" | ||
| import logging | ||
| import os | ||
|
|
||
| from aiohttp import web | ||
| from prometheus_client import generate_latest | ||
|
|
||
|
|
||
| async def metrics_handler(request): | ||
| """Handler for /metrics endpoint exposing Prometheus client metrics.""" | ||
| return web.Response(body=generate_latest(), content_type="text/plain") | ||
|
|
||
|
|
||
| async def start_metrics_server(host='0.0.0.0', port=None): | ||
| """Start a basic Prometheus metrics server exposing client metrics.""" | ||
| port = port or int(os.getenv('FILE_TRACKER_METRICS_PORT', '9091')) | ||
| app = web.Application() | ||
| app.router.add_get('/metrics', metrics_handler) | ||
| runner = web.AppRunner(app) | ||
| await runner.setup() | ||
| site = web.TCPSite(runner, host, port) | ||
| await site.start() | ||
| logging.info("Metrics server started on %s:%s", host, port) | ||
| return runner |
This file contains hidden or 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,2 +1,3 @@ | ||
| aiortc | ||
| aiohttp | ||
| aiohttp | ||
| prometheus-client |
This file contains hidden or 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,16 @@ | ||
| global: | ||
| scrape_interval: 15s | ||
| evaluation_interval: 15s | ||
|
|
||
| scrape_configs: | ||
| - job_name: "file-tracker" | ||
| static_configs: | ||
| - targets: ["localhost:9091"] | ||
| metrics_path: "/metrics" | ||
|
|
||
| - job_name: "task-runner-lite" | ||
| static_configs: | ||
| - targets: ["localhost:8000"] | ||
| metrics_path: "/metrics" | ||
|
|
||
|
|
This file contains hidden or 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,4 @@ gputil | |
| pydantic~=2.11.7 | ||
| tenacity | ||
| pytest | ||
| prometheus-client | ||
This file contains hidden or 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 hidden or 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 @@ | ||
| """Prometheus metrics server for task-runner service.""" | ||
| import logging | ||
| import os | ||
|
|
||
| from prometheus_client import Counter, Gauge, Histogram, start_http_server | ||
|
|
||
| # Task metrics | ||
| tasks_active = Gauge('tasks_active', 'Number of currently active tasks') | ||
| tasks_total = Counter('tasks_total', 'Total number of tasks', ['status']) | ||
| task_duration = Histogram('task_duration_seconds', | ||
| 'Task execution duration in seconds') | ||
|
|
||
|
|
||
| def start_metrics_server(port=None): | ||
| """Start a basic Prometheus metrics server exposing client metrics.""" | ||
| port = port or int(os.getenv('TASK_RUNNER_METRICS_PORT', '8000')) | ||
| start_http_server(port) | ||
| logging.info("Metrics server started on port %s", port) | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -30,10 +30,11 @@ | |
| executers, | ||
| observers, | ||
| task_message_listener, | ||
| task_status, | ||
| utils, | ||
| ) | ||
| from task_runner.metrics import task_duration, tasks_active, tasks_total | ||
| from task_runner.operations_logger import OperationName, OperationsLogger | ||
| from task_runner.task_status import task_status | ||
| from task_runner.utils import files | ||
|
|
||
| KILL_MESSAGE = "kill" | ||
|
|
@@ -287,6 +288,10 @@ def __call__(self, request: dict[str, str]) -> None: | |
| Args: | ||
| request: Request describing the task to be executed. | ||
| """ | ||
| task_start_time = time.time() | ||
| task_status_str = 'failed' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could use a |
||
| tasks_active.inc() | ||
|
|
||
| # Save the task request to use during output recovery | ||
| with open(self.request_path, "w", encoding="utf-8") as request_file: | ||
| json.dump(request, request_file, default=str) | ||
|
|
@@ -416,12 +421,15 @@ def __call__(self, request: dict[str, str]) -> None: | |
|
|
||
| if exit_reason == TaskExitReason.KILLED: | ||
| new_status = task_status.TaskStatusCode.KILLED.value | ||
| task_status_str = 'killed' | ||
| elif exit_reason == TaskExitReason.TTL_EXCEEDED: | ||
| new_status = task_status.TaskStatusCode.TTL_EXCEEDED.value | ||
| task_status_str = 'ttl_exceeded' | ||
| else: | ||
| new_status = (task_status.TaskStatusCode.SUCCESS.value | ||
| if exit_code == 0 else | ||
| task_status.TaskStatusCode.FAILED.value) | ||
| task_status_str = 'success' if exit_code == 0 else 'failed' | ||
|
|
||
| safely_delete = self.save_output(new_task_status=new_status) | ||
|
|
||
|
|
@@ -436,6 +444,7 @@ def __call__(self, request: dict[str, str]) -> None: | |
|
|
||
| # Catch all exceptions to ensure that we log the error message | ||
| except Exception as e: # noqa: BLE001 | ||
| task_status_str = 'error' | ||
| message = utils.get_exception_root_cause_message(e) | ||
| try: | ||
| self._publish_event( | ||
|
|
@@ -461,6 +470,11 @@ def __call__(self, request: dict[str, str]) -> None: | |
| safely_delete = False | ||
|
|
||
| finally: | ||
| # Track metrics | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really nice -- we should move all metrics (e.g., system metrics) to Prometheus |
||
| task_duration.observe(time.time() - task_start_time) | ||
| tasks_total.labels(status=task_status_str).inc() | ||
| tasks_active.dec() | ||
|
|
||
| self.cleaning_up = True | ||
| self._cleanup(safely_delete) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will always be 1 or 0 right?