-
-
Notifications
You must be signed in to change notification settings - Fork 591
Expose jobrunner metrics #976
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
sbidoul
wants to merge
1
commit into
OCA:19.0
Choose a base branch
from
acsone:19.0-metrics-sbi
base: 19.0
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.
+196
−4
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,16 @@ | |
| # Copyright 2015-2016 Camptocamp SA | ||
| # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) | ||
| import logging | ||
| import math | ||
| import weakref | ||
| from collections import namedtuple | ||
| from functools import total_ordering | ||
| from heapq import heappop, heappush | ||
| from weakref import WeakValueDictionary | ||
|
|
||
| from ..exception import ChannelNotFound | ||
| from ..job import CANCELLED, DONE, ENQUEUED, FAILED, PENDING, STARTED, WAIT_DEPENDENCIES | ||
| from . import metrics | ||
|
|
||
| NOT_DONE = (WAIT_DEPENDENCIES, PENDING, ENQUEUED, STARTED, FAILED) | ||
| JobSortingKey = namedtuple("SortingKey", "eta priority date_created seq") | ||
|
|
@@ -409,14 +412,70 @@ def __init__(self, name, parent, capacity=None, sequential=False, throttle=0): | |
| self.parent = parent | ||
| if self.parent: | ||
| self.parent.children[name] = self | ||
| self.parent._register_channel_gauges() | ||
| self.children = {} | ||
| self._queue = ChannelQueue() | ||
| self._running = set() | ||
| self._failed = set() | ||
| self._waiting_dependencies = set() | ||
| self._pause_until = 0 # utc seconds since the epoch | ||
| self.capacity = capacity | ||
| self.throttle = throttle # seconds | ||
| self.sequential = sequential | ||
| self._metrics_labels = None | ||
| self._register_channel_gauges() | ||
|
|
||
| def __del__(self): | ||
| self._unregister_channel_gauges() | ||
|
|
||
| def _register_channel_gauges(self) -> None: | ||
| self._unregister_channel_gauges() | ||
| self._metrics_labels = { | ||
| "channel": self.fullname, | ||
| "root": not bool(self.parent), | ||
| "leaf": not bool(self.children), | ||
| } | ||
| metrics.channel_capacity.labels(**self._metrics_labels).set_function( | ||
| weakref.proxy(self)._capacity_gauge | ||
| ) | ||
| metrics.channel_pending.labels(**self._metrics_labels).set_function( | ||
| weakref.proxy(self)._pending_gauge | ||
| ) | ||
| metrics.channel_running.labels(**self._metrics_labels).set_function( | ||
| weakref.proxy(self)._running_gauge | ||
| ) | ||
| metrics.channel_failed.labels(**self._metrics_labels).set_function( | ||
| weakref.proxy(self)._failed_gauge | ||
| ) | ||
| metrics.channel_waiting_dependencies.labels( | ||
| **self._metrics_labels | ||
| ).set_function(weakref.proxy(self)._waiting_dependencies_gauge) | ||
|
|
||
| def _unregister_channel_gauges(self) -> None: | ||
| if not self._metrics_labels: | ||
| return | ||
| metrics.channel_capacity.remove_by_labels(self._metrics_labels) | ||
| metrics.channel_pending.remove_by_labels(self._metrics_labels) | ||
| metrics.channel_running.remove_by_labels(self._metrics_labels) | ||
| metrics.channel_failed.remove_by_labels(self._metrics_labels) | ||
| metrics.channel_waiting_dependencies.remove_by_labels(self._metrics_labels) | ||
|
|
||
| def _capacity_gauge(self) -> float: | ||
| if self.capacity is None: | ||
| return math.inf | ||
|
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. I wonder how will it look in the output metric.
Member
Author
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. Yes I have not tested that yet. |
||
| return self.capacity | ||
|
|
||
| def _pending_gauge(self) -> float: | ||
| return len(self._queue) | ||
|
|
||
| def _running_gauge(self) -> float: | ||
| return len(self._running) | ||
|
|
||
| def _failed_gauge(self) -> float: | ||
| return len(self._failed) | ||
|
|
||
| def _waiting_dependencies_gauge(self) -> float: | ||
| return len(self._waiting_dependencies) | ||
|
|
||
| @property | ||
| def sequential(self): | ||
|
|
@@ -457,14 +516,15 @@ def __str__(self): | |
| capacity = "∞" if self.capacity is None else str(self.capacity) | ||
| return ( | ||
| f"{self.fullname}(C:{capacity},Q:{len(self._queue)}," | ||
| f"R:{len(self._running)},F:{len(self._failed)})" | ||
| f"R:{len(self._running)},F:{len(self._failed)},W:{len(self._waiting_dependencies)})" | ||
| ) | ||
|
|
||
| def remove(self, job): | ||
| """Remove a job from the channel.""" | ||
| self._queue.remove(job) | ||
| self._running.discard(job) | ||
| self._failed.discard(job) | ||
| self._waiting_dependencies.discard(job) | ||
| if self.parent: | ||
| self.parent.remove(job) | ||
|
|
||
|
|
@@ -486,6 +546,7 @@ def set_pending(self, job): | |
| self._queue.add(job) | ||
| self._running.discard(job) | ||
| self._failed.discard(job) | ||
| self._waiting_dependencies.discard(job) | ||
| if self.parent: | ||
| self.parent.remove(job) | ||
| _logger.debug("job %s marked pending in channel %s", job.uuid, self) | ||
|
|
@@ -499,6 +560,7 @@ def set_running(self, job): | |
| self._queue.remove(job) | ||
| self._running.add(job) | ||
| self._failed.discard(job) | ||
| self._waiting_dependencies.discard(job) | ||
| if self.parent: | ||
| self.parent.set_running(job) | ||
| _logger.debug("job %s marked running in channel %s", job.uuid, self) | ||
|
|
@@ -509,10 +571,23 @@ def set_failed(self, job): | |
| self._queue.remove(job) | ||
| self._running.discard(job) | ||
| self._failed.add(job) | ||
| self._waiting_dependencies.discard(job) | ||
| if self.parent: | ||
| self.parent.remove(job) | ||
| _logger.debug("job %s marked failed in channel %s", job.uuid, self) | ||
|
|
||
| def set_waiting_dependencies(self, job): | ||
| if job not in self._waiting_dependencies: | ||
| self._queue.remove(job) | ||
| self._running.discard(job) | ||
| self._failed.discard(job) | ||
| self._waiting_dependencies.add(job) | ||
| if self.parent: | ||
| self.parent.remove(job) | ||
| _logger.debug( | ||
| "job %s marked waiting dependencies in channel %s", job.uuid, self | ||
| ) | ||
|
|
||
| def has_capacity(self): | ||
| if self.sequential and self._failed: | ||
| # a sequential queue blocks on failed jobs | ||
|
|
@@ -1056,7 +1131,7 @@ def notify( | |
| job.channel.set_failed(job) | ||
| elif state == WAIT_DEPENDENCIES: | ||
| # wait until all parent jobs are done | ||
| pass | ||
| job.channel.set_waiting_dependencies(job) | ||
| else: | ||
| _logger.error("unexpected state %s for job %s", state, job) | ||
|
|
||
|
|
@@ -1077,3 +1152,6 @@ def get_jobs_to_run(self, now): | |
|
|
||
| def get_wakeup_time(self): | ||
| return self._root_channel.get_wakeup_time() | ||
|
|
||
| def _jobs_to_do_gauge(self) -> float: | ||
| return len(self._jobs_by_uuid) | ||
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,73 @@ | ||
| import socket | ||
| from http.server import HTTPServer | ||
|
|
||
| from prometheus_client import CollectorRegistry, Counter, Gauge, MetricsHandler | ||
|
|
||
| _registry = CollectorRegistry() | ||
|
|
||
| _channel_label_names = ("channel", "root", "leaf") | ||
| channel_capacity = Gauge( | ||
| "queue_job_channel_capacity", | ||
| documentation="Channel Capacity", | ||
| labelnames=_channel_label_names, | ||
| registry=_registry, | ||
| ) | ||
| channel_pending = Gauge( | ||
| "queue_job_channel_pending", | ||
| documentation="Pending jobs in channel", | ||
| labelnames=_channel_label_names, | ||
| registry=_registry, | ||
| ) | ||
| channel_running = Gauge( | ||
| "queue_job_channel_running", | ||
| documentation="Running jobs in channel", | ||
| labelnames=_channel_label_names, | ||
| registry=_registry, | ||
| ) | ||
| channel_failed = Gauge( | ||
| "queue_job_channel_failed", | ||
| documentation="Failed jobs in channel", | ||
| labelnames=_channel_label_names, | ||
| registry=_registry, | ||
| ) | ||
| channel_waiting_dependencies = Gauge( | ||
| "queue_job_channel_waiting_dependencies", | ||
| documentation="Jobs waiting for dependencies in channel", | ||
| labelnames=_channel_label_names, | ||
| registry=_registry, | ||
| ) | ||
|
|
||
| jobs_to_do = Gauge( | ||
| "queue_job_jobs_to_do", | ||
| documentation=( | ||
| "Number of jobs waiting to be done (including running and failed jobs)" | ||
| ), | ||
| registry=_registry, | ||
| ) | ||
| jobs_scheduled_total = Counter( | ||
| "queue_job_jobs_scheduled_total", | ||
| documentation=( | ||
| "Total number of jobs scheduled for execution (asked Odoo to run job)" | ||
| ), | ||
| labelnames=("db",), | ||
| registry=_registry, | ||
| ) | ||
| dead_jobs_requeued_total = Counter( | ||
| "queue_job_dead_jobs_requeued_total", | ||
| documentation=("Total number of dead jobs requeued"), | ||
| labelnames=("db",), | ||
| registry=_registry, | ||
| ) | ||
|
|
||
|
|
||
| def make_metrics_server(bind_addr, port) -> HTTPServer: | ||
| infos = socket.getaddrinfo( | ||
| bind_addr, | ||
| port, | ||
| type=socket.SOCK_STREAM, | ||
| flags=socket.AI_PASSIVE, | ||
| ) | ||
| _, _, _, _, sockaddr = next(iter(infos)) | ||
| server = HTTPServer(sockaddr, MetricsHandler.factory(_registry)) | ||
| server.socket.setblocking(False) | ||
| return server |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.