Skip to content

Commit 246f699

Browse files
authored
feat: test against semaphore (#17)
* feat: test against semaphore * chore: refactor stripping module into event module * fix: install rust non-interactively * fix: add rust to path * chore: travis cache * fix: encoding bug under py3
1 parent c6bfd12 commit 246f699

File tree

15 files changed

+242
-120
lines changed

15 files changed

+242
-120
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ ignore = E203, E266, E501, W503, E402, E731
33
max-line-length = 80
44
max-complexity = 18
55
select = B,C,E,F,W,T4,B9
6+
exclude=checkouts,lol*,.tox

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ venv
1818
.vscode/tags
1919
.pytest_cache
2020
.hypothesis
21+
checkouts

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ python:
99
- "3.6"
1010
- "3.7-dev"
1111

12+
cache:
13+
pip: true
14+
directories:
15+
- checkouts/semaphore/target/debug/
16+
- ~/.cargo/registry/
17+
- ~/.rustup/
18+
1219
branches:
1320
only:
1421
- master
@@ -26,7 +33,10 @@ matrix:
2633
- zeus upload -t "application/zip+wheel" dist/*
2734

2835
install:
36+
- curl https://sh.rustup.rs -sSf | sh -s -- -y
37+
- . $HOME/.cargo/env
2938
- pip install tox
39+
- sh scripts/checkout-semaphore.sh
3040

3141
script:
3242
- sh scripts/runtox.sh

pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.black]
2+
exclude = '''
3+
/(
4+
\.git
5+
| \.hg
6+
| \.mypy_cache
7+
| \.tox
8+
| \.venv
9+
| _build
10+
| buck-out
11+
| build
12+
| dist
13+
| checkouts
14+
)/
15+
'''

scripts/checkout-semaphore.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
3+
# This script is able to restore partially checked out repositories,
4+
# repositories that have all files but no git content, repositories checked out
5+
# on the wrong branch, etc.
6+
#
7+
# This is mainly useful because Travis creates an empty folder in the attempt
8+
# to restore the build cache.
9+
10+
set -xe
11+
12+
if [ ! -d checkouts/semaphore/.git ]; then
13+
mkdir -p checkouts/semaphore/
14+
fi
15+
16+
cd checkouts/semaphore/
17+
git init
18+
git remote remove origin || true
19+
git remote add origin https://github.com/getsentry/semaphore
20+
git fetch
21+
git reset --hard origin/master
22+
git clean -f
23+
cargo build

sentry_sdk/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import random
44
import atexit
55

6-
from .utils import Dsn, SkipEvent, ContextVar, Event
6+
from .utils import Dsn, SkipEvent, ContextVar
77
from .transport import Transport
88
from .consts import DEFAULT_OPTIONS, SDK_INFO
9-
from .stripping import strip_event, flatten_metadata
9+
from .event import strip_event, flatten_metadata, Event
1010

1111

1212
NO_DSN = object()

sentry_sdk/stripping.py renamed to sentry_sdk/event.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,70 @@
1+
import uuid
2+
from datetime import datetime
3+
14
from collections import Mapping, Sequence
25

6+
from .utils import exceptions_from_error_tuple
37
from ._compat import text_type
48

59

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+
668
class AnnotatedValue(object):
769
def __init__(self, value, metadata):
870
self.value = value

sentry_sdk/hub.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from ._compat import with_metaclass
66
from .scope import Scope
7-
from .utils import Event, skip_internal_frames, ContextVar
7+
from .utils import skip_internal_frames, ContextVar
8+
from .event import Event
89

910

1011
_local = ContextVar("sentry_current_hub")

sentry_sdk/integrations/_wsgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22

33
from sentry_sdk.hub import _should_send_default_pii
4-
from sentry_sdk.stripping import AnnotatedValue
4+
from sentry_sdk.event import AnnotatedValue
55

66

77
def get_environ(environ):

sentry_sdk/integrations/logging.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
import sys
55
import logging
6+
import datetime
67

78
from sentry_sdk import get_current_hub, capture_event, add_breadcrumb
8-
from sentry_sdk.utils import to_string, Event, skip_internal_frames
9+
from sentry_sdk.utils import to_string, skip_internal_frames
10+
from sentry_sdk.event import Event, datetime_to_json
911
from sentry_sdk.hub import _internal_exceptions
1012

1113
from . import Integration
@@ -51,6 +53,9 @@ def _breadcrumb_from_record(self, record):
5153
"level": self._logging_to_event_level(record.levelname),
5254
"category": record.name,
5355
"message": record.message,
56+
"timestamp": datetime_to_json(
57+
datetime.datetime.fromtimestamp(record.created)
58+
),
5459
}
5560

5661
def _emit(self, record):

0 commit comments

Comments
 (0)