-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrf_prometheus_listener.py
44 lines (33 loc) · 1.32 KB
/
rf_prometheus_listener.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
"""
Listener that posts results to Prometheus
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#listener-interface
"""
import os
import re
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
ROBOT_LISTENER_API_VERSION = 3
PUSH_GATEWAY = os.environ['PUSH_GATEWAY']
JOB_NAME = os.environ.get('PROMETHEUS_JOB_NAME', 'robot_framework_tests')
registry = CollectorRegistry()
g = Gauge('robot_framework_test_passed', 'Result from a Robot Framework test.', ['name'], registry=registry)
gt = Gauge('robot_framework_test_suite_timestamp', 'Timestamp of a Robot Framework test suite.', ['name'], registry=registry)
def slugify(text):
text = text.lower()
return re.sub(r'[\W_]+', '_', text)
def end_suite(suite, result):
# Record the timestamp of suite execution:
suite_name = slugify(result.name)
gt.labels(suite_name).set_to_current_time()
def end_test(test, result):
# Record the outcome of each test:
test_name = slugify(result.name)
if result.passed:
g.labels(test_name).set(1)
else:
g.labels(test_name).set(0)
def close():
# Push the results somewhere useful...
if PUSH_GATEWAY:
push_to_gateway(PUSH_GATEWAY, job=JOB_NAME, registry=registry)
else:
print("WARNING! No Push Gateway set, so no metrics have been published.")