-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect-exceptions
executable file
·79 lines (63 loc) · 2.02 KB
/
detect-exceptions
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
#!/usr/bin/env python
import sys
import time
import json
import threading
import signal
import optparse
exceptions = {}
in_exception = False
exception_lines = []
running_lock = threading.Lock()
parser = optparse.OptionParser(usage="Usage: %prog [options]")
parser.add_option('-n', '--namespace', action="store", dest="ns", help="ns parameter of the json output.")
parser.add_option('-i', '--interval', action="store", dest="interval",
help="How often (in seconds) should flush exceptions.", default=5)
options, args = parser.parse_args()
def exception_ended():
global in_exception, exception_lines, running_lock
exception_string = "\n".join(exception_lines)
running_lock.acquire(True)
try:
exceptions[exception_string] = exceptions.get(exception_string, 0) + 1
finally:
running_lock.release()
exception_lines = []
in_exception = False
def process_input():
global in_exception, exception_lines
for line in sys.stdin:
line = line.rstrip()
if not in_exception and (line.startswith("Error") or line.startswith("Exception")):
in_exception = True
exception_lines.append(line)
continue
if in_exception:
if line.startswith(' '):
exception_lines.append(line)
else:
exception_ended()
in_exception = False
flush()
def print_exceptions():
global exceptions, running_lock
interval = float(options.interval)
while True:
time.sleep(interval)
flush()
def flush():
global exceptions, running_lock
localExceptions = {}
running_lock.acquire(True)
try:
localExceptions = exceptions
exceptions = {}
finally:
running_lock.release()
for exception, count in localExceptions.items():
print(json.dumps({"ns": options.ns, "count": count, "exception": exception}))
sys.stdout.flush()
thread = threading.Thread(target=print_exceptions)
thread.daemon = True
thread.start()
process_input()