-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime_latency.py
47 lines (38 loc) · 1.29 KB
/
realtime_latency.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
# coding: utf-8
'''
Upstream latency reporter
==========================
This example allows you to collect upstream response time from nginx access log with the following log_format:
log_format with_latency '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_x_forwarded_for" $upstream_response_time';
'''
import re
import sys
import time
import random
import datetime
from dotter import dotter
def main(path, size):
publisher = dotter.Dotter.create(path, size)
ts_current = time.time()
values = [0]
while True:
line = sys.stdin.readline()
timestamp, value = re.match(r'^.*\[(.*)\].* ([^ ]*)$', line).groups()
timestamp = time.mktime(
datetime.datetime.strptime(
timestamp.split()[0], r'%d/%b/%Y:%H:%M:%S').utctimetuple())
try:
value = float(value)
except ValueError:
continue
if timestamp == ts_current:
values.append(value)
else:
publisher.dot(ts_current, sum(values) / len(values))
ts_current = timestamp
values = [value]
if __name__ == '__main__':
main(sys.argv[1], int(sys.argv[2]))