Skip to content

Commit 258a041

Browse files
adinhodovicdanihodovic
authored andcommitted
fix(metrics): Ensure task_sent counters are not instantiated
1 parent e116052 commit 258a041

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/exporter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ def track_task_event(self, event):
301301
logger.debug(
302302
"Incremented metric='{}' labels='{}'", counter._name, labels
303303
)
304-
elif (
305-
event["type"] != "task-sent"
306-
): # task-sent is sent by various hosts (webservers, task creators etc.) which cause label cardinality # pylint: disable=line-too-long
307-
# increase unaffected counters by zero in order to make them visible
304+
elif counter_name != "task-sent":
305+
# instantiate unaffected counters by zero in order to make them visible
306+
# task-sent is sent by various hosts (webservers, task creators)
307+
# this causes label cardinality, therefore we do not want to instantiate the counter
308308
counter.labels(**_labels).inc(0)
309309

310310
# observe task runtime

src/test_metrics.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_purge_offline_worker_metrics(
196196
)
197197

198198

199-
def test_worker_generic_task_sent_hostname(threaded_exporter, celery_app):
199+
def test_worker_generic_task_sent_hostname(threaded_exporter, celery_app, hostname):
200200
threaded_exporter.generic_hostname_task_sent_metric = True
201201
time.sleep(5)
202202

@@ -219,3 +219,15 @@ def succeed():
219219
)
220220
== 1.0
221221
)
222+
223+
assert (
224+
threaded_exporter.registry.get_sample_value(
225+
"celery_task_sent_total",
226+
labels={
227+
"hostname": hostname,
228+
"name": "src.test_metrics.succeed",
229+
"queue_name": "celery",
230+
},
231+
)
232+
is None
233+
)

0 commit comments

Comments
 (0)