From 45477048fc5c9ec90c1a575fc94149ab564d37aa Mon Sep 17 00:00:00 2001 From: Ali Helmy Date: Mon, 9 Jan 2023 03:18:03 +0200 Subject: [PATCH 1/3] Add queues priority to metrics only works in redis --- README.md | 5 ++++- src/exporter.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b4d7e8c..6cd134a 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,10 @@ docker run -p 9808:9808 danihodovic/celery-exporter --broker-url=redis://redis.s --broker-transport-option global_keyprefix=danihodovic \ --broker-transport-option visibility_timeout=7200 ``` - +In case you changed celery transport options to enable [priority](https://docs.celeryq.dev/en/stable/userguide/routing.html#redis-message-priorities) or changed separator just use same config here as well. +```sh +--broker-transport-option sep=':' --broker-transport-option priority_steps=[0,1,2,3,4,5,6,7,8,9] +``` In case of extended transport options, such as `sentinel_kwargs` you can pass JSON string:, for example: diff --git a/src/exporter.py b/src/exporter.py index 050e5a1..169ba95 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -9,7 +9,12 @@ from celery.utils import nodesplit # type: ignore from kombu.exceptions import ChannelError # type: ignore from loguru import logger -from prometheus_client import CollectorRegistry, Counter, Gauge, Histogram +from prometheus_client import ( + CollectorRegistry, + Counter, + Gauge, + Histogram, +) from .http_server import start_http_server @@ -123,9 +128,20 @@ def track_queue_metrics(self): # we need to cache queue info in exporter in case all workers are offline # so that no worker response to exporter will make active_queues return None queues = self.app.control.inspect().active_queues() or {} + queue_cache = set() for info_list in queues.values(): for queue_info in info_list: self.queue_cache.add(queue_info["name"]) + queue_cache.add((queue_info['name'])) + + # Check celery queues based on worker separator & priority steps + separator = '\x06\x16' + if 'sep' in self.app.conf["broker_transport_options"]: + separator = self.app.conf["broker_transport_options"]['sep'] + if 'priority_steps' in self.app.conf["broker_transport_options"]: + for queue in queue_cache: + for step in self.app.conf['broker_transport_options']['priority_steps']: + self.queue_cache.add(f'{queue}{separator}{str(step)}') track_length = lambda q, l: self.celery_queue_length.labels( queue_name=q From 005bc5f65cc7920900af3db6286e468852f396c0 Mon Sep 17 00:00:00 2001 From: Ali Helmy Date: Sat, 18 Mar 2023 15:02:40 +0200 Subject: [PATCH 2/3] idea --- .gitignore | 1 + .idea/celery-exporter.iml | 15 ++++ .idea/inspectionProfiles/Project_Default.xml | 25 ++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .../439a11d97562b4c11f5afddb0c766d9ff3818c45 | 0 .idea/sonarlint/issuestore/index.pb | 3 + .idea/vcs.xml | 6 ++ .idea/workspace.xml | 78 +++++++++++++++++++ 10 files changed, 146 insertions(+) create mode 100644 .idea/celery-exporter.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sonarlint/issuestore/4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 create mode 100644 .idea/sonarlint/issuestore/index.pb create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 03ac2c1..75515d9 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,4 @@ dmypy.json .virtualenv celery-exporter +.idea diff --git a/.idea/celery-exporter.iml b/.idea/celery-exporter.iml new file mode 100644 index 0000000..5fdd65b --- /dev/null +++ b/.idea/celery-exporter.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..1c5f09b --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,25 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2d0d27 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c84e48e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 b/.idea/sonarlint/issuestore/4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000..82427c3 --- /dev/null +++ b/.idea/sonarlint/issuestore/index.pb @@ -0,0 +1,3 @@ + +C +src/test_metrics.py,4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..764fa24 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + 1671793254796 + + + 1673227085331 + + + + + + + + + + + + + file://$PROJECT_DIR$/src/exporter.py + 128 + + + + + \ No newline at end of file From 2f481843315dd4041ec745b98396cd27273427f0 Mon Sep 17 00:00:00 2001 From: Ali Helmy Date: Sat, 18 Mar 2023 16:36:18 +0200 Subject: [PATCH 3/3] add unit testing --- .../439a11d97562b4c11f5afddb0c766d9ff3818c45 | 0 .idea/sonarlint/issuestore/index.pb | 3 - .idea/workspace.xml | 143 +++++++++++++++++- conftest.py | 8 +- src/exporter.py | 4 +- src/test_cli.py | 11 ++ 6 files changed, 158 insertions(+), 11 deletions(-) delete mode 100644 .idea/sonarlint/issuestore/4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 diff --git a/.idea/sonarlint/issuestore/4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 b/.idea/sonarlint/issuestore/4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 deleted file mode 100644 index e69de29..0000000 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb index 82427c3..e69de29 100644 --- a/.idea/sonarlint/issuestore/index.pb +++ b/.idea/sonarlint/issuestore/index.pb @@ -1,3 +0,0 @@ - -C -src/test_metrics.py,4/3/439a11d97562b4c11f5afddb0c766d9ff3818c45 \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 764fa24..2c0bd0e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,7 +4,14 @@