|
| 1 | +import os |
| 2 | +import datetime |
1 | 3 | import asyncio |
2 | 4 | from unittest import mock |
3 | 5 |
|
4 | 6 | import httpx |
5 | 7 | import pytest |
| 8 | +from contextlib import contextmanager |
6 | 9 |
|
7 | 10 | import sentry_sdk |
8 | 11 | from sentry_sdk import capture_message, start_transaction |
@@ -393,6 +396,312 @@ def test_omit_url_data_if_parsing_fails(sentry_init, capture_events, httpx_mock) |
393 | 396 | assert SPANDATA.HTTP_QUERY not in event["breadcrumbs"]["values"][0]["data"] |
394 | 397 |
|
395 | 398 |
|
| 399 | +@pytest.mark.parametrize( |
| 400 | + "httpx_client", |
| 401 | + (httpx.Client(), httpx.AsyncClient()), |
| 402 | +) |
| 403 | +def test_request_source_disabled(sentry_init, capture_events, httpx_client, httpx_mock): |
| 404 | + httpx_mock.add_response() |
| 405 | + sentry_init( |
| 406 | + integrations=[HttpxIntegration()], |
| 407 | + traces_sample_rate=1.0, |
| 408 | + enable_http_request_source=False, |
| 409 | + http_request_source_threshold_ms=0, |
| 410 | + ) |
| 411 | + |
| 412 | + events = capture_events() |
| 413 | + |
| 414 | + url = "http://example.com/" |
| 415 | + |
| 416 | + with start_transaction(name="test_transaction"): |
| 417 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 418 | + asyncio.get_event_loop().run_until_complete(httpx_client.get(url)) |
| 419 | + else: |
| 420 | + httpx_client.get(url) |
| 421 | + |
| 422 | + (event,) = events |
| 423 | + |
| 424 | + span = event["spans"][-1] |
| 425 | + assert span["description"].startswith("GET") |
| 426 | + |
| 427 | + data = span.get("data", {}) |
| 428 | + |
| 429 | + assert SPANDATA.CODE_LINENO not in data |
| 430 | + assert SPANDATA.CODE_NAMESPACE not in data |
| 431 | + assert SPANDATA.CODE_FILEPATH not in data |
| 432 | + assert SPANDATA.CODE_FUNCTION not in data |
| 433 | + |
| 434 | + |
| 435 | +@pytest.mark.parametrize("enable_http_request_source", [None, True]) |
| 436 | +@pytest.mark.parametrize( |
| 437 | + "httpx_client", |
| 438 | + (httpx.Client(), httpx.AsyncClient()), |
| 439 | +) |
| 440 | +def test_request_source_enabled( |
| 441 | + sentry_init, capture_events, enable_http_request_source, httpx_client, httpx_mock |
| 442 | +): |
| 443 | + httpx_mock.add_response() |
| 444 | + sentry_options = { |
| 445 | + "integrations": [HttpxIntegration()], |
| 446 | + "traces_sample_rate": 1.0, |
| 447 | + "http_request_source_threshold_ms": 0, |
| 448 | + } |
| 449 | + if enable_http_request_source is not None: |
| 450 | + sentry_options["enable_http_request_source"] = enable_http_request_source |
| 451 | + |
| 452 | + sentry_init(**sentry_options) |
| 453 | + |
| 454 | + events = capture_events() |
| 455 | + |
| 456 | + url = "http://example.com/" |
| 457 | + |
| 458 | + with start_transaction(name="test_transaction"): |
| 459 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 460 | + asyncio.get_event_loop().run_until_complete(httpx_client.get(url)) |
| 461 | + else: |
| 462 | + httpx_client.get(url) |
| 463 | + |
| 464 | + (event,) = events |
| 465 | + |
| 466 | + span = event["spans"][-1] |
| 467 | + assert span["description"].startswith("GET") |
| 468 | + |
| 469 | + data = span.get("data", {}) |
| 470 | + |
| 471 | + assert SPANDATA.CODE_LINENO in data |
| 472 | + assert SPANDATA.CODE_NAMESPACE in data |
| 473 | + assert SPANDATA.CODE_FILEPATH in data |
| 474 | + assert SPANDATA.CODE_FUNCTION in data |
| 475 | + |
| 476 | + |
| 477 | +@pytest.mark.parametrize( |
| 478 | + "httpx_client", |
| 479 | + (httpx.Client(), httpx.AsyncClient()), |
| 480 | +) |
| 481 | +def test_request_source(sentry_init, capture_events, httpx_client, httpx_mock): |
| 482 | + httpx_mock.add_response() |
| 483 | + |
| 484 | + sentry_init( |
| 485 | + integrations=[HttpxIntegration()], |
| 486 | + traces_sample_rate=1.0, |
| 487 | + enable_http_request_source=True, |
| 488 | + http_request_source_threshold_ms=0, |
| 489 | + ) |
| 490 | + |
| 491 | + events = capture_events() |
| 492 | + |
| 493 | + url = "http://example.com/" |
| 494 | + |
| 495 | + with start_transaction(name="test_transaction"): |
| 496 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 497 | + asyncio.get_event_loop().run_until_complete(httpx_client.get(url)) |
| 498 | + else: |
| 499 | + httpx_client.get(url) |
| 500 | + |
| 501 | + (event,) = events |
| 502 | + |
| 503 | + span = event["spans"][-1] |
| 504 | + assert span["description"].startswith("GET") |
| 505 | + |
| 506 | + data = span.get("data", {}) |
| 507 | + |
| 508 | + assert SPANDATA.CODE_LINENO in data |
| 509 | + assert SPANDATA.CODE_NAMESPACE in data |
| 510 | + assert SPANDATA.CODE_FILEPATH in data |
| 511 | + assert SPANDATA.CODE_FUNCTION in data |
| 512 | + |
| 513 | + assert type(data.get(SPANDATA.CODE_LINENO)) == int |
| 514 | + assert data.get(SPANDATA.CODE_LINENO) > 0 |
| 515 | + assert data.get(SPANDATA.CODE_NAMESPACE) == "tests.integrations.httpx.test_httpx" |
| 516 | + assert data.get(SPANDATA.CODE_FILEPATH).endswith( |
| 517 | + "tests/integrations/httpx/test_httpx.py" |
| 518 | + ) |
| 519 | + |
| 520 | + is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep |
| 521 | + assert is_relative_path |
| 522 | + |
| 523 | + assert data.get(SPANDATA.CODE_FUNCTION) == "test_request_source" |
| 524 | + |
| 525 | + |
| 526 | +@pytest.mark.parametrize( |
| 527 | + "httpx_client", |
| 528 | + (httpx.Client(), httpx.AsyncClient()), |
| 529 | +) |
| 530 | +def test_request_source_with_module_in_search_path( |
| 531 | + sentry_init, capture_events, httpx_client, httpx_mock |
| 532 | +): |
| 533 | + """ |
| 534 | + Test that request source is relative to the path of the module it ran in |
| 535 | + """ |
| 536 | + httpx_mock.add_response() |
| 537 | + sentry_init( |
| 538 | + integrations=[HttpxIntegration()], |
| 539 | + traces_sample_rate=1.0, |
| 540 | + enable_http_request_source=True, |
| 541 | + http_request_source_threshold_ms=0, |
| 542 | + ) |
| 543 | + |
| 544 | + events = capture_events() |
| 545 | + |
| 546 | + url = "http://example.com/" |
| 547 | + |
| 548 | + with start_transaction(name="test_transaction"): |
| 549 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 550 | + from httpx_helpers.helpers import async_get_request_with_client |
| 551 | + |
| 552 | + asyncio.get_event_loop().run_until_complete( |
| 553 | + async_get_request_with_client(httpx_client, url) |
| 554 | + ) |
| 555 | + else: |
| 556 | + from httpx_helpers.helpers import get_request_with_client |
| 557 | + |
| 558 | + get_request_with_client(httpx_client, url) |
| 559 | + |
| 560 | + (event,) = events |
| 561 | + |
| 562 | + span = event["spans"][-1] |
| 563 | + assert span["description"].startswith("GET") |
| 564 | + |
| 565 | + data = span.get("data", {}) |
| 566 | + |
| 567 | + assert SPANDATA.CODE_LINENO in data |
| 568 | + assert SPANDATA.CODE_NAMESPACE in data |
| 569 | + assert SPANDATA.CODE_FILEPATH in data |
| 570 | + assert SPANDATA.CODE_FUNCTION in data |
| 571 | + |
| 572 | + assert type(data.get(SPANDATA.CODE_LINENO)) == int |
| 573 | + assert data.get(SPANDATA.CODE_LINENO) > 0 |
| 574 | + assert data.get(SPANDATA.CODE_NAMESPACE) == "httpx_helpers.helpers" |
| 575 | + assert data.get(SPANDATA.CODE_FILEPATH) == "httpx_helpers/helpers.py" |
| 576 | + |
| 577 | + is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep |
| 578 | + assert is_relative_path |
| 579 | + |
| 580 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 581 | + assert data.get(SPANDATA.CODE_FUNCTION) == "async_get_request_with_client" |
| 582 | + else: |
| 583 | + assert data.get(SPANDATA.CODE_FUNCTION) == "get_request_with_client" |
| 584 | + |
| 585 | + |
| 586 | +@pytest.mark.parametrize( |
| 587 | + "httpx_client", |
| 588 | + (httpx.Client(), httpx.AsyncClient()), |
| 589 | +) |
| 590 | +def test_no_request_source_if_duration_too_short( |
| 591 | + sentry_init, capture_events, httpx_client, httpx_mock |
| 592 | +): |
| 593 | + httpx_mock.add_response() |
| 594 | + |
| 595 | + sentry_init( |
| 596 | + integrations=[HttpxIntegration()], |
| 597 | + traces_sample_rate=1.0, |
| 598 | + enable_http_request_source=True, |
| 599 | + http_request_source_threshold_ms=100, |
| 600 | + ) |
| 601 | + |
| 602 | + events = capture_events() |
| 603 | + |
| 604 | + url = "http://example.com/" |
| 605 | + |
| 606 | + with start_transaction(name="test_transaction"): |
| 607 | + |
| 608 | + @contextmanager |
| 609 | + def fake_start_span(*args, **kwargs): |
| 610 | + with sentry_sdk.start_span(*args, **kwargs) as span: |
| 611 | + pass |
| 612 | + span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0) |
| 613 | + span.timestamp = datetime.datetime(2024, 1, 1, microsecond=99999) |
| 614 | + yield span |
| 615 | + |
| 616 | + with mock.patch( |
| 617 | + "sentry_sdk.integrations.httpx.start_span", |
| 618 | + fake_start_span, |
| 619 | + ): |
| 620 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 621 | + asyncio.get_event_loop().run_until_complete(httpx_client.get(url)) |
| 622 | + else: |
| 623 | + httpx_client.get(url) |
| 624 | + |
| 625 | + (event,) = events |
| 626 | + |
| 627 | + span = event["spans"][-1] |
| 628 | + assert span["description"].startswith("GET") |
| 629 | + |
| 630 | + data = span.get("data", {}) |
| 631 | + |
| 632 | + assert SPANDATA.CODE_LINENO not in data |
| 633 | + assert SPANDATA.CODE_NAMESPACE not in data |
| 634 | + assert SPANDATA.CODE_FILEPATH not in data |
| 635 | + assert SPANDATA.CODE_FUNCTION not in data |
| 636 | + |
| 637 | + |
| 638 | +@pytest.mark.parametrize( |
| 639 | + "httpx_client", |
| 640 | + (httpx.Client(), httpx.AsyncClient()), |
| 641 | +) |
| 642 | +def test_request_source_if_duration_over_threshold( |
| 643 | + sentry_init, capture_events, httpx_client, httpx_mock |
| 644 | +): |
| 645 | + httpx_mock.add_response() |
| 646 | + |
| 647 | + sentry_init( |
| 648 | + integrations=[HttpxIntegration()], |
| 649 | + traces_sample_rate=1.0, |
| 650 | + enable_db_query_source=True, |
| 651 | + db_query_source_threshold_ms=100, |
| 652 | + ) |
| 653 | + |
| 654 | + events = capture_events() |
| 655 | + |
| 656 | + url = "http://example.com/" |
| 657 | + |
| 658 | + with start_transaction(name="test_transaction"): |
| 659 | + |
| 660 | + @contextmanager |
| 661 | + def fake_start_span(*args, **kwargs): |
| 662 | + with sentry_sdk.start_span(*args, **kwargs) as span: |
| 663 | + pass |
| 664 | + span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0) |
| 665 | + span.timestamp = datetime.datetime(2024, 1, 1, microsecond=100001) |
| 666 | + yield span |
| 667 | + |
| 668 | + with mock.patch( |
| 669 | + "sentry_sdk.integrations.httpx.start_span", |
| 670 | + fake_start_span, |
| 671 | + ): |
| 672 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 673 | + asyncio.get_event_loop().run_until_complete(httpx_client.get(url)) |
| 674 | + else: |
| 675 | + httpx_client.get(url) |
| 676 | + |
| 677 | + (event,) = events |
| 678 | + |
| 679 | + span = event["spans"][-1] |
| 680 | + assert span["description"].startswith("GET") |
| 681 | + |
| 682 | + data = span.get("data", {}) |
| 683 | + |
| 684 | + assert SPANDATA.CODE_LINENO in data |
| 685 | + assert SPANDATA.CODE_NAMESPACE in data |
| 686 | + assert SPANDATA.CODE_FILEPATH in data |
| 687 | + assert SPANDATA.CODE_FUNCTION in data |
| 688 | + |
| 689 | + assert type(data.get(SPANDATA.CODE_LINENO)) == int |
| 690 | + assert data.get(SPANDATA.CODE_LINENO) > 0 |
| 691 | + assert data.get(SPANDATA.CODE_NAMESPACE) == "tests.integrations.httpx.test_httpx" |
| 692 | + assert data.get(SPANDATA.CODE_FILEPATH).endswith( |
| 693 | + "tests/integrations/httpx/test_httpx.py" |
| 694 | + ) |
| 695 | + |
| 696 | + is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep |
| 697 | + assert is_relative_path |
| 698 | + |
| 699 | + assert ( |
| 700 | + data.get(SPANDATA.CODE_FUNCTION) |
| 701 | + == "test_query_source_if_duration_over_threshold" |
| 702 | + ) |
| 703 | + |
| 704 | + |
396 | 705 | @pytest.mark.parametrize( |
397 | 706 | "httpx_client", |
398 | 707 | (httpx.Client(), httpx.AsyncClient()), |
|
0 commit comments