|
| 1 | +import uuid |
| 2 | +from datetime import datetime |
| 3 | + |
1 | 4 | from collections import Mapping, Sequence
|
2 | 5 |
|
| 6 | +from .utils import exceptions_from_error_tuple |
3 | 7 | from ._compat import text_type
|
4 | 8 |
|
5 | 9 |
|
| 10 | +def datetime_to_json(dt): |
| 11 | + return dt.strftime("%Y-%m-%dT%H:%M:%SZ") |
| 12 | + |
| 13 | + |
| 14 | +class Event(Mapping): |
| 15 | + __slots__ = ("_data", "_exc_value") |
| 16 | + |
| 17 | + def __init__(self, data={}): |
| 18 | + self._data = { |
| 19 | + "event_id": uuid.uuid4().hex, |
| 20 | + "timestamp": datetime_to_json(datetime.utcnow()), |
| 21 | + "level": "error", |
| 22 | + } |
| 23 | + |
| 24 | + self._data.update(data) |
| 25 | + |
| 26 | + self._exc_value = None |
| 27 | + |
| 28 | + def set_exception(self, exc_type, exc_value, tb, with_locals): |
| 29 | + self["exception"] = { |
| 30 | + "values": exceptions_from_error_tuple(exc_type, exc_value, tb, with_locals) |
| 31 | + } |
| 32 | + self._exc_value = exc_value |
| 33 | + |
| 34 | + def __getitem__(self, key): |
| 35 | + return self._data[key] |
| 36 | + |
| 37 | + def __contains__(self, key): |
| 38 | + return key in self._data |
| 39 | + |
| 40 | + def get(self, *a, **kw): |
| 41 | + return self._data.get(*a, **kw) |
| 42 | + |
| 43 | + def setdefault(self, *a, **kw): |
| 44 | + return self._data.setdefault(*a, **kw) |
| 45 | + |
| 46 | + def __setitem__(self, key, value): |
| 47 | + self._data[key] = value |
| 48 | + |
| 49 | + def __iter__(self): |
| 50 | + return iter(self._data) |
| 51 | + |
| 52 | + def __len__(self): |
| 53 | + return len(self._data) |
| 54 | + |
| 55 | + def iter_frames(self): |
| 56 | + stacktraces = [] |
| 57 | + if "stacktrace" in self: |
| 58 | + stacktraces.append(self["stacktrace"]) |
| 59 | + if "exception" in self: |
| 60 | + for exception in self["exception"].get("values") or (): |
| 61 | + if "stacktrace" in exception: |
| 62 | + stacktraces.append(exception["stacktrace"]) |
| 63 | + for stacktrace in stacktraces: |
| 64 | + for frame in stacktrace.get("frames") or (): |
| 65 | + yield frame |
| 66 | + |
| 67 | + |
6 | 68 | class AnnotatedValue(object):
|
7 | 69 | def __init__(self, value, metadata):
|
8 | 70 | self.value = value
|
|
0 commit comments