33
44from __future__ import annotations
55
6+ import datetime
67from typing import Iterable
78
89import prometheus_client
@@ -85,7 +86,7 @@ def worker_metrics(watcher: WorkerWatcher) -> list[prometheus_client.Metric]:
8586 active_task_count_metric = GaugeMetricFamily (
8687 name = "celerymon_inspect_worker_held_task_count" ,
8788 documentation = "Number of tasks held in all workers" ,
88- labels = ["task_name" , "state" ],
89+ labels = ["task_name" , "state" , "hostname" ],
8990 )
9091 if watcher .last_updated_timestamp is not None :
9192 last_updated_timestamp_seconds_metric .add_metric (
@@ -100,9 +101,9 @@ def worker_metrics(watcher: WorkerWatcher) -> list[prometheus_client.Metric]:
100101 timestamp = watcher .last_updated_timestamp .timestamp (),
101102 )
102103 for key , count in watcher .task_count .items ():
103- state , task_name = key
104+ state , task_name , hostname = key
104105 active_task_count_metric .add_metric (
105- labels = [task_name , state ],
106+ labels = [task_name , state , hostname ],
106107 value = count ,
107108 timestamp = watcher .last_updated_timestamp .timestamp (),
108109 )
@@ -125,12 +126,21 @@ def event_metrics(watcher: EventWatcher) -> list[prometheus_client.Metric]:
125126 documentation = "The task event count per task name and event name." ,
126127 labels = ["task_name" , "event_name" ],
127128 )
128- success_task_runtime_seconds_metric = HistogramMetricFamily (
129- name = "celerymon_events_success_task_runtime_seconds" ,
130- documentation = "The task runtime per task name for finished success tasks." ,
131- labels = ["task_name" ],
129+ task_runtime_seconds_metric = HistogramMetricFamily (
130+ name = "celerymon_events_task_runtime_seconds" ,
131+ documentation = (
132+ "Task runtime per task name, labeled by result (success|failed)."
133+ ),
134+ labels = ["task_name" , "result" ],
132135 unit = "seconds" ,
133136 )
137+ online_worker_count_metric = GaugeMetricFamily (
138+ name = "celerymon_events_online_worker_count" ,
139+ documentation = (
140+ "Number of workers currently online, derived from worker-online, "
141+ "worker-heartbeat, and worker-offline events."
142+ ),
143+ )
134144 if watcher .last_received_timestamp is not None :
135145 for key , timestamp in watcher .last_received_timestamp_per_task_event .items ():
136146 count = watcher .num_events_per_task_count [key ]
@@ -147,19 +157,28 @@ def event_metrics(watcher: EventWatcher) -> list[prometheus_client.Metric]:
147157 timestamp = timestamp .timestamp (),
148158 )
149159 for task_name in watcher .task_names :
150- acc = 0.0
151- buckets = []
152- for i , bound in enumerate (watcher .upper_bounds ):
153- acc += watcher .succeeded_task_runtime_sec [i ][task_name ]
154- buckets .append ((floatToGoString (bound ), acc ))
155- success_task_runtime_seconds_metric .add_metric (
156- labels = [task_name ],
157- buckets = buckets ,
158- sum_value = watcher .succeeded_task_runtime_sec_sum [task_name ],
159- timestamp = watcher .last_received_timestamp .timestamp (),
160- )
160+ for result in ("success" , "failed" ):
161+ key = (task_name , result )
162+ acc = 0.0
163+ buckets = []
164+ for i , bound in enumerate (watcher .upper_bounds ):
165+ acc += watcher .task_runtime_sec [i ].get (key , 0 )
166+ buckets .append ((floatToGoString (bound ), acc ))
167+ task_runtime_seconds_metric .add_metric (
168+ labels = [task_name , result ],
169+ buckets = buckets ,
170+ sum_value = watcher .task_runtime_sec_sum .get (key , 0.0 ),
171+ timestamp = watcher .last_received_timestamp .timestamp (),
172+ )
173+ now = datetime .datetime .now (tz = datetime .UTC )
174+ online_worker_count_metric .add_metric (
175+ labels = [],
176+ value = watcher .online_worker_count (now ),
177+ timestamp = watcher .last_received_timestamp .timestamp (),
178+ )
161179 return [
162180 last_received_timestamp_seconds_metric ,
163181 events_count_metric ,
164- success_task_runtime_seconds_metric ,
182+ task_runtime_seconds_metric ,
183+ online_worker_count_metric ,
165184 ]
0 commit comments