|
1 | 1 | from unittest import mock
|
2 | 2 |
|
3 | 3 | import sentry_sdk
|
4 |
| -from sentry_sdk.sessions import auto_session_tracking |
| 4 | +from sentry_sdk.sessions import auto_session_tracking, track_session |
5 | 5 |
|
6 | 6 |
|
7 | 7 | def sorted_aggregates(item):
|
@@ -50,6 +50,48 @@ def test_aggregates(sentry_init, capture_envelopes):
|
50 | 50 | )
|
51 | 51 | envelopes = capture_envelopes()
|
52 | 52 |
|
| 53 | + with sentry_sdk.isolation_scope() as scope: |
| 54 | + with track_session(scope, session_mode="request"): |
| 55 | + try: |
| 56 | + scope.set_user({"id": "42"}) |
| 57 | + raise Exception("all is wrong") |
| 58 | + except Exception: |
| 59 | + sentry_sdk.capture_exception() |
| 60 | + |
| 61 | + with sentry_sdk.isolation_scope() as scope: |
| 62 | + with track_session(scope, session_mode="request"): |
| 63 | + pass |
| 64 | + |
| 65 | + sentry_sdk.get_isolation_scope().start_session(session_mode="request") |
| 66 | + sentry_sdk.get_isolation_scope().end_session() |
| 67 | + sentry_sdk.flush() |
| 68 | + |
| 69 | + assert len(envelopes) == 2 |
| 70 | + assert envelopes[0].get_event() is not None |
| 71 | + |
| 72 | + sess = envelopes[1] |
| 73 | + assert len(sess.items) == 1 |
| 74 | + sess_event = sess.items[0].payload.json |
| 75 | + assert sess_event["attrs"] == { |
| 76 | + "release": "fun-release", |
| 77 | + "environment": "not-fun-env", |
| 78 | + } |
| 79 | + |
| 80 | + aggregates = sorted_aggregates(sess_event) |
| 81 | + assert len(aggregates) == 1 |
| 82 | + assert aggregates[0]["exited"] == 2 |
| 83 | + assert aggregates[0]["errored"] == 1 |
| 84 | + |
| 85 | + |
| 86 | +def test_aggregates_deprecated( |
| 87 | + sentry_init, capture_envelopes, suppress_deprecation_warnings |
| 88 | +): |
| 89 | + sentry_init( |
| 90 | + release="fun-release", |
| 91 | + environment="not-fun-env", |
| 92 | + ) |
| 93 | + envelopes = capture_envelopes() |
| 94 | + |
53 | 95 | with auto_session_tracking(session_mode="request"):
|
54 | 96 | with sentry_sdk.new_scope() as scope:
|
55 | 97 | try:
|
@@ -90,6 +132,39 @@ def test_aggregates_explicitly_disabled_session_tracking_request_mode(
|
90 | 132 | )
|
91 | 133 | envelopes = capture_envelopes()
|
92 | 134 |
|
| 135 | + with sentry_sdk.isolation_scope() as scope: |
| 136 | + with track_session(scope, session_mode="request"): |
| 137 | + try: |
| 138 | + raise Exception("all is wrong") |
| 139 | + except Exception: |
| 140 | + sentry_sdk.capture_exception() |
| 141 | + |
| 142 | + with sentry_sdk.isolation_scope() as scope: |
| 143 | + with track_session(scope, session_mode="request"): |
| 144 | + pass |
| 145 | + |
| 146 | + sentry_sdk.get_isolation_scope().start_session(session_mode="request") |
| 147 | + sentry_sdk.get_isolation_scope().end_session() |
| 148 | + sentry_sdk.flush() |
| 149 | + |
| 150 | + sess = envelopes[1] |
| 151 | + assert len(sess.items) == 1 |
| 152 | + sess_event = sess.items[0].payload.json |
| 153 | + |
| 154 | + aggregates = sorted_aggregates(sess_event) |
| 155 | + assert len(aggregates) == 1 |
| 156 | + assert aggregates[0]["exited"] == 1 |
| 157 | + assert "errored" not in aggregates[0] |
| 158 | + |
| 159 | + |
| 160 | +def test_aggregates_explicitly_disabled_session_tracking_request_mode_deprecated( |
| 161 | + sentry_init, capture_envelopes, suppress_deprecation_warnings |
| 162 | +): |
| 163 | + sentry_init( |
| 164 | + release="fun-release", environment="not-fun-env", auto_session_tracking=False |
| 165 | + ) |
| 166 | + envelopes = capture_envelopes() |
| 167 | + |
93 | 168 | with auto_session_tracking(session_mode="request"):
|
94 | 169 | with sentry_sdk.new_scope():
|
95 | 170 | try:
|
@@ -120,6 +195,35 @@ def test_no_thread_on_shutdown_no_errors(sentry_init):
|
120 | 195 | environment="not-fun-env",
|
121 | 196 | )
|
122 | 197 |
|
| 198 | + # make it seem like the interpreter is shutting down |
| 199 | + with mock.patch( |
| 200 | + "threading.Thread.start", |
| 201 | + side_effect=RuntimeError("can't create new thread at interpreter shutdown"), |
| 202 | + ): |
| 203 | + with sentry_sdk.isolation_scope() as scope: |
| 204 | + with track_session(scope, session_mode="request"): |
| 205 | + try: |
| 206 | + raise Exception("all is wrong") |
| 207 | + except Exception: |
| 208 | + sentry_sdk.capture_exception() |
| 209 | + |
| 210 | + with sentry_sdk.isolation_scope() as scope: |
| 211 | + with track_session(scope, session_mode="request"): |
| 212 | + pass |
| 213 | + |
| 214 | + sentry_sdk.get_isolation_scope().start_session(session_mode="request") |
| 215 | + sentry_sdk.get_isolation_scope().end_session() |
| 216 | + sentry_sdk.flush() |
| 217 | + |
| 218 | + |
| 219 | +def test_no_thread_on_shutdown_no_errors_deprecated( |
| 220 | + sentry_init, suppress_deprecation_warnings |
| 221 | +): |
| 222 | + sentry_init( |
| 223 | + release="fun-release", |
| 224 | + environment="not-fun-env", |
| 225 | + ) |
| 226 | + |
123 | 227 | # make it seem like the interpreter is shutting down
|
124 | 228 | with mock.patch(
|
125 | 229 | "threading.Thread.start",
|
|
0 commit comments