diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 573c49fb01..d95353c652 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -17,13 +17,15 @@ on: - master - sentry-sdk-2.0 pull_request: - # The branches below must be a subset of the branches above - branches: - - master - - sentry-sdk-2.0 schedule: - cron: '18 18 * * 3' +# Cancel in progress workflows on pull_requests. +# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + permissions: contents: read diff --git a/.github/workflows/enforce-license-compliance.yml b/.github/workflows/enforce-license-compliance.yml index 01e02ccb8b..ef79ed112b 100644 --- a/.github/workflows/enforce-license-compliance.yml +++ b/.github/workflows/enforce-license-compliance.yml @@ -8,10 +8,11 @@ on: - release/* - sentry-sdk-2.0 pull_request: - branches: - - master - - main - - sentry-sdk-2.0 + +# Cancel in progress workflows on pull_requests. +# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} jobs: enforce-license-compliance: diff --git a/.github/workflows/test-integrations-ai.yml b/.github/workflows/test-integrations-ai.yml index 723f9c8412..24ccc77a87 100644 --- a/.github/workflows/test-integrations-ai.yml +++ b/.github/workflows/test-integrations-ai.yml @@ -165,7 +165,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All AI tests passed + name: All pinned AI tests passed needs: test-ai-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-aws-lambda.yml b/.github/workflows/test-integrations-aws-lambda.yml index 38c838ab33..6f5ea794b8 100644 --- a/.github/workflows/test-integrations-aws-lambda.yml +++ b/.github/workflows/test-integrations-aws-lambda.yml @@ -112,7 +112,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All AWS Lambda tests passed + name: All pinned AWS Lambda tests passed needs: test-aws_lambda-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-cloud-computing.yml b/.github/workflows/test-integrations-cloud-computing.yml index a3b7fc57ab..1f6913ea4a 100644 --- a/.github/workflows/test-integrations-cloud-computing.yml +++ b/.github/workflows/test-integrations-cloud-computing.yml @@ -157,7 +157,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Cloud Computing tests passed + name: All pinned Cloud Computing tests passed needs: test-cloud_computing-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-common.yml b/.github/workflows/test-integrations-common.yml index 8116b1b67c..ecffdb6f3e 100644 --- a/.github/workflows/test-integrations-common.yml +++ b/.github/workflows/test-integrations-common.yml @@ -77,7 +77,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Common tests passed + name: All pinned Common tests passed needs: test-common-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-data-processing.yml b/.github/workflows/test-integrations-data-processing.yml index acabcd1748..49d18fc24c 100644 --- a/.github/workflows/test-integrations-data-processing.yml +++ b/.github/workflows/test-integrations-data-processing.yml @@ -193,7 +193,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Data Processing tests passed + name: All pinned Data Processing tests passed needs: test-data_processing-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-databases.yml b/.github/workflows/test-integrations-databases.yml index 741e8fc43e..49d3e923ee 100644 --- a/.github/workflows/test-integrations-databases.yml +++ b/.github/workflows/test-integrations-databases.yml @@ -211,7 +211,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Databases tests passed + name: All pinned Databases tests passed needs: test-databases-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-graphql.yml b/.github/workflows/test-integrations-graphql.yml index ba4091215e..2cefb5d191 100644 --- a/.github/workflows/test-integrations-graphql.yml +++ b/.github/workflows/test-integrations-graphql.yml @@ -157,7 +157,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All GraphQL tests passed + name: All pinned GraphQL tests passed needs: test-graphql-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-miscellaneous.yml b/.github/workflows/test-integrations-miscellaneous.yml index 064d083335..0b49a27219 100644 --- a/.github/workflows/test-integrations-miscellaneous.yml +++ b/.github/workflows/test-integrations-miscellaneous.yml @@ -165,7 +165,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Miscellaneous tests passed + name: All pinned Miscellaneous tests passed needs: test-miscellaneous-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-networking.yml b/.github/workflows/test-integrations-networking.yml index 192eb1b35b..c24edff174 100644 --- a/.github/workflows/test-integrations-networking.yml +++ b/.github/workflows/test-integrations-networking.yml @@ -157,7 +157,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Networking tests passed + name: All pinned Networking tests passed needs: test-networking-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-web-frameworks-1.yml b/.github/workflows/test-integrations-web-frameworks-1.yml index f2bcb336dd..a655710843 100644 --- a/.github/workflows/test-integrations-web-frameworks-1.yml +++ b/.github/workflows/test-integrations-web-frameworks-1.yml @@ -193,7 +193,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Web Frameworks 1 tests passed + name: All pinned Web Frameworks 1 tests passed needs: test-web_frameworks_1-pinned # Always run this, even if a dependent job failed if: always() diff --git a/.github/workflows/test-integrations-web-frameworks-2.yml b/.github/workflows/test-integrations-web-frameworks-2.yml index 8f6bd543df..d3f1001e2c 100644 --- a/.github/workflows/test-integrations-web-frameworks-2.yml +++ b/.github/workflows/test-integrations-web-frameworks-2.yml @@ -205,7 +205,7 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All Web Frameworks 2 tests passed + name: All pinned Web Frameworks 2 tests passed needs: test-web_frameworks_2-pinned # Always run this, even if a dependent job failed if: always() diff --git a/requirements-devenv.txt b/requirements-devenv.txt index 29d3f15ec9..c0fa5cf245 100644 --- a/requirements-devenv.txt +++ b/requirements-devenv.txt @@ -1,5 +1,5 @@ -r requirements-linting.txt -r requirements-testing.txt mockupdb # required by `pymongo` tests that are enabled by `pymongo` from linter requirements -pytest<7.0.0 # https://github.com/pytest-dev/pytest/issues/9621; see tox.ini +pytest pytest-asyncio diff --git a/scripts/split-tox-gh-actions/templates/check_required.jinja b/scripts/split-tox-gh-actions/templates/check_required.jinja index b9b0f54015..ddb47cddf1 100644 --- a/scripts/split-tox-gh-actions/templates/check_required.jinja +++ b/scripts/split-tox-gh-actions/templates/check_required.jinja @@ -1,5 +1,5 @@ check_required_tests: - name: All {{ group }} tests passed + name: All pinned {{ group }} tests passed {% if "pinned" in categories %} needs: test-{{ group | replace(" ", "_") | lower }}-pinned {% endif %} diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 52c64f6843..d9db8bd6b8 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -65,7 +65,12 @@ try: # Optional dependency of Starlette to parse form data. - import multipart # type: ignore + try: + # python-multipart 0.0.13 and later + import python_multipart as multipart # type: ignore + except ImportError: + # python-multipart 0.0.12 and earlier + import multipart # type: ignore except ImportError: multipart = None diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index 287c8cb272..d388c5bca6 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -127,11 +127,13 @@ def getresponse(self, *args, **kwargs): if span is None: return real_getresponse(self, *args, **kwargs) - rv = real_getresponse(self, *args, **kwargs) + try: + rv = real_getresponse(self, *args, **kwargs) - span.set_http_status(int(rv.status)) - span.set_data("reason", rv.reason) - span.finish() + span.set_http_status(int(rv.status)) + span.set_data("reason", rv.reason) + finally: + span.finish() return rv diff --git a/sentry_sdk/profiler/utils.py b/sentry_sdk/profiler/utils.py index e78ea54256..3554cddb5d 100644 --- a/sentry_sdk/profiler/utils.py +++ b/sentry_sdk/profiler/utils.py @@ -89,7 +89,7 @@ def get_frame_name(frame): and co_varnames[0] == "self" and "self" in frame.f_locals ): - for cls in frame.f_locals["self"].__class__.__mro__: + for cls in type(frame.f_locals["self"]).__mro__: if name in cls.__dict__: return "{}.{}".format(cls.__name__, name) except (AttributeError, ValueError): diff --git a/sentry_sdk/transport.py b/sentry_sdk/transport.py index 1b1842d03e..8798115898 100644 --- a/sentry_sdk/transport.py +++ b/sentry_sdk/transport.py @@ -720,6 +720,7 @@ def _request( try: import httpcore + import h2 # type: ignore # noqa: F401 except ImportError: # Sorry, no Http2Transport for you class Http2Transport(HttpTransport): diff --git a/tests/integrations/celery/test_celery.py b/tests/integrations/celery/test_celery.py index ffd3f0db62..e51341599f 100644 --- a/tests/integrations/celery/test_celery.py +++ b/tests/integrations/celery/test_celery.py @@ -831,3 +831,11 @@ def test_send_task_wrapped( assert span["description"] == "very_creative_task_name" assert span["op"] == "queue.submit.celery" assert span["trace_id"] == kwargs["headers"]["sentry-trace"].split("-")[0] + + +@pytest.mark.skip(reason="placeholder so that forked test does not come last") +def test_placeholder(): + """Forked tests must not come last in the module. + See https://github.com/pytest-dev/pytest-forked/issues/67#issuecomment-1964718720. + """ + pass diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index c8282412ea..0e3f700105 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -1,6 +1,8 @@ +import inspect import json import os import re +import sys import pytest from functools import partial from unittest.mock import patch @@ -12,6 +14,7 @@ from django.core.management import execute_from_command_line from django.db.utils import OperationalError, ProgrammingError, DataError from django.http.request import RawPostDataException +from django.utils.functional import SimpleLazyObject try: from django.urls import reverse @@ -29,6 +32,7 @@ ) from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name from sentry_sdk.integrations.executing import ExecutingIntegration +from sentry_sdk.profiler.utils import get_frame_name from sentry_sdk.tracing import Span from tests.conftest import unpack_werkzeug_response from tests.integrations.django.myapp.wsgi import application @@ -1295,3 +1299,47 @@ def test_ensures_no_spotlight_middleware_when_no_spotlight( added = frozenset(settings.MIDDLEWARE) ^ original_middleware assert "sentry_sdk.spotlight.SpotlightMiddleware" not in added + + +def test_get_frame_name_when_in_lazy_object(): + allowed_to_init = False + + class SimpleLazyObjectWrapper(SimpleLazyObject): + def unproxied_method(self): + """ + For testing purposes. We inject a method on the SimpleLazyObject + class so if python is executing this method, we should get + this class instead of the wrapped class and avoid evaluating + the wrapped object too early. + """ + return inspect.currentframe() + + class GetFrame: + def __init__(self): + assert allowed_to_init, "GetFrame not permitted to initialize yet" + + def proxied_method(self): + """ + For testing purposes. We add an proxied method on the instance + class so if python is executing this method, we should get + this class instead of the wrapper class. + """ + return inspect.currentframe() + + instance = SimpleLazyObjectWrapper(lambda: GetFrame()) + + assert get_frame_name(instance.unproxied_method()) == ( + "SimpleLazyObjectWrapper.unproxied_method" + if sys.version_info < (3, 11) + else "test_get_frame_name_when_in_lazy_object..SimpleLazyObjectWrapper.unproxied_method" + ) + + # Now that we're about to access an instance method on the wrapped class, + # we should permit initializing it + allowed_to_init = True + + assert get_frame_name(instance.proxied_method()) == ( + "GetFrame.proxied_method" + if sys.version_info < (3, 11) + else "test_get_frame_name_when_in_lazy_object..GetFrame.proxied_method" + ) diff --git a/tests/integrations/rq/test_rq.py b/tests/integrations/rq/test_rq.py index e445b588be..ffd6f458e1 100644 --- a/tests/integrations/rq/test_rq.py +++ b/tests/integrations/rq/test_rq.py @@ -254,6 +254,11 @@ def test_traces_sampler_gets_correct_values_in_sampling_context( @pytest.mark.skipif( parse_version(rq.__version__) < (1, 5), reason="At least rq-1.5 required" ) +@pytest.mark.skipif( + parse_version(rq.__version__) >= (2,), + reason="Test broke in RQ 2.0. Investigate and fix. " + "See https://github.com/getsentry/sentry-python/issues/3707.", +) def test_job_with_retries(sentry_init, capture_events): sentry_init(integrations=[RqIntegration()]) events = capture_events() diff --git a/tests/integrations/stdlib/test_httplib.py b/tests/integrations/stdlib/test_httplib.py index c327331608..200b282f53 100644 --- a/tests/integrations/stdlib/test_httplib.py +++ b/tests/integrations/stdlib/test_httplib.py @@ -1,5 +1,6 @@ import random from http.client import HTTPConnection, HTTPSConnection +from socket import SocketIO from urllib.request import urlopen from unittest import mock @@ -342,3 +343,35 @@ def test_span_origin(sentry_init, capture_events): assert event["spans"][0]["op"] == "http.client" assert event["spans"][0]["origin"] == "auto.http.stdlib.httplib" + + +def test_http_timeout(monkeypatch, sentry_init, capture_envelopes): + mock_readinto = mock.Mock(side_effect=TimeoutError) + monkeypatch.setattr(SocketIO, "readinto", mock_readinto) + + sentry_init(traces_sample_rate=1.0) + + envelopes = capture_envelopes() + + with start_transaction(op="op", name="name"): + try: + conn = HTTPSConnection("www.squirrelchasers.com") + conn.request("GET", "/top-chasers") + conn.getresponse() + except Exception: + pass + + items = [ + item + for envelope in envelopes + for item in envelope.items + if item.type == "transaction" + ] + assert len(items) == 1 + + transaction = items[0].payload.json + assert len(transaction["spans"]) == 1 + + span = transaction["spans"][0] + assert span["op"] == "http.client" + assert span["description"] == "GET https://www.squirrelchasers.com/top-chasers" diff --git a/tests/test_utils.py b/tests/test_utils.py index 87e2659a12..6e01bb4f3a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -69,8 +69,8 @@ def _normalize_distribution_name(name): ), # UTC time ( "2021-01-01T00:00:00.000000", - datetime(2021, 1, 1, tzinfo=timezone.utc), - ), # No TZ -- assume UTC + datetime(2021, 1, 1).astimezone(timezone.utc), + ), # No TZ -- assume local but convert to UTC ( "2021-01-01T00:00:00Z", datetime(2021, 1, 1, tzinfo=timezone.utc), diff --git a/tox.ini b/tox.ini index b53cc73d7f..ef30e539b5 100644 --- a/tox.ini +++ b/tox.ini @@ -294,8 +294,8 @@ deps = # See https://github.com/pytest-dev/pytest/issues/9621 # and https://github.com/pytest-dev/pytest-forked/issues/67 # for justification of the upper bound on pytest - {py3.6,py3.7,py3.8,py3.9,py3.10,py3.11,py3.12}-common: pytest<7.0.0 - py3.13-common: pytest + {py3.6,py3.7}-common: pytest<7.0.0 + {py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-common: pytest # === Gevent === {py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-gevent: gevent>=22.10.0, <22.11.0 @@ -303,7 +303,8 @@ deps = # See https://github.com/pytest-dev/pytest/issues/9621 # and https://github.com/pytest-dev/pytest-forked/issues/67 # for justification of the upper bound on pytest - {py3.6,py3.7,py3.8,py3.9,py3.10,py3.11,py3.12}-gevent: pytest<7.0.0 + {py3.6,py3.7}-gevent: pytest<7.0.0 + {py3.8,py3.9,py3.10,py3.11,py3.12}-gevent: pytest # === Integrations === @@ -374,7 +375,6 @@ deps = celery-latest: Celery celery: newrelic - celery: pytest<7 {py3.7}-celery: importlib-metadata<5.0 # Chalice @@ -583,7 +583,8 @@ deps = # Redis redis: fakeredis!=1.7.4 redis: pytest<8.0.0 - {py3.7,py3.8,py3.9,py3.10,py3.11}-redis: pytest-asyncio + {py3.6,py3.7}-redis: fakeredis!=2.26.0 # https://github.com/cunla/fakeredis-py/issues/341 + {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-redis: pytest-asyncio redis-v3: redis~=3.0 redis-v4: redis~=4.0 redis-v5: redis~=5.0 @@ -602,7 +603,9 @@ deps = rq-v{0.6}: redis<3.2.2 rq-v{0.13,1.0,1.5,1.10}: fakeredis>=1.0,<1.7.4 rq-v{1.15,1.16}: fakeredis + {py3.6,py3.7}-rq-v{1.15,1.16}: fakeredis!=2.26.0 # https://github.com/cunla/fakeredis-py/issues/341 rq-latest: fakeredis + {py3.6,py3.7}-rq-latest: fakeredis!=2.26.0 # https://github.com/cunla/fakeredis-py/issues/341 rq-v0.6: rq~=0.6.0 rq-v0.13: rq~=0.13.0 rq-v1.0: rq~=1.0.0 @@ -670,7 +673,9 @@ deps = strawberry-latest: strawberry-graphql[fastapi,flask] # Tornado - tornado: pytest<8.2 + # Tornado <6.4.1 is incompatible with Pytest ≥8.2 + # See https://github.com/tornadoweb/tornado/pull/3382. + tornado-{v6.0,v6.2}: pytest<8.2 tornado-v6.0: tornado~=6.0.0 tornado-v6.2: tornado~=6.2.0 tornado-latest: tornado