summaryrefslogtreecommitdiff
path: root/slog-errors
blob: f4d0a0e84eb61b83fefdbfbb62013546eefa38d1 (plain)
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
#!/usr/bin/env python3
#
# Read Qvarn structured log files, look for errors, and output all
# messages related to a failed HTTP request (status code 5xx).


import json
import sys


def parse_log_file(filename):
    with open(filename) as f:
        for line in f:
            yield json.loads(line)


def find_contexts(msgs):
    return set(get_context(msg) for msg in msgs)


def get_context(msg):
    return msg['_context'], msg['_process_id'], msg['_thread_id']


def is_in_context(msg, context):
    return get_context(msg) == context


def filter_msgs(msgs, func):
    for msg in msgs:
        if func(msg):
            yield msg

def has_error_status(msgs):
    for msg in msgs:
        if msg['msg_type'] == 'error':
            return True
        if msg['msg_type'] == 'http-response' and msg['status'] >= 400:
            return True
        if '_traceback' in msg:
            return True
    return False


def show_msgs(msgs):
    sys.stdout.write(json.dumps(msgs))
    sys.stdout.write('\n')


msgs = []
for filename in sys.argv[1:]:
    for msg in parse_log_file(filename):
        msg['_filename'] = filename
        msgs.append(msg)


contexts = find_contexts(msgs)
for context in contexts:
    context_msgs = list(filter_msgs(msgs, lambda m: is_in_context(m, context)))
    if has_error_status(context_msgs):
        show_msgs(context_msgs)