-
Notifications
You must be signed in to change notification settings - Fork 4
/
koji-prometheus-exporter.py
executable file
·418 lines (336 loc) · 14.1 KB
/
koji-prometheus-exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python3
""" A simple prometheus exporter for koji.
Scrapes koji on an interval and exposes metrics about tasks.
"""
from datetime import datetime
import logging
import os
import time
import dogpile.cache
import koji
from prometheus_client.core import (
REGISTRY,
CounterMetricFamily,
GaugeMetricFamily,
HistogramMetricFamily,
)
from prometheus_client import start_http_server
cache = dogpile.cache.make_region().configure(
'dogpile.cache.memory', expiration_time=1000
)
KOJI_URL = os.environ['KOJI_URL'] # Required
KOJI_TIMEOUT = os.environ.get('KOJI_TIMEOUT', 60) # Optional
b = koji.ClientSession(KOJI_URL, opts=dict(timeout=KOJI_TIMEOUT))
channels = b.listChannels()
CHANNELS = dict([(channel['id'], channel['name']) for channel in channels])
TASK_LABELS = ['channel', 'method']
HOST_LABELS = ['channel']
BUILDER_LABELS = ['hostname', 'channel']
# In seconds
DURATION_BUCKETS = [
10,
30,
60, # 1 minute
180, # 3 minutes
480, # 8 minutes
1200, # 20 minutes
3600, # 1 hour
7200, # 2 hours
]
START = None
metrics = {}
error_states = [
koji.TASK_STATES['FAILED'],
]
completed_states = [
koji.TASK_STATES['FAILED'],
koji.TASK_STATES['CANCELED'],
koji.TASK_STATES['CLOSED'],
]
waiting_states = [
koji.TASK_STATES['FREE'],
koji.TASK_STATES['ASSIGNED'],
]
in_progress_states = [
koji.TASK_STATES['OPEN'],
]
class IncompleteTask(Exception):
""" Error raised when a koji task is not complete. """
pass
def retrieve_recent_koji_tasks():
b = koji.ClientSession(KOJI_URL, opts=dict(timeout=KOJI_TIMEOUT))
tasks = b.listTasks(opts=dict(createdAfter=START))
return tasks
def retrieve_open_koji_tasks():
b = koji.ClientSession(KOJI_URL, opts=dict(timeout=KOJI_TIMEOUT))
tasks = b.listTasks(opts=dict(state=in_progress_states))
return tasks
def retrieve_waiting_koji_tasks():
b = koji.ClientSession(KOJI_URL, opts=dict(timeout=KOJI_TIMEOUT))
tasks = b.listTasks(opts=dict(state=waiting_states))
return tasks
# This takes about 3s to generate and it changes very infrequently, so cache it.
# It is useful for making "saturation" metrics in promql.
@cache.cache_on_arguments()
def retrieve_hosts_by_channel():
b = koji.ClientSession(KOJI_URL, opts=dict(timeout=KOJI_TIMEOUT))
hosts = {}
for idx, channel in CHANNELS.items():
hosts[channel] = list(b.listHosts(channelID=idx, enabled=True))
return hosts
def retrieve_task_load_by_channel():
# Initialize return structure
task_load = {}
for idx, channel in CHANNELS.items():
task_load[channel] = 0
# Grab the channel to host mapping from in memory cache
by_channel = retrieve_hosts_by_channel()
hosts = b.listHosts(enabled=True)
for host in hosts:
for idx, channel in CHANNELS.items():
if host['name'] in [h['name'] for h in by_channel[channel]]:
task_load[channel] += host['task_load']
return task_load
def koji_tasks_total(tasks):
counts = {}
for task in tasks:
method = task['method']
channel = CHANNELS[task['channel_id']]
counts[channel] = counts.get(channel, {})
counts[channel][method] = counts[channel].get(method, 0)
counts[channel][method] += 1
for channel in counts:
for method in counts[channel]:
yield counts[channel][method], [channel, method]
def calculate_overall_duration(task):
if not task['completion_ts']:
# Duration is undefined.
# We could consider using `time.time()` as the duration, but that would produce durations
# that change for incomlete tasks -- and changing durations like that is incompatible with
# prometheus' histogram and counter model. So - we just ignore tasks until they are
# complete and have a final duration.
raise IncompleteTask("Task is not yet complete. Duration is undefined.")
return task['completion_ts'] - task['create_ts']
def calculate_waiting_duration(task):
if not task['start_ts']:
# Duration is undefined because the task has not started yet.
# We could consider using `time.time()` as the duration, but that would produce durations
# that change for incomlete tasks -- and changing durations like that is incompatible with
# prometheus' histogram and counter model. So - we just ignore tasks until they are
# started and have a final waiting duration.
raise IncompleteTask("Task is not yet started. Duration is undefined.")
return task['start_ts'] - task['create_ts']
def calculate_in_progress_duration(task):
if not task['completion_ts']:
# Duration is undefined because the task has not completed yet.
# We could consider using `time.time()` as the duration, but that would produce durations
# that change for incomlete tasks -- and changing durations like that is incompatible with
# prometheus' histogram and counter model. So - we just ignore tasks until they are
# complete and have a final duration.
raise IncompleteTask("Task is not yet complete. Duration is undefined.")
if not task['start_ts']:
# This shouldn't happen as a task with a completion_ts should always have a start_ts.
# However, if a task is cancelled, or due to an unknown corner case, the start_ts may not
# be set.
raise IncompleteTask("Task completed but not started. Duration is undefined.")
return task['completion_ts'] - task['start_ts']
def find_applicable_buckets(duration):
buckets = DURATION_BUCKETS + ["+Inf"]
for bucket in buckets:
if duration < float(bucket):
yield bucket
def koji_task_duration_seconds(tasks, calculate_duration):
duration_buckets = DURATION_BUCKETS + ["+Inf"]
# Build counts of observations into histogram "buckets"
counts = {}
# Sum of all observed durations
durations = {}
for task in tasks:
method = task['method']
channel = CHANNELS[task['channel_id']]
try:
duration = calculate_duration(task)
except IncompleteTask:
continue
# Initialize structures
durations[channel] = durations.get(channel, {})
durations[channel][method] = durations[channel].get(method, 0)
counts[channel] = counts.get(channel, {})
counts[channel][method] = counts[channel].get(method, {})
for bucket in duration_buckets:
counts[channel][method][bucket] = counts[channel][method].get(bucket, 0)
# Increment applicable bucket counts and duration sums
durations[channel][method] += duration
for bucket in find_applicable_buckets(duration):
counts[channel][method][bucket] += 1
for channel in counts:
for method in counts[channel]:
buckets = [
(str(bucket), counts[channel][method][bucket])
for bucket in duration_buckets
]
yield buckets, durations[channel][method], [channel, method]
def koji_enabled_hosts_count(hosts):
for channel in hosts:
yield len(hosts[channel]), [channel]
def koji_enabled_hosts_capacity(hosts):
for channel in hosts:
yield sum([h['capacity'] for h in hosts[channel]]), [channel]
def koji_hosts_last_update(hosts):
results = []
with b.multicall() as m:
for channel, host_list in hosts.items():
for host in host_list:
# m.getLastHostUpdate returns a VirtualCall object.
# It is handled later.
results.append( [ m.getLastHostUpdate(host['id'], ts=True),
[ host['name'],
channel]])
# Now, filter out any that have None for their value.
results = [result for result in results if result[0].result is not None]
return results
def koji_task_load(task_load):
for channel, value in task_load.items():
yield value, [channel]
def only(tasks, states):
for task in tasks:
state = task['state']
if state in states:
yield task
def scrape():
global START
today = datetime.utcnow().date()
START = datetime.timestamp(datetime.combine(today, datetime.min.time()))
tasks = retrieve_recent_koji_tasks()
koji_tasks_total_family = CounterMetricFamily(
'koji_tasks_total', 'Count of all koji tasks', labels=TASK_LABELS
)
for value, labels in koji_tasks_total(tasks):
koji_tasks_total_family.add_metric(labels, value)
koji_task_errors_total_family = CounterMetricFamily(
'koji_task_errors_total', 'Count of all koji task errors', labels=TASK_LABELS
)
error_tasks = only(tasks, states=error_states)
for value, labels in koji_tasks_total(error_tasks):
koji_task_errors_total_family.add_metric(labels, value)
koji_task_completions_total_family = CounterMetricFamily(
'koji_task_completions_total', 'Count of all koji task completed', labels=TASK_LABELS
)
completed_tasks = only(tasks, states=completed_states)
for value, labels in koji_tasks_total(completed_tasks):
koji_task_completions_total_family.add_metric(labels, value)
koji_in_progress_tasks_family = GaugeMetricFamily(
'koji_in_progress_tasks',
'Count of all in-progress koji tasks',
labels=TASK_LABELS,
)
in_progress_tasks = retrieve_open_koji_tasks()
for value, labels in koji_tasks_total(in_progress_tasks):
koji_in_progress_tasks_family.add_metric(labels, value)
koji_waiting_tasks_family = GaugeMetricFamily(
'koji_waiting_tasks',
'Count of all waiting, unscheduled koji tasks',
labels=TASK_LABELS,
)
waiting_tasks = retrieve_waiting_koji_tasks()
for value, labels in koji_tasks_total(waiting_tasks):
koji_waiting_tasks_family.add_metric(labels, value)
koji_task_duration_seconds_family = HistogramMetricFamily(
'koji_task_duration_seconds',
'Histogram of koji task durations',
labels=TASK_LABELS,
)
for buckets, duration_sum, labels in koji_task_duration_seconds(
tasks, calculate_overall_duration
):
koji_task_duration_seconds_family.add_metric(labels, buckets, sum_value=duration_sum)
koji_task_waiting_duration_seconds_family = HistogramMetricFamily(
'koji_task_waiting_duration_seconds',
'Histogram of koji tasks durations while waiting',
labels=TASK_LABELS,
)
for buckets, duration_sum, labels in koji_task_duration_seconds(
tasks, calculate_waiting_duration
):
koji_task_waiting_duration_seconds_family.add_metric(
labels, buckets, sum_value=duration_sum
)
koji_task_in_progress_duration_seconds_family = HistogramMetricFamily(
'koji_task_in_progress_duration_seconds',
'Histogram of koji task durations while in-progress',
labels=TASK_LABELS,
)
for buckets, duration_sum, labels in koji_task_duration_seconds(
tasks, calculate_in_progress_duration
):
koji_task_in_progress_duration_seconds_family.add_metric(
labels, buckets, sum_value=duration_sum
)
koji_enabled_hosts_count_family = GaugeMetricFamily(
'koji_enabled_hosts_count',
'Count of all koji hosts by channel',
labels=HOST_LABELS,
)
koji_enabled_hosts_capacity_family = GaugeMetricFamily(
'koji_enabled_hosts_capacity',
'Reported capacity of all koji hosts by channel',
labels=HOST_LABELS,
)
koji_hosts_last_update_family = GaugeMetricFamily(
'koji_hosts_last_update',
'Gauge of last update from host',
labels=BUILDER_LABELS,
)
hosts = retrieve_hosts_by_channel()
# result_object is a VirtualCall object from the use of the MultiCallSession from the Koji API
for result_object, labels in koji_hosts_last_update(hosts):
koji_hosts_last_update_family.add_metric(labels, result_object.result)
for value, labels in koji_enabled_hosts_count(hosts):
koji_enabled_hosts_count_family.add_metric(labels, value)
for value, labels in koji_enabled_hosts_capacity(hosts):
koji_enabled_hosts_capacity_family.add_metric(labels, value)
koji_task_load_family = GaugeMetricFamily(
'koji_task_load',
'Task load of all koji builders by channel',
labels=HOST_LABELS,
)
task_load = retrieve_task_load_by_channel()
for value, labels in koji_task_load(task_load):
koji_task_load_family.add_metric(labels, value)
# Replace this in one atomic operation to avoid race condition to the Expositor
metrics.update(
{
'koji_tasks_total': koji_tasks_total_family,
'koji_task_errors_total': koji_task_errors_total_family,
'koji_task_completions_total': koji_task_completions_total_family,
'koji_in_progress_tasks': koji_in_progress_tasks_family,
'koji_waiting_tasks': koji_waiting_tasks_family,
'koji_task_duration_seconds': koji_task_duration_seconds_family,
'koji_task_waiting_duration_seconds': koji_task_waiting_duration_seconds_family,
'koji_task_in_progress_duration_seconds': koji_task_in_progress_duration_seconds_family,
'koji_enabled_hosts_count': koji_enabled_hosts_count_family,
'koji_enabled_hosts_capacity': koji_enabled_hosts_capacity_family,
'koji_task_load': koji_task_load_family,
'koji_hosts_last_update': koji_hosts_last_update_family,
}
)
class Expositor(object):
""" Responsible for exposing metrics to prometheus """
def collect(self):
logging.info("Serving prometheus data")
for key in sorted(metrics):
yield metrics[key]
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
for collector in list(REGISTRY._collector_to_names):
REGISTRY.unregister(collector)
REGISTRY.register(Expositor())
start_time = time.time()
# Popluate data before exposing over http
scrape()
start_http_server(8000)
ready_time = time.time()
print("Ready time: ", ready_time-start_time)
while True:
time.sleep(int(os.environ.get('KOJI_POLL_INTERVAL', '3')))
scrape()