forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sessions.py
More file actions
121 lines (92 loc) · 3.18 KB
/
test_sessions.py
File metadata and controls
121 lines (92 loc) · 3.18 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sentry_sdk
from sentry_sdk import Hub
from sentry_sdk.sessions import auto_session_tracking
def sorted_aggregates(item):
aggregates = item["aggregates"]
aggregates.sort(key=lambda item: (item["started"], item.get("did", "")))
return aggregates
def test_basic(sentry_init, capture_envelopes):
sentry_init(release="fun-release", environment="not-fun-env")
envelopes = capture_envelopes()
hub = Hub.current
hub.start_session()
try:
with hub.configure_scope() as scope:
scope.set_user({"id": "42"})
raise Exception("all is wrong")
except Exception:
hub.capture_exception()
hub.end_session()
hub.flush()
assert len(envelopes) == 2
assert envelopes[0].get_event() is not None
sess = envelopes[1]
assert len(sess.items) == 1
sess_event = sess.items[0].payload.json
assert sess_event["attrs"] == {
"release": "fun-release",
"environment": "not-fun-env",
}
assert sess_event["did"] == "42"
assert sess_event["init"]
assert sess_event["status"] == "exited"
assert sess_event["errors"] == 1
def test_aggregates(sentry_init, capture_envelopes):
sentry_init(
release="fun-release",
environment="not-fun-env",
)
envelopes = capture_envelopes()
hub = Hub.current
with auto_session_tracking(session_mode="request"):
with sentry_sdk.push_scope():
try:
with sentry_sdk.configure_scope() as scope:
scope.set_user({"id": "42"})
raise Exception("all is wrong")
except Exception:
sentry_sdk.capture_exception()
with auto_session_tracking(session_mode="request"):
pass
hub.start_session(session_mode="request")
hub.end_session()
sentry_sdk.flush()
assert len(envelopes) == 2
assert envelopes[0].get_event() is not None
sess = envelopes[1]
assert len(sess.items) == 1
sess_event = sess.items[0].payload.json
assert sess_event["attrs"] == {
"release": "fun-release",
"environment": "not-fun-env",
}
aggregates = sorted_aggregates(sess_event)
assert len(aggregates) == 1
assert aggregates[0]["exited"] == 2
assert aggregates[0]["errored"] == 1
def test_aggregates_explicitly_disabled_session_tracking_request_mode(
sentry_init, capture_envelopes
):
sentry_init(
release="fun-release", environment="not-fun-env", auto_session_tracking=False
)
envelopes = capture_envelopes()
hub = Hub.current
with auto_session_tracking(session_mode="request"):
with sentry_sdk.push_scope():
try:
raise Exception("all is wrong")
except Exception:
sentry_sdk.capture_exception()
with auto_session_tracking(session_mode="request"):
pass
hub.start_session(session_mode="request")
hub.end_session()
sentry_sdk.flush()
sess = envelopes[1]
assert len(sess.items) == 1
sess_event = sess.items[0].payload.json
aggregates = sorted_aggregates(sess_event)
assert len(aggregates) == 1
assert aggregates[0]["exited"] == 1
assert "errored" not in aggregates[0]