forked from nats-io/latency-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_exporter.py
56 lines (47 loc) · 1.53 KB
/
metrics_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
# -*- coding: utf-8 -*-
import flask
import os
import sys
import ast
import json
import argparse
app = flask.Flask(__name__)
filename = ''
@app.route('/latency_metrics')
def get_latency_percentiles():
""" Retrieves the last saved latency hdr histogram percentiles
and the average latency
Args:
-
Returns:
dict: A JSON object containing the metrics
"""
status = 200
return flask.Response(get_stats_json(),
status=status,
mimetype='application/json')
def get_stats_json():
try:
f = open(filename, 'r')
line = f.readline()
percentiles_dict = {}
while line:
percentile = line.split(' ')[0]
latency = float(line.split(' ')[1])
percentiles_dict[percentile] = latency
line = f.readline()
js = json.dumps(percentiles_dict, indent=2)
f.close()
return js
except Exception as e:
print("An exception occurred")
print(str(e))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Give configuration options')
parser.add_argument('--filename', metavar='filename', type=str,
help='the filename from which will retrieve the metrics')
parser.add_argument('--port', metavar='port', type=int, default=5000,
help='Server port (default 5000)')
args = parser.parse_args()
filename = args.filename
app.run(host='0.0.0.0', port=args.port, debug=True)