From 8292add5c7a91b766ed107c38bbffd291425ea5f Mon Sep 17 00:00:00 2001 From: Adam McCrea Date: Sat, 26 Aug 2023 10:13:57 -0400 Subject: [PATCH] fix: Gracefully handle missing task ID (#70) --- judoscale/celery/collector.py | 3 ++- tests/test_collectors.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/judoscale/celery/collector.py b/judoscale/celery/collector.py index 01e8ce1..1100e8c 100644 --- a/judoscale/celery/collector.py +++ b/judoscale/celery/collector.py @@ -129,9 +129,10 @@ def collect(self) -> List[Metric]: Metric.for_queue(queue_name=queue, oldest_job_ts=published_at) ) else: + task_id = task.get("id", None) logger.warning( "Unable to find `published_at` in task properties for " - f"task ID {task['id']}." + f"task ID {task_id}." ) else: metrics.append( diff --git a/tests/test_collectors.py b/tests/test_collectors.py index c387f6e..4f55c5d 100644 --- a/tests/test_collectors.py +++ b/tests/test_collectors.py @@ -163,6 +163,15 @@ def test_collect_missing_published_at(self, worker_1, celery): collector = CeleryMetricsCollector(worker_1, celery) assert len(collector.collect()) == 0 + def test_collect_missing_published_at_and_id(self, worker_1, celery): + celery.connection_for_read().channel().client.scan_iter.return_value = [b"foo"] + celery.connection_for_read().channel().client.lindex.return_value = bytes( + json.dumps({"properties": {}}), "utf-8" + ) + + collector = CeleryMetricsCollector(worker_1, celery) + assert len(collector.collect()) == 0 + def test_collect_response_error(self, worker_1, celery, caplog): celery.connection_for_read().channel().client.scan_iter.return_value = [b"foo"] celery.connection_for_read().channel().client.lindex.side_effect = (