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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
import pytest
from unittest import mock
import sentry_sdk
from sentry_sdk.hub import Hub
from sentry_sdk.integrations import iter_default_integrations
from sentry_sdk.scrubber import EventScrubber, DEFAULT_DENYLIST
"""
Those tests are meant to check the compatibility of the new scopes in SDK 2.0 with the old Hub/Scope system in SDK 1.x.
Those tests have been run with the latest SDK 1.x version and the data used in the `assert` statements represents
the behvaior of the SDK 1.x.
This makes sure that we are backwards compatible. (on a best effort basis, there will probably be some edge cases that are not covered here)
"""
@pytest.fixture
def integrations():
return [
integration.identifier
for integration in iter_default_integrations(
with_auto_enabling_integrations=False
)
]
@pytest.fixture
def expected_error(integrations):
def create_expected_error_event(trx, span):
return {
"level": "warning-X",
"exception": {
"values": [
{
"mechanism": {"type": "generic", "handled": True},
"module": None,
"type": "ValueError",
"value": "This is a test exception",
"stacktrace": {
"frames": [
{
"filename": "tests/new_scopes_compat/test_new_scopes_compat_event.py",
"abs_path": mock.ANY,
"function": "_faulty_function",
"module": "tests.new_scopes_compat.test_new_scopes_compat_event",
"lineno": mock.ANY,
"pre_context": [
" return create_expected_transaction_event",
"",
"",
"def _faulty_function():",
" try:",
],
"context_line": ' raise ValueError("This is a test exception")',
"post_context": [
" except ValueError as ex:",
" sentry_sdk.capture_exception(ex)",
"",
"",
"def _test_before_send(event, hint):",
],
"vars": {
"ex": mock.ANY,
},
"in_app": True,
}
]
},
}
]
},
"event_id": mock.ANY,
"timestamp": mock.ANY,
"contexts": {
"character": {
"name": "Mighty Fighter changed by before_send",
"age": 19,
"attack_type": "melee",
},
"trace": {
"trace_id": trx.trace_id,
"span_id": span.span_id,
"parent_span_id": span.parent_span_id,
"op": "test_span",
"origin": "manual",
"description": None,
"data": {
"thread.id": mock.ANY,
"thread.name": "MainThread",
},
},
"runtime": {
"name": "CPython",
"version": mock.ANY,
"build": mock.ANY,
},
},
"user": {
"id": "123",
"email": "jane.doe@example.com",
"ip_address": "[Filtered]",
},
"transaction": "test_transaction",
"transaction_info": {"source": "custom"},
"tags": {"tag1": "tag1_value", "tag2": "tag2_value"},
"extra": {
"extra1": "extra1_value",
"extra2": "extra2_value",
"should_be_removed_by_event_scrubber": "[Filtered]",
"sys.argv": "[Filtered]",
},
"breadcrumbs": {
"values": [
{
"category": "error-level",
"message": "Authenticated user %s",
"level": "error",
"data": {"breadcrumb2": "somedata"},
"timestamp": mock.ANY,
"type": "default",
}
]
},
"modules": mock.ANY,
"release": "0.1.2rc3",
"environment": "checking-compatibility-with-sdk1",
"server_name": mock.ANY,
"sdk": {
"name": "sentry.python",
"version": mock.ANY,
"packages": [{"name": "pypi:sentry-sdk", "version": mock.ANY}],
"integrations": integrations,
},
"platform": "python",
"_meta": {
"user": {"ip_address": {"": {"rem": [["!config", "s"]]}}},
"extra": {
"should_be_removed_by_event_scrubber": {
"": {"rem": [["!config", "s"]]}
},
"sys.argv": {"": {"rem": [["!config", "s"]]}},
},
},
}
return create_expected_error_event
@pytest.fixture
def expected_transaction(integrations):
def create_expected_transaction_event(trx, span):
return {
"type": "transaction",
"transaction": "test_transaction changed by before_send_transaction",
"transaction_info": {"source": "custom"},
"contexts": {
"trace": {
"trace_id": trx.trace_id,
"span_id": trx.span_id,
"parent_span_id": None,
"op": "test_transaction_op",
"origin": "manual",
"description": None,
"data": {
"thread.id": mock.ANY,
"thread.name": "MainThread",
},
},
"character": {
"name": "Mighty Fighter changed by before_send_transaction",
"age": 19,
"attack_type": "melee",
},
"runtime": {
"name": "CPython",
"version": mock.ANY,
"build": mock.ANY,
},
},
"tags": {"tag1": "tag1_value", "tag2": "tag2_value"},
"timestamp": mock.ANY,
"start_timestamp": mock.ANY,
"spans": [
{
"data": {
"thread.id": mock.ANY,
"thread.name": "MainThread",
},
"trace_id": trx.trace_id,
"span_id": span.span_id,
"parent_span_id": span.parent_span_id,
"same_process_as_parent": True,
"op": "test_span",
"origin": "manual",
"description": None,
"start_timestamp": mock.ANY,
"timestamp": mock.ANY,
}
],
"measurements": {"memory_used": {"value": 456, "unit": "byte"}},
"event_id": mock.ANY,
"level": "warning-X",
"user": {
"id": "123",
"email": "jane.doe@example.com",
"ip_address": "[Filtered]",
},
"extra": {
"extra1": "extra1_value",
"extra2": "extra2_value",
"should_be_removed_by_event_scrubber": "[Filtered]",
"sys.argv": "[Filtered]",
},
"release": "0.1.2rc3",
"environment": "checking-compatibility-with-sdk1",
"server_name": mock.ANY,
"sdk": {
"name": "sentry.python",
"version": mock.ANY,
"packages": [{"name": "pypi:sentry-sdk", "version": mock.ANY}],
"integrations": integrations,
},
"platform": "python",
"_meta": {
"user": {"ip_address": {"": {"rem": [["!config", "s"]]}}},
"extra": {
"should_be_removed_by_event_scrubber": {
"": {"rem": [["!config", "s"]]}
},
"sys.argv": {"": {"rem": [["!config", "s"]]}},
},
},
}
return create_expected_transaction_event
def _faulty_function():
try:
raise ValueError("This is a test exception")
except ValueError as ex:
sentry_sdk.capture_exception(ex)
def _test_before_send(event, hint):
event["contexts"]["character"]["name"] += " changed by before_send"
return event
def _test_before_send_transaction(event, hint):
event["transaction"] += " changed by before_send_transaction"
event["contexts"]["character"]["name"] += " changed by before_send_transaction"
return event
def _test_before_breadcrumb(breadcrumb, hint):
if breadcrumb["category"] == "info-level":
return None
return breadcrumb
def _generate_event_data(scope=None):
"""
Generates some data to be used in the events sent by the tests.
"""
sentry_sdk.set_level("warning-X")
sentry_sdk.add_breadcrumb(
category="info-level",
message="Authenticated user %s",
level="info",
data={"breadcrumb1": "somedata"},
)
sentry_sdk.add_breadcrumb(
category="error-level",
message="Authenticated user %s",
level="error",
data={"breadcrumb2": "somedata"},
)
sentry_sdk.set_context(
"character",
{
"name": "Mighty Fighter",
"age": 19,
"attack_type": "melee",
},
)
sentry_sdk.set_extra("extra1", "extra1_value")
sentry_sdk.set_extra("extra2", "extra2_value")
sentry_sdk.set_extra("should_be_removed_by_event_scrubber", "XXX")
sentry_sdk.set_tag("tag1", "tag1_value")
sentry_sdk.set_tag("tag2", "tag2_value")
sentry_sdk.set_user(
{"id": "123", "email": "jane.doe@example.com", "ip_address": "211.161.1.124"}
)
sentry_sdk.set_measurement("memory_used", 456, "byte")
if scope is not None:
scope.add_attachment(bytes=b"Hello World", filename="hello.txt")
def _init_sentry_sdk(sentry_init):
sentry_init(
environment="checking-compatibility-with-sdk1",
release="0.1.2rc3",
before_send=_test_before_send,
before_send_transaction=_test_before_send_transaction,
before_breadcrumb=_test_before_breadcrumb,
event_scrubber=EventScrubber(
denylist=DEFAULT_DENYLIST
+ ["should_be_removed_by_event_scrubber", "sys.argv"]
),
send_default_pii=False,
traces_sample_rate=1.0,
auto_enabling_integrations=False,
)
#
# The actual Tests start here!
#
def test_event(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)
envelopes = capture_envelopes()
with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
with sentry_sdk.configure_scope() as scope: # configure scope
_generate_event_data(scope)
_faulty_function()
(error_envelope, transaction_envelope) = envelopes
error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]
assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"
def test_event2(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)
envelopes = capture_envelopes()
with Hub(Hub.current):
sentry_sdk.set_tag("A", 1) # will not be added
with Hub.current: # with hub
with sentry_sdk.push_scope() as scope:
scope.set_tag("B", 1) # will not be added
with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
with sentry_sdk.configure_scope() as scope: # configure scope
_generate_event_data(scope)
_faulty_function()
(error_envelope, transaction_envelope) = envelopes
error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]
assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"
def test_event3(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)
envelopes = capture_envelopes()
with Hub(Hub.current):
sentry_sdk.set_tag("A", 1) # will not be added
with Hub.current: # with hub
with sentry_sdk.push_scope() as scope:
scope.set_tag("B", 1) # will not be added
with sentry_sdk.push_scope() as scope: # push scope
with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
_generate_event_data(scope)
_faulty_function()
(error_envelope, transaction_envelope) = envelopes
error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]
assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"
def test_event4(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)
envelopes = capture_envelopes()
with Hub(Hub.current):
sentry_sdk.set_tag("A", 1) # will not be added
with Hub(Hub.current): # with hub clone
with sentry_sdk.push_scope() as scope:
scope.set_tag("B", 1) # will not be added
with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
with sentry_sdk.configure_scope() as scope: # configure scope
_generate_event_data(scope)
_faulty_function()
(error_envelope, transaction_envelope) = envelopes
error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]
assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"
def test_event5(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)
envelopes = capture_envelopes()
with Hub(Hub.current):
sentry_sdk.set_tag("A", 1) # will not be added
with Hub(Hub.current): # with hub clone
with sentry_sdk.push_scope() as scope:
scope.set_tag("B", 1) # will not be added
with sentry_sdk.push_scope() as scope: # push scope
with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
_generate_event_data(scope)
_faulty_function()
(error_envelope, transaction_envelope) = envelopes
error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]
assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"
|