Skip to content

Commit

Permalink
Handle exceptions from collectors (#3044)
Browse files Browse the repository at this point in the history
* wrapping collectors with try blocks

* remove unused e

* items not enumerate

* extract task_name, not name

* started_at, not time_start
  • Loading branch information
joaquincasares committed May 5, 2022
1 parent 8c69d9c commit 23d350a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion discovery-provider/src/queries/get_celery_tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging

from src.monitors import monitor_names, monitors
from src.utils.prometheus_metric import PrometheusMetric, PrometheusType

logger = logging.getLogger(__name__)
MONITORS = monitors.MONITORS


Expand All @@ -27,7 +30,12 @@ def celery_tasks_prometheus_exporter():
)

for task in tasks:
metric.save_time({"task_name": task["name"]}, start_time=task["time_start"])
try:
metric.save_time(
{"task_name": task["task_name"]}, start_time=task["started_at"]
)
except:
logger.exception(f"Processing failed for task: {task}")


PrometheusMetric.register_collector(
Expand Down
10 changes: 8 additions & 2 deletions discovery-provider/src/utils/prometheus_metric.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
from time import time
from typing import Callable, Dict

from prometheus_client import Gauge, Histogram

logger = logging.getLogger(__name__)


class PrometheusType:
HISTOGRAM = "histogram"
Expand Down Expand Up @@ -73,5 +76,8 @@ def register_collector(cls, name, collector_func):

@classmethod
def populate_collectors(cls):
for collector in cls.registered_collectors.values():
collector()
for name, collector in cls.registered_collectors.items():
try:
collector()
except:
logger.exception(f"Failure to collect '{name}'")

0 comments on commit 23d350a

Please sign in to comment.