forked from rq/django-rq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export RQ status as prometheus metrics
fixes: rq#503
- Loading branch information
1 parent
025a53f
commit 15bf703
Showing
4 changed files
with
96 additions
and
1 deletion.
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,6 +1,71 @@ | ||
from django.apps import AppConfig | ||
|
||
from .queues import filter_connection_params, get_connection, get_queue_class, get_unique_connection_configs | ||
from .workers import get_worker_class | ||
|
||
try: | ||
from rq.job import JobStatus | ||
from prometheus_client import Summary, REGISTRY | ||
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily | ||
|
||
class RQCollector: | ||
"""RQ stats collector""" | ||
|
||
summary = Summary('rq_request_processing_seconds', 'Time spent collecting RQ data') | ||
|
||
def collect(self): | ||
from .settings import QUEUES | ||
|
||
with self.summary.time(): | ||
rq_workers = GaugeMetricFamily('rq_workers', 'RQ workers', labels=['name', 'state', 'queues']) | ||
rq_workers_success = CounterMetricFamily('rq_workers_success', 'RQ workers success count', labels=['name', 'queues']) | ||
rq_workers_failed = CounterMetricFamily('rq_workers_failed', 'RQ workers fail count', labels=['name', 'queues']) | ||
rq_workers_working_time = CounterMetricFamily('rq_workers_working_time', 'RQ workers spent seconds', labels=['name', 'queues']) | ||
|
||
rq_jobs = GaugeMetricFamily('rq_jobs', 'RQ jobs by state', labels=['queue', 'status']) | ||
|
||
worker_class = get_worker_class() | ||
unique_configs = get_unique_connection_configs() | ||
connections = {} | ||
for queue_name, config in QUEUES.items(): | ||
index = unique_configs.index(filter_connection_params(config)) | ||
if index not in connections: | ||
connections[index] = connection = get_connection(queue_name) | ||
else: | ||
connection = connections[index] | ||
|
||
for worker in worker_class.all(connection): | ||
name = worker.name | ||
label_queues = ','.join(worker.queue_names()) | ||
rq_workers.add_metric([name, worker.get_state(), label_queues], 1) | ||
rq_workers_success.add_metric([name, label_queues], worker.successful_job_count) | ||
rq_workers_failed.add_metric([name, label_queues], worker.failed_job_count) | ||
rq_workers_working_time.add_metric([name, label_queues], worker.total_working_time) | ||
|
||
yield rq_workers | ||
yield rq_workers_success | ||
yield rq_workers_failed | ||
yield rq_workers_working_time | ||
|
||
queue_class = get_queue_class(config) | ||
for queue in queue_class.all(connection): | ||
rq_jobs.add_metric([queue_name, JobStatus.QUEUED], queue.count) | ||
rq_jobs.add_metric([queue_name, JobStatus.STARTED], queue.started_job_registry.count) | ||
rq_jobs.add_metric([queue_name, JobStatus.FINISHED], queue.finished_job_registry.count) | ||
rq_jobs.add_metric([queue_name, JobStatus.FAILED], queue.failed_job_registry.count) | ||
rq_jobs.add_metric([queue_name, JobStatus.DEFERRED], queue.deferred_job_registry.count) | ||
rq_jobs.add_metric([queue_name, JobStatus.SCHEDULED], queue.scheduled_job_registry.count) | ||
|
||
yield rq_jobs | ||
|
||
except ImportError: | ||
RQCollector = None | ||
|
||
|
||
class DjangoRqAdminConfig(AppConfig): | ||
default_auto_field = "django.db.models.AutoField" | ||
name = "django_rq" | ||
|
||
def ready(self): | ||
if RQCollector is not None: | ||
REGISTRY.register(RQCollector()) |
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