|
2 | 2 |
|
3 | 3 | from ._compat import text_type
|
4 | 4 |
|
| 5 | + |
| 6 | +class AnnotatedValue(object): |
| 7 | + def __init__(self, value, metadata): |
| 8 | + self.value = value |
| 9 | + self.metadata = metadata |
| 10 | + |
| 11 | + |
| 12 | +def flatten_metadata(obj): |
| 13 | + def inner(obj): |
| 14 | + if isinstance(obj, Mapping): |
| 15 | + assert "" not in obj, "Merging metadata not supported" |
| 16 | + rv = {} |
| 17 | + meta = {} |
| 18 | + for k, v in obj.items(): |
| 19 | + rv[k], meta[k] = inner(v) |
| 20 | + if meta[k] is None: |
| 21 | + del meta[k] |
| 22 | + return rv, (meta or None) |
| 23 | + if isinstance(obj, Sequence) and not isinstance(obj, (text_type, bytes)): |
| 24 | + rv = [] |
| 25 | + meta = {} |
| 26 | + for i, v in enumerate(obj): |
| 27 | + new_v, meta[i] = inner(v) |
| 28 | + rv.append(new_v) |
| 29 | + if meta[i] is None: |
| 30 | + del meta[i] |
| 31 | + return rv, (meta or None) |
| 32 | + if isinstance(obj, AnnotatedValue): |
| 33 | + return obj.value, {"": obj.metadata} |
| 34 | + return obj, None |
| 35 | + |
| 36 | + obj, meta = inner(obj) |
| 37 | + if meta is not None: |
| 38 | + obj[''] = meta |
| 39 | + return obj |
| 40 | + |
| 41 | + |
5 | 42 | def strip_event(event):
|
6 |
| - assert "" not in event, "Merging metadata not supported" |
7 |
| - old_frames = event.get('stacktrace', {}).get('frames', []) |
8 |
| - new_frames = [] |
9 |
| - frames_meta = {} |
10 |
| - meta = {"stacktrace": {"frames": frames_meta}} |
| 43 | + old_frames = event.get('stacktrace', {}).get('frames', None) |
| 44 | + if old_frames: |
| 45 | + event['stacktrace']['frames'] = [strip_frame(frame) |
| 46 | + for frame in old_frames] |
11 | 47 |
|
12 |
| - for i, frame in old_frames: |
13 |
| - frame_meta, new_frame = strip_frame(frame) |
14 |
| - new_frames.append(new_frame) |
15 |
| - if frame_meta is not None: |
16 |
| - frames_meta[str(i)] = frame_meta |
| 48 | + old_request_data = event.get('request', {}).get('data', None) |
| 49 | + if old_request_data: |
| 50 | + event['request']['data'] = strip_databag(old_request_data) |
17 | 51 |
|
18 |
| - return event, meta |
| 52 | + return event |
19 | 53 |
|
20 | 54 | def strip_frame(frame):
|
21 | 55 | frame['vars'], meta = strip_databag(frame.get('vars'))
|
22 | 56 | return frame, ({"vars": meta} if meta is not None else None)
|
23 | 57 |
|
24 | 58 | def strip_databag(obj, remaining_depth=20):
|
| 59 | + assert not isinstance(obj, bytes), 'bytes should have been normalized before' |
25 | 60 | if remaining_depth <= 0:
|
26 |
| - return None, {"": {"rem": [["!dep", "x"]]}} |
| 61 | + return AnnotatedValue(None, {"": {"rem": [["!dep", "x"]]}}) |
27 | 62 | if isinstance(obj, text_type):
|
28 | 63 | return strip_string(obj)
|
29 | 64 | if isinstance(obj, Mapping):
|
30 |
| - rv = {} |
31 |
| - meta = {} |
32 |
| - for k, v in obj.items(): |
33 |
| - rv[k], v_meta = strip_databag(v, remaining_depth - 1) |
34 |
| - if v_meta is not None: |
35 |
| - meta[k] = v_meta |
36 |
| - return rv, meta |
| 65 | + return {k: strip_databag(v, remaining_depth - 1) |
| 66 | + for k, v in obj.items()} |
37 | 67 | if isinstance(obj, Sequence):
|
38 |
| - rv = [] |
39 |
| - meta = {} |
40 |
| - for i, v in enumerate(obj): |
41 |
| - new_v, v_meta = strip_databag(obj, remaining_depth - 1) |
42 |
| - rv.append(new_v) |
43 |
| - if v_meta is not None: |
44 |
| - meta[str(i)] = v_meta |
45 |
| - return rv, meta |
| 68 | + return [strip_databag(v, remaining_depth - 1) |
| 69 | + for v in obj] |
| 70 | + return obj |
46 | 71 |
|
47 |
| - return obj, None |
48 | 72 |
|
49 |
| - |
50 |
| -def strip_string(value, length=512): |
| 73 | +def strip_string(value, assume_length=None, max_length=512): |
| 74 | + # TODO: read max_length from config |
51 | 75 | if not value:
|
52 |
| - return value, None |
53 |
| - if len(value) > length: |
54 |
| - meta = { |
55 |
| - "": { |
56 |
| - "len": len(value), |
57 |
| - "rem": [["!len", "x", length, len(value)]] |
| 76 | + return value |
| 77 | + if assume_length is None: |
| 78 | + assume_length = len(value) |
| 79 | + |
| 80 | + if assume_length > max_length: |
| 81 | + return AnnotatedValue( |
| 82 | + value=value[:max_length - 3] + u'...', |
| 83 | + metadata={ |
| 84 | + "len": assume_length, |
| 85 | + "rem": [["!len", "x", max_length - 3, max_length]] |
58 | 86 | }
|
59 |
| - } |
60 |
| - return value[:length - 3] + '...', meta |
61 |
| - return value[:length], None |
| 87 | + ) |
| 88 | + return value[:max_length] |
0 commit comments