-
Notifications
You must be signed in to change notification settings - Fork 2
/
events.py
462 lines (357 loc) · 11.5 KB
/
events.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import functools
import json
import os
from collections import OrderedDict
from constants import EVENTS_PATH
from constants import LOCKS_PATH
from constants import WORKER_ID
from datetime import datetime
from filelock import FileLock
from observable import Observable
from types import SimpleNamespace
from util import arguments_as_namespace
from util import dot_access
from util import global_run_id
# Global observable object
OBS = Observable()
# Global context
CTX = SimpleNamespace(
current_test=None,
worker_id=WORKER_ID,
)
# Undefined return value
UNDEFINED = object()
def trigger(event, **attributes):
""" Triggers the event with the given name, passing the given attributes
to any handler connected to it.
Returns True if any handlers were invoked. False if there were none.
"""
return OBS.trigger(event, **attributes)
def with_trigger(event):
""" Trigger the given event as follows:
First the event '<event>.before' is triggered, with the function arguments
passed, using the argument name 'args'.
Second, the event '<event>.after' is triggered, with 'args', as well as the
following values:
* 'exception' -> An exception object or None.
* 'result' -> The result of the function (if no exception).
* 'took' -> The seconds the call took.
"""
def decorator(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
trigger_args = arguments_as_namespace(fn, args, kwargs)
trigger(
event=f'{event}.before',
args=trigger_args,
)
time = datetime.utcnow()
try:
result = fn(*args, **kwargs)
except Exception as e:
trigger(
event=f'{event}.after',
args=trigger_args,
exception=e,
result=UNDEFINED,
took=(datetime.utcnow() - time).total_seconds(),
)
raise e
else:
trigger(
event=f'{event}.after',
args=trigger_args,
exception=None,
result=result,
took=(datetime.utcnow() - time).total_seconds(),
)
return result
return wrapper
return decorator
def record(event, **attributes):
""" Writes a new record to the event log.
The "event" should be something easily identifyable. For example:
- run.start
- server.start
- server.stop
The other key-value attributes will be added to the record. Additionally,
the following attributes are automatically added:
- current test
- time
- worker
"""
for reserved in ('time', 'worker', 'test'):
assert reserved not in attributes
record = OrderedDict()
record['time'] = datetime.now().isoformat()
record['worker'] = CTX.worker_id
record['test'] = CTX.current_test
record['event'] = event
record['run'] = int(os.environ.get('TEST_RUN', '1'))
for key in sorted(attributes):
record[key] = attributes[key]
line = json.dumps(record)
file_path = f'{EVENTS_PATH}/{global_run_id()}.log'
lock_path = f'{LOCKS_PATH}/{global_run_id()}.lock'
with FileLock(lock_path):
with open(file_path, 'a') as f:
f.write(line)
f.write("\n")
def track_in_event_log(event, include=None):
""" Listens to the given event in perpetuity and writes it to the event
log using the attributes as given.
The attributes included in the event log are specified through the
`include` parameter, which is a dictionary where the key is the key that
will be recorded, and the value is an expression that will be evaluated.
1. If the value is a string, it will be resolved by looking at the
attributes of the event.
For examaple, if 'request': 'request' is given, the request attribute is
stored under the request key in the log.
If 'url': 'request.url' is given, the url attribute of the request
attribute is stored under url key in the log.
2. If the value is a callable, a namespace with all attributes is passed
to the given function and the result of the function is stored under
the given key.
"""
include = include or {}
def extract_data(attributes):
for k, v in include.items():
if isinstance(v, str):
try:
yield k, dot_access(v, attributes)
except AttributeError:
# Ignore attribute if it does not exist
pass
elif callable(v):
yield k, v(SimpleNamespace(**attributes))
else:
raise RuntimeError(f"Unexpected value for {k}: {v}")
@OBS.on(event)
def on_record_event(**attributes):
record(**{'event': event, **dict(extract_data(attributes))})
# Often tracked attributes
RESOURCE_ID = {
'name': 'args.self.name',
'uuid': 'args.self.uuid',
}
RESULT = {
'took': 'took',
'result': lambda a: a.exception and 'failure' or 'success',
}
# Keep track of test runs
track_in_event_log('run.start', include={'run_id': 'run_id'})
track_in_event_log('run.end', include={'result': 'result', 'run_id': 'run_id'})
# Keep track of test items, recording their start and their result
track_in_event_log('test.start')
@OBS.on('test.start')
def on_test_start(name):
CTX.current_test = name.split('::')[-1]
@OBS.on('test.teardown')
def on_test_teardown(name, outcome, error, short_error):
CTX.current_test = None
for phase in ('call', 'setup', 'teardown'):
track_in_event_log(f'test.{phase}', include={
'outcome': 'outcome',
'error': 'error',
'short_error': 'short_error',
})
# Keep track of API requests
track_in_event_log('request.after', include={
'event': lambda a: f'request.{a.request.method}',
'url': 'request.url',
'status': 'response.status_code',
'took': lambda a: a.response.elapsed.total_seconds(),
})
# Keep track of server creation
track_in_event_log('server.create.before', include={
'name': 'args.self.name',
'zone': 'args.self.zone',
'image': 'args.self.image',
'flavor': 'args.self.flavor',
})
track_in_event_log('server.create.after', include={
**RESOURCE_ID,
**RESULT,
'zone': 'args.self.zone.slug',
'image': 'args.self.image.slug',
'public_ipv4': lambda a: str(a.args.self.ip('public', 4, False)),
'public_ipv6': lambda a: str(a.args.self.ip('public', 6, False)),
'private_ipv4': lambda a: str(a.args.self.ip('private', 4, False)),
'private_ipv6': lambda a: str(a.args.self.ip('private', 6, False)),
})
track_in_event_log('resource.wait.before', include={
**RESOURCE_ID,
'status': 'args.status',
})
track_in_event_log('resource.wait.after', include={
**RESOURCE_ID,
**RESULT,
'status': 'args.status',
})
track_in_event_log('server.connect.before', include=RESOURCE_ID)
track_in_event_log('server.connect.after', include={
**RESOURCE_ID,
**RESULT,
})
track_in_event_log('server.wait-for-cloud-init.after', include={
**RESOURCE_ID,
**RESULT,
})
track_in_event_log('server.wait-for-port.after', include={
**RESOURCE_ID,
**RESULT,
'port': 'args.port',
'state': 'args.state',
})
track_in_event_log('server.wait-for-non-tentative-ipv6.after', include={
**RESOURCE_ID,
**RESULT,
})
track_in_event_log('server.wait-for-ipv6-default-route.after', include={
**RESOURCE_ID,
**RESULT,
})
# Keep track of server changes
track_in_event_log('server.update.after', include={
**RESOURCE_ID,
**RESULT,
'changes': lambda a: {
k: v for k, v in a.args.__dict__.items() if k != 'self'
}
})
track_in_event_log('sever.scale-root.after', include={
**RESOURCE_ID,
**RESULT,
'new_size': 'args.new_size',
})
# Keep track of power events
track_in_event_log('server.start.before', include={
**RESOURCE_ID,
})
track_in_event_log('server.start.after', include={
**RESOURCE_ID,
**RESULT,
})
track_in_event_log('server.stop.before', include={
**RESOURCE_ID,
})
track_in_event_log('server.stop.after', include={
**RESOURCE_ID,
**RESULT,
})
track_in_event_log('server.reboot.before', include={
**RESOURCE_ID,
})
track_in_event_log('server.reboot.after', include={
**RESOURCE_ID,
**RESULT,
})
# Keep track of server commands
track_in_event_log('server.run.after', include={
**RESOURCE_ID,
**RESULT,
'command': 'args.command',
'exit_status': 'result.exit_status',
})
track_in_event_log('server.output-of.after', include={
**RESOURCE_ID,
**RESULT,
'command': 'args.command',
})
track_in_event_log('server.assert-run.after', include={
**RESOURCE_ID,
**RESULT,
'command': 'args.command',
})
# Keep track of server groups
track_in_event_log('server-group.create.after', include={
**RESOURCE_ID,
**RESULT,
'zone': 'args.self.zone.slug',
})
track_in_event_log('server-group.rename.after', include={
**RESOURCE_ID,
**RESULT,
'zone': 'args.self.zone.slug',
})
# Keep track of Floating IPs
track_in_event_log('floating-ip.create.after', include={
'network': lambda a: str(a.args.self.network),
'region': 'args.self.region.slug',
**RESULT,
})
track_in_event_log('floating-ip.assign.after', include={
'network': lambda a: str(a.args.self.network),
'server_name': 'args.server.name',
'server_uuid': 'args.server.uuid',
'load_balancer_name': 'args.load_balancer.name',
'load_balancer_uuid': 'args.load_balancer.uuid',
**RESULT,
})
track_in_event_log('floating-ip.update.after', include={
'network': lambda a: str(a.args.self.network),
**RESULT,
'changes': lambda a: {
k: v for k, v in a.args.__dict__.items() if k != 'self'
}
})
# Keep track of Volumes
track_in_event_log('volume.create.after', include={
**RESOURCE_ID,
**RESULT,
'size_gb': 'args.self.size_gb',
'type': 'args.self.type',
'zone': 'args.self.zone'
})
track_in_event_log('volume.attach.after', include={
**RESOURCE_ID,
**RESULT,
'server_name': 'args.server.name',
'server_uuid': 'args.server.uuid',
})
track_in_event_log('volume.scale.after', include={
**RESOURCE_ID,
**RESULT,
'new_size': 'args.new_size',
})
track_in_event_log('volume.detach.after', include={
**RESOURCE_ID,
**RESULT,
})
# Keep track of networks
track_in_event_log('network.create.after', include={
**RESOURCE_ID,
**RESULT,
'zone': 'args.self.zone.slug',
'auto_create_ipv4_subnet': 'args.self.auto_create_ipv4_subnet',
})
track_in_event_log('network.change-mtu.after', include={
**RESOURCE_ID,
**RESULT,
'mtu': 'args.mtu',
})
# Keep track of subnets
track_in_event_log('subnet.create.after', include={
**RESULT,
'uuid': 'args.self.uuid',
'network_uuid': 'args.self.network.uuid',
'network_name': 'args.self.network.name',
'cidr': 'args.self.cidr',
'gateway_address': 'args.self.gateway_address',
'dns_servers': 'args.self.dns_servers',
})
track_in_event_log('subnet.change-dns-servers.after', include={
**RESOURCE_ID,
**RESULT,
'dns_servers': 'args.self.dns_servers',
})
# Keep track of custom images
track_in_event_log('custom-image.import.after', include={
**RESOURCE_ID,
**RESULT,
'url': 'args.self.url',
'slug': 'args.self.slug',
'user_data_handling': 'args.self.user_data_handling',
'zones': 'args.self.zones',
'source_format': 'args.self.source_format',
})