Skip to content
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

Missing a queue name upon receiving a task event #335

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/exporter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable=protected-access,,attribute-defined-outside-init
from functools import lru_cache
import json
import re
import sys
Expand All @@ -7,7 +8,7 @@
from typing import Callable, Optional

from celery import Celery
from celery.events.state import State # type: ignore
from celery.events.state import State, Task # type: ignore
from celery.utils import nodesplit # type: ignore
from celery.utils.time import utcoffset # type: ignore
from kombu.exceptions import ChannelError # type: ignore
Expand Down Expand Up @@ -141,6 +142,27 @@ def scrape(self):
self.track_timed_out_workers()
self.track_queue_metrics()

@lru_cache(maxsize=32)
def find_queue_by_task(self, target: Task) -> str:
"""Provider a queue name based on metadata coming from eiether a worker or a task being processed by it"""

try:
queue_name = None
# https://github.com/celery/celery/issues/5321
# task_info = self.app.control.inspect().query_task(task.id)
# As the received tasks are considered as active ones
task_set = self.app.control.inspect().registered().get(target.hostname)
task_info = [task for task in task_set if task['id'] == target.id].pop()
queue_name = task_info["delivery_info"]["routing_key"]
except TimeoutError as error:
# the broker doesn't respond
logger.error(f"Couldn't fetch the task info of {target.id}: {error.strerror}")
except IndexError as error:
# couldn't find the target task by id
# the dictionary path is missing
logger.warning(f"Couldn't find the target task by its id: {target.id}")
return queue_name
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when an app deployment changes the queue and the celery-exporter doesn't restart?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will keep return the old queue name until the cache will have been invalidated.
That was my next question which I missed to include in the header. Pardon for that 🙏

How to not query the broker for each task as it can lead to some performance degradation depending on the producing rate ?

Several strategies comes at once, namely:

  • we need to start another worker to invalidate the cache
  • remove caching for this lookup method at this point
  • decrease the cache size

I've also taken a look into Flower to see how it manages such cases, but couldn't find any relevant parts related to queue name processing.


def forget_worker(self, hostname):
if hostname in self.worker_last_seen:
self.celery_worker_up.labels(hostname=hostname).set(0)
Expand Down Expand Up @@ -263,7 +285,9 @@ def track_task_event(self, event):
labels = {
"name": task.name,
"hostname": get_hostname(task.hostname),
"queue_name": getattr(task, "queue", "celery"),
# queue property should be available when a task is called with a passed queue name
# otherwise, we need to query its meta using the celery instance
"queue_name": getattr(task, "queue", self.find_queue_by_task(task) or "celery"),
}
if event["type"] == "task-sent" and self.generic_hostname_task_sent_metric:
labels["hostname"] = "generic"
Expand Down
Loading