channel update exporter: export current revision
Eelco Dolstra committed
Dec 30, 2019 at 20:48 UTC
29b04840421726f64955470ea530005c82d06997
1 file changed
+26
-6
delft/eris/channel-exporter.py
+26
-6
@@ -2,12 +2,22 @@
2
3
import requests
4
from dateutil.parser import parse
5
-from prometheus_client import Counter, Histogram, Gauge, start_http_server
5
+from prometheus_client import Counter, Histogram, Gauge, start_http_server, REGISTRY
6
import time
7
import sys
8
from pprint import pprint
9
import json
10
11
+
12
+def new_revision_registry():
13
+ return Gauge(
14
+ "channel_revision",
15
+ "Current revision, exported as a hack",
16
+ ["channel", "revision"],
17
+ registry=None
18
+ )
19
+
20
+
21
CHANNEL_REQUEST_TIME = Histogram(
22
"channel_request_time", "Time spent requesting channel data"
23
)
@@ -26,13 +36,15 @@ CHANNEL_REQUEST_FAILURES = Counter(
36
"Number of channel status requests which have failed",
37
)
38
29
-
39
@CHANNEL_REQUEST_TIME.time()
40
def measure_channel(name):
41
try:
42
with CHANNEL_REQUEST_FAILURES.count_exceptions():
34
- result = requests.get(f"https://nixos.org/channels/{name}")
35
- return parse(result.headers["last-modified"]).timestamp()
43
+ result = requests.get(f"https://nixos.org/channels/{name}/git-revision")
44
+ return {
45
+ "timestamp": parse(result.headers["last-modified"]).timestamp(),
46
+ "revision": result.text
47
+ }
48
except Exception as e:
49
pprint(e)
50
@@ -43,10 +55,18 @@ if __name__ == "__main__":
55
with open(sys.argv[1]) as channel_data:
56
channels = json.load(channel_data)
57
58
+ previous_channel_revision = None
59
while True:
60
+ CHANNEL_REVISION = new_revision_registry()
61
for (channel, about) in channels.items():
62
measurement = measure_channel(channel)
63
if measurement is not None:
50
- CHANNEL_UPDATE_TIME.labels(channel=channel).set(measurement)
64
+ CHANNEL_UPDATE_TIME.labels(channel=channel).set(measurement['timestamp'])
65
+ CHANNEL_REVISION.labels(channel=channel, revision=measurement['revision']).set(1)
66
CHANNEL_CURRENT.labels(channel=channel).set(int(about['current']))
52
- time.sleep(60)
67
+
68
+ if previous_channel_revision is not None:
69
+ REGISTRY.unregister(previous_channel_revision)
70
+ REGISTRY.register(CHANNEL_REVISION)
71
+ previous_channel_revision = CHANNEL_REVISION
72
+ time.sleep(55)