From a545ec090bfb2f95ca2508f65a01163f77c40130 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 6 Dec 2024 15:53:46 +0100 Subject: [PATCH 01/19] Properly sort breadcrumbs (#3864) --- sentry_sdk/scope.py | 3 ++- sentry_sdk/utils.py | 25 ++++++++++++++++++++++ tests/test_basics.py | 6 +++--- tests/test_utils.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index a643cfa9c5..28fe89febe 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -33,6 +33,7 @@ capture_internal_exception, capture_internal_exceptions, ContextVar, + datetime_from_isoformat, disable_capture_event, event_from_exception, exc_info_from_error, @@ -1264,7 +1265,7 @@ def _apply_breadcrumbs_to_event(self, event, hint, options): try: for crumb in event["breadcrumbs"]["values"]: if isinstance(crumb["timestamp"], str): - crumb["timestamp"] = datetime.fromisoformat(crumb["timestamp"]) + crumb["timestamp"] = datetime_from_isoformat(crumb["timestamp"]) event["breadcrumbs"]["values"].sort(key=lambda crumb: crumb["timestamp"]) except Exception as err: diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 8b45f555ae..16dfd9c4fa 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1929,3 +1929,28 @@ def _serialize_span_attribute(value): return str(value) except Exception: return None + + +ISO_TZ_SEPARATORS = frozenset(("+", "-")) + + +def datetime_from_isoformat(value): + # type: (str) -> datetime + try: + result = datetime.fromisoformat(value) + except (AttributeError, ValueError): + # py 3.6 + timestamp_format = ( + "%Y-%m-%dT%H:%M:%S.%f" if "." in value else "%Y-%m-%dT%H:%M:%S" + ) + if value.endswith("Z"): + value = value[:-1] + "+0000" + + if value[-6] in ISO_TZ_SEPARATORS: + timestamp_format += "%z" + value = value[:-3] + value[-2:] + elif value[-5] in ISO_TZ_SEPARATORS: + timestamp_format += "%z" + + result = datetime.strptime(value, timestamp_format) + return result.astimezone(timezone.utc) diff --git a/tests/test_basics.py b/tests/test_basics.py index 749d31d7d3..3c05f9848a 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -32,7 +32,7 @@ from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.scope import add_global_event_processor -from sentry_sdk.utils import get_sdk_name, reraise +from sentry_sdk.utils import datetime_from_isoformat, get_sdk_name, reraise from sentry_sdk.tracing_utils import has_tracing_enabled @@ -348,7 +348,7 @@ def test_breadcrumb_ordering(sentry_init, capture_events): assert len(event["breadcrumbs"]["values"]) == len(timestamps) timestamps_from_event = [ - datetime.fromisoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"] + datetime_from_isoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"] ] assert timestamps_from_event == sorted(timestamps) @@ -389,7 +389,7 @@ def test_breadcrumb_ordering_different_types(sentry_init, capture_events): assert len(event["breadcrumbs"]["values"]) == len(timestamps) timestamps_from_event = [ - datetime.fromisoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"] + datetime_from_isoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"] ] assert timestamps_from_event == sorted(timestamps) diff --git a/tests/test_utils.py b/tests/test_utils.py index 5011662f05..2eab252573 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -12,6 +12,7 @@ from sentry_sdk.utils import ( Components, Dsn, + datetime_from_isoformat, env_to_bool, format_timestamp, get_current_thread_meta, @@ -933,3 +934,52 @@ def __str__(self): ) def test_serialize_span_attribute(value, result): assert _serialize_span_attribute(value) == result + + +@pytest.mark.parametrize( + ("input_str", "expected_output"), + ( + ( + "2021-01-01T00:00:00.000000Z", + datetime(2021, 1, 1, tzinfo=timezone.utc), + ), # UTC time + ( + "2021-01-01T00:00:00.000000", + datetime(2021, 1, 1, tzinfo=datetime.now().astimezone().tzinfo), + ), # No TZ -- assume UTC + ( + "2021-01-01T00:00:00Z", + datetime(2021, 1, 1, tzinfo=timezone.utc), + ), # UTC - No milliseconds + ( + "2021-01-01T00:00:00.000000+00:00", + datetime(2021, 1, 1, tzinfo=timezone.utc), + ), + ( + "2021-01-01T00:00:00.000000-00:00", + datetime(2021, 1, 1, tzinfo=timezone.utc), + ), + ( + "2021-01-01T00:00:00.000000+0000", + datetime(2021, 1, 1, tzinfo=timezone.utc), + ), + ( + "2021-01-01T00:00:00.000000-0000", + datetime(2021, 1, 1, tzinfo=timezone.utc), + ), + ( + "2020-12-31T00:00:00.000000+02:00", + datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=2))), + ), # UTC+2 time + ( + "2020-12-31T00:00:00.000000-0200", + datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))), + ), # UTC-2 time + ( + "2020-12-31T00:00:00-0200", + datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))), + ), # UTC-2 time - no milliseconds + ), +) +def test_datetime_from_isoformat(input_str, expected_output): + assert datetime_from_isoformat(input_str) == expected_output, input_str \ No newline at end of file From 6448c709b840f37ca40b297fd64a99467f05d39b Mon Sep 17 00:00:00 2001 From: Jeffrey Hung <17494876+Jeffreyhung@users.noreply.github.com> Date: Wed, 11 Dec 2024 04:05:57 -0800 Subject: [PATCH 02/19] Replace release bot with GH app (#3868) --- .github/workflows/release.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 268f62c4cc..2cd3dfb2ac 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,14 +18,20 @@ jobs: runs-on: ubuntu-latest name: "Release a new version" steps: + - name: Get auth token + id: token + uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1.11.0 + with: + app-id: ${{ vars.SENTRY_RELEASE_BOT_CLIENT_ID }} + private-key: ${{ secrets.SENTRY_RELEASE_BOT_PRIVATE_KEY }} - uses: actions/checkout@v4.2.2 with: - token: ${{ secrets.GH_RELEASE_PAT }} + token: ${{ steps.token.outputs.token }} fetch-depth: 0 - name: Prepare release uses: getsentry/action-prepare-release@v1 env: - GITHUB_TOKEN: ${{ secrets.GH_RELEASE_PAT }} + GITHUB_TOKEN: ${{ steps.token.outputs.token }} with: version: ${{ github.event.inputs.version }} force: ${{ github.event.inputs.force }} From 1239499b5d6274f997a890650a516f6c5538a188 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Fri, 13 Dec 2024 11:26:43 +0000 Subject: [PATCH 03/19] fix(spotlight): Make Django middleware init even more defensive (#3870) I just got faced with a situation where even trying to do `settings.DEBUG` may trigger a Django exception if the settings are not loaded yet, hence widening the `capture_internal_exceptions()` scope for this. --- sentry_sdk/spotlight.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/spotlight.py b/sentry_sdk/spotlight.py index a94c691723..1555afb829 100644 --- a/sentry_sdk/spotlight.py +++ b/sentry_sdk/spotlight.py @@ -210,13 +210,13 @@ def setup_spotlight(options): if not isinstance(url, str): return None - if ( - settings is not None - and settings.DEBUG - and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_ON_ERROR", "1")) - and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_MIDDLEWARE", "1")) - ): - with capture_internal_exceptions(): + with capture_internal_exceptions(): + if ( + settings is not None + and settings.DEBUG + and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_ON_ERROR", "1")) + and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_MIDDLEWARE", "1")) + ): middleware = settings.MIDDLEWARE if DJANGO_SPOTLIGHT_MIDDLEWARE_PATH not in middleware: settings.MIDDLEWARE = type(middleware)( From 81b806321fed9715d0c7ff227bdf22c9f1178ce9 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Sat, 14 Dec 2024 00:55:25 +0000 Subject: [PATCH 04/19] fix(spotlight): Use the spotlight_url passed into the SDK when loading Spotlight (#3871) When we inject spotlight, we don't set the correct sidecar URL. This is an issue when a user defines a custom sidecar URL where we are able to load Spotlight UI from the correct URL but don't tell it the correct sidecar URL, making it non-functional. --------- Co-authored-by: Ivana Kellyer --- sentry_sdk/spotlight.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/spotlight.py b/sentry_sdk/spotlight.py index 1555afb829..a783b155a1 100644 --- a/sentry_sdk/spotlight.py +++ b/sentry_sdk/spotlight.py @@ -66,7 +66,8 @@ def capture_envelope(self, envelope): SPOTLIGHT_JS_ENTRY_PATH = "/assets/main.js" SPOTLIGHT_JS_SNIPPET_PATTERN = ( - '' + "\n" + '\n' ) SPOTLIGHT_ERROR_PAGE_SNIPPET = ( '\n' @@ -113,7 +114,8 @@ def spotlight_script(self): ) urllib.request.urlopen(req) self._spotlight_script = SPOTLIGHT_JS_SNIPPET_PATTERN.format( - spotlight_js_url + spotlight_url=self._spotlight_url, + spotlight_js_url=spotlight_js_url, ) except urllib.error.URLError as err: sentry_logger.debug( From 9d20ffdeb024cebe74f59d230ff4ec7d4101e1f7 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Mon, 16 Dec 2024 17:51:11 +0530 Subject: [PATCH 05/19] Fix grpc aio method handling (#3873) --- sentry_sdk/integrations/grpc/aio/client.py | 8 ++- sentry_sdk/integrations/rust_tracing.py | 2 +- tests/integrations/grpc/test_grpc.py | 67 +++++++++++----------- tests/integrations/grpc/test_grpc_aio.py | 48 ++++++++-------- tests/test_utils.py | 2 +- 5 files changed, 63 insertions(+), 64 deletions(-) diff --git a/sentry_sdk/integrations/grpc/aio/client.py b/sentry_sdk/integrations/grpc/aio/client.py index 1a7086c55d..2fd9f70bed 100644 --- a/sentry_sdk/integrations/grpc/aio/client.py +++ b/sentry_sdk/integrations/grpc/aio/client.py @@ -44,10 +44,12 @@ async def intercept_unary_unary( request: Message, ) -> Union[UnaryUnaryCall, Message]: method = client_call_details.method + if isinstance(method, bytes): + method = method.decode() with sentry_sdk.start_span( op=OP.GRPC_CLIENT, - name="unary unary call to %s" % method.decode(), + name="unary unary call to %s" % method, origin=SPAN_ORIGIN, only_if_parent=True, ) as span: @@ -75,10 +77,12 @@ async def intercept_unary_stream( request: Message, ) -> Union[AsyncIterable[Any], UnaryStreamCall]: method = client_call_details.method + if isinstance(method, bytes): + method = method.decode() with sentry_sdk.start_span( op=OP.GRPC_CLIENT, - name="unary stream call to %s" % method.decode(), + name="unary stream call to %s" % method, origin=SPAN_ORIGIN, only_if_parent=True, ) as span: diff --git a/sentry_sdk/integrations/rust_tracing.py b/sentry_sdk/integrations/rust_tracing.py index d394ba5712..68b807064a 100644 --- a/sentry_sdk/integrations/rust_tracing.py +++ b/sentry_sdk/integrations/rust_tracing.py @@ -32,7 +32,7 @@ import json from enum import Enum, auto -from typing import Any, Callable, Dict, Tuple, Optional +from typing import Any, Callable, Dict, Optional import sentry_sdk from sentry_sdk.integrations import Integration diff --git a/tests/integrations/grpc/test_grpc.py b/tests/integrations/grpc/test_grpc.py index a8872ef0b5..51eaef7339 100644 --- a/tests/integrations/grpc/test_grpc.py +++ b/tests/integrations/grpc/test_grpc.py @@ -7,7 +7,7 @@ from typing import List, Optional from unittest.mock import Mock -from sentry_sdk import start_span, start_transaction +from sentry_sdk import start_span from sentry_sdk.consts import OP from sentry_sdk.integrations.grpc import GRPCIntegration from tests.conftest import ApproxDict @@ -41,7 +41,7 @@ def _tear_down(server: grpc.Server): @pytest.mark.forked -def test_grpc_server_starts_transaction(sentry_init, capture_events_forksafe): +def test_grpc_server_starts_root_span(sentry_init, capture_events_forksafe): sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()]) events = capture_events_forksafe() @@ -99,7 +99,7 @@ def test_grpc_server_other_interceptors(sentry_init, capture_events_forksafe): @pytest.mark.forked -def test_grpc_server_continues_transaction(sentry_init, capture_events_forksafe): +def test_grpc_server_continues_trace(sentry_init, capture_events_forksafe): sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()]) events = capture_events_forksafe() @@ -108,20 +108,20 @@ def test_grpc_server_continues_transaction(sentry_init, capture_events_forksafe) with grpc.insecure_channel("localhost:{}".format(PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction() as transaction: + with start_span() as root_span: metadata = ( ( "baggage", "sentry-trace_id={trace_id},sentry-environment=test," "sentry-transaction=test-transaction,sentry-sample_rate=1.0".format( - trace_id=transaction.trace_id + trace_id=root_span.trace_id ), ), ( "sentry-trace", "{trace_id}-{parent_span_id}-{sampled}".format( - trace_id=transaction.trace_id, - parent_span_id=transaction.span_id, + trace_id=root_span.trace_id, + parent_span_id=root_span.span_id, sampled=1, ), ), @@ -139,7 +139,7 @@ def test_grpc_server_continues_transaction(sentry_init, capture_events_forksafe) "source": "custom", } assert event["contexts"]["trace"]["op"] == OP.GRPC_SERVER - assert event["contexts"]["trace"]["trace_id"] == transaction.trace_id + assert event["contexts"]["trace"]["trace_id"] == root_span.trace_id assert span["op"] == "test" @@ -153,17 +153,17 @@ def test_grpc_client_starts_span(sentry_init, capture_events_forksafe): with grpc.insecure_channel("localhost:{}".format(PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction(): + with start_span(): stub.TestServe(gRPCTestMessage(text="test")) _tear_down(server=server) events.write_file.close() events.read_event() - local_transaction = events.read_event() - span = local_transaction["spans"][0] + local_root_span = events.read_event() + span = local_root_span["spans"][0] - assert len(local_transaction["spans"]) == 1 + assert len(local_root_span["spans"]) == 1 assert span["op"] == OP.GRPC_CLIENT assert ( span["description"] @@ -188,16 +188,16 @@ def test_grpc_client_unary_stream_starts_span(sentry_init, capture_events_forksa with grpc.insecure_channel("localhost:{}".format(PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction(): + with start_span(): [el for el in stub.TestUnaryStream(gRPCTestMessage(text="test"))] _tear_down(server=server) events.write_file.close() - local_transaction = events.read_event() - span = local_transaction["spans"][0] + local_root_span = events.read_event() + span = local_root_span["spans"][0] - assert len(local_transaction["spans"]) == 1 + assert len(local_root_span["spans"]) == 1 assert span["op"] == OP.GRPC_CLIENT assert ( span["description"] @@ -233,7 +233,7 @@ def test_grpc_client_other_interceptor(sentry_init, capture_events_forksafe): channel = grpc.intercept_channel(channel, MockClientInterceptor()) stub = gRPCTestServiceStub(channel) - with start_transaction(): + with start_span(): stub.TestServe(gRPCTestMessage(text="test")) _tear_down(server=server) @@ -242,10 +242,10 @@ def test_grpc_client_other_interceptor(sentry_init, capture_events_forksafe): events.write_file.close() events.read_event() - local_transaction = events.read_event() - span = local_transaction["spans"][0] + local_root_span = events.read_event() + span = local_root_span["spans"][0] - assert len(local_transaction["spans"]) == 1 + assert len(local_root_span["spans"]) == 1 assert span["op"] == OP.GRPC_CLIENT assert ( span["description"] @@ -272,18 +272,18 @@ def test_grpc_client_and_servers_interceptors_integration( with grpc.insecure_channel("localhost:{}".format(PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction(): + with start_span(): stub.TestServe(gRPCTestMessage(text="test")) _tear_down(server=server) events.write_file.close() - server_transaction = events.read_event() - local_transaction = events.read_event() + server_root_span = events.read_event() + local_root_span = events.read_event() assert ( - server_transaction["contexts"]["trace"]["trace_id"] - == local_transaction["contexts"]["trace"]["trace_id"] + server_root_span["contexts"]["trace"]["trace_id"] + == local_root_span["contexts"]["trace"]["trace_id"] ) @@ -328,26 +328,23 @@ def test_span_origin(sentry_init, capture_events_forksafe): with grpc.insecure_channel("localhost:{}".format(PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction(name="custom_transaction"): + with start_span(name="custom_root"): stub.TestServe(gRPCTestMessage(text="test")) _tear_down(server=server) events.write_file.close() - transaction_from_integration = events.read_event() - custom_transaction = events.read_event() + root_span_from_integration = events.read_event() + custom_root_span = events.read_event() + assert root_span_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc" assert ( - transaction_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc" - ) - assert ( - transaction_from_integration["spans"][0]["origin"] - == "auto.grpc.grpc.TestService" + root_span_from_integration["spans"][0]["origin"] == "auto.grpc.grpc.TestService" ) # manually created in TestService, not the instrumentation - assert custom_transaction["contexts"]["trace"]["origin"] == "manual" - assert custom_transaction["spans"][0]["origin"] == "auto.grpc.grpc" + assert custom_root_span["contexts"]["trace"]["origin"] == "manual" + assert custom_root_span["spans"][0]["origin"] == "auto.grpc.grpc" class TestService(gRPCTestServiceServicer): diff --git a/tests/integrations/grpc/test_grpc_aio.py b/tests/integrations/grpc/test_grpc_aio.py index 9ce9aef6a5..0d30c59681 100644 --- a/tests/integrations/grpc/test_grpc_aio.py +++ b/tests/integrations/grpc/test_grpc_aio.py @@ -6,7 +6,7 @@ import pytest_asyncio import sentry_sdk -from sentry_sdk import start_span, start_transaction +from sentry_sdk import start_span from sentry_sdk.consts import OP from sentry_sdk.integrations.grpc import GRPCIntegration from tests.conftest import ApproxDict @@ -60,7 +60,7 @@ async def test_noop_for_unimplemented_method(sentry_init, capture_events): @pytest.mark.asyncio -async def test_grpc_server_starts_transaction(grpc_server, capture_events): +async def test_grpc_server_starts_root_span(grpc_server, capture_events): events = capture_events() async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel: @@ -79,26 +79,26 @@ async def test_grpc_server_starts_transaction(grpc_server, capture_events): @pytest.mark.asyncio -async def test_grpc_server_continues_transaction(grpc_server, capture_events): +async def test_grpc_server_continues_trace(grpc_server, capture_events): events = capture_events() async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel: stub = gRPCTestServiceStub(channel) - with sentry_sdk.start_transaction() as transaction: + with sentry_sdk.start_span() as root_span: metadata = ( ( "baggage", "sentry-trace_id={trace_id},sentry-environment=test," "sentry-transaction=test-transaction,sentry-sample_rate=1.0".format( - trace_id=transaction.trace_id + trace_id=root_span.trace_id ), ), ( "sentry-trace", "{trace_id}-{parent_span_id}-{sampled}".format( - trace_id=transaction.trace_id, - parent_span_id=transaction.span_id, + trace_id=root_span.trace_id, + parent_span_id=root_span.span_id, sampled=1, ), ), @@ -114,7 +114,7 @@ async def test_grpc_server_continues_transaction(grpc_server, capture_events): "source": "custom", } assert event["contexts"]["trace"]["op"] == OP.GRPC_SERVER - assert event["contexts"]["trace"]["trace_id"] == transaction.trace_id + assert event["contexts"]["trace"]["trace_id"] == root_span.trace_id assert span["op"] == "test" @@ -159,15 +159,15 @@ async def test_grpc_client_starts_span(grpc_server, capture_events_forksafe): async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction(): + with start_span(): await stub.TestServe(gRPCTestMessage(text="test")) events.write_file.close() events.read_event() - local_transaction = events.read_event() - span = local_transaction["spans"][0] + local_root_span = events.read_event() + span = local_root_span["spans"][0] - assert len(local_transaction["spans"]) == 1 + assert len(local_root_span["spans"]) == 1 assert span["op"] == OP.GRPC_CLIENT assert ( span["description"] @@ -190,15 +190,15 @@ async def test_grpc_client_unary_stream_starts_span( async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction(): + with start_span(): response = stub.TestUnaryStream(gRPCTestMessage(text="test")) [_ async for _ in response] events.write_file.close() - local_transaction = events.read_event() - span = local_transaction["spans"][0] + local_root_span = events.read_event() + span = local_root_span["spans"][0] - assert len(local_transaction["spans"]) == 1 + assert len(local_root_span["spans"]) == 1 assert span["op"] == OP.GRPC_CLIENT assert ( span["description"] @@ -243,24 +243,22 @@ async def test_span_origin(grpc_server, capture_events_forksafe): async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel: stub = gRPCTestServiceStub(channel) - with start_transaction(name="custom_transaction"): + with start_span(name="custom_root"): await stub.TestServe(gRPCTestMessage(text="test")) events.write_file.close() - transaction_from_integration = events.read_event() - custom_transaction = events.read_event() + root_span_from_integration = events.read_event() + custom_root_span = events.read_event() + assert root_span_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc" assert ( - transaction_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc" - ) - assert ( - transaction_from_integration["spans"][0]["origin"] + root_span_from_integration["spans"][0]["origin"] == "auto.grpc.grpc.TestService.aio" ) # manually created in TestService, not the instrumentation - assert custom_transaction["contexts"]["trace"]["origin"] == "manual" - assert custom_transaction["spans"][0]["origin"] == "auto.grpc.grpc" + assert custom_root_span["contexts"]["trace"]["origin"] == "manual" + assert custom_root_span["spans"][0]["origin"] == "auto.grpc.grpc" class TestService(gRPCTestServiceServicer): diff --git a/tests/test_utils.py b/tests/test_utils.py index 2eab252573..8613079ebd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -982,4 +982,4 @@ def test_serialize_span_attribute(value, result): ), ) def test_datetime_from_isoformat(input_str, expected_output): - assert datetime_from_isoformat(input_str) == expected_output, input_str \ No newline at end of file + assert datetime_from_isoformat(input_str) == expected_output, input_str From 1984a5ced9cc41d332a2dfca425fdf7354d3d9e8 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 17 Dec 2024 13:48:23 +0530 Subject: [PATCH 06/19] Fix graphene integration --- sentry_sdk/integrations/graphene.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/sentry_sdk/integrations/graphene.py b/sentry_sdk/integrations/graphene.py index 828bb0ade5..42300e978f 100644 --- a/sentry_sdk/integrations/graphene.py +++ b/sentry_sdk/integrations/graphene.py @@ -140,19 +140,10 @@ def graphql_span(schema, source, kwargs): }, ) - scope = sentry_sdk.get_current_scope() - if scope.span: - _graphql_span = scope.span.start_child(op=op, name=operation_name) - else: - _graphql_span = sentry_sdk.start_span( - op=op, name=operation_name, only_if_parent=True - ) - - _graphql_span.set_data("graphql.document", source) - _graphql_span.set_data("graphql.operation.name", operation_name) - _graphql_span.set_data("graphql.operation.type", operation_type) - - try: + with sentry_sdk.start_span( + op=op, name=operation_name, only_if_parent=True + ) as graphql_span: + graphql_span.set_data("graphql.document", source) + graphql_span.set_data("graphql.operation.name", operation_name) + graphql_span.set_data("graphql.operation.type", operation_type) yield - finally: - _graphql_span.finish() From 2666022f490dfe3f94db80059535818b37e76839 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 17 Dec 2024 15:33:04 +0100 Subject: [PATCH 07/19] Fix CI (#3878) --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index d3bd83cb03..9ccc4dc0eb 100644 --- a/tox.ini +++ b/tox.ini @@ -603,6 +603,7 @@ deps = quart-v0.16: quart~=0.16.0 quart-v0.19: Werkzeug>=3.0.0 quart-v0.19: quart~=0.19.0 + {py3.8}-quart: taskgroup==0.0.0a4 quart-latest: quart # Ray From 92f53910d54eb6f78632b689980a6a29474a9e6a Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 18 Dec 2024 13:46:02 +0530 Subject: [PATCH 08/19] Fix strawberry integration (#3874) --- sentry_sdk/integrations/strawberry.py | 69 ++++++++----------- .../strawberry/test_strawberry.py | 4 +- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index bf174e9d99..dc96676538 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -179,59 +179,50 @@ def on_operation(self): }, ) - span = sentry_sdk.get_current_span() - if span: - self.graphql_span = span.start_child( - op=op, - name=description, - origin=StrawberryIntegration.origin, - ) - else: - self.graphql_span = sentry_sdk.start_span( - op=op, - name=description, - origin=StrawberryIntegration.origin, - only_if_parent=True, - ) + with sentry_sdk.start_span( + op=op, + name=description, + origin=StrawberryIntegration.origin, + only_if_parent=True, + ) as graphql_span: + graphql_span.set_data("graphql.operation.type", operation_type) + graphql_span.set_data("graphql.document", self.execution_context.query) + graphql_span.set_data("graphql.resource_name", self._resource_name) + + yield - self.graphql_span.set_data("graphql.operation.type", operation_type) - self.graphql_span.set_data("graphql.operation.name", self._operation_name) - self.graphql_span.set_data("graphql.document", self.execution_context.query) - self.graphql_span.set_data("graphql.resource_name", self._resource_name) + # we might have a more accurate operation_name after the parsing + self._operation_name = self.execution_context.operation_name - yield + if self._operation_name is not None: + graphql_span.set_data("graphql.operation.name", self._operation_name) - transaction = self.graphql_span.containing_transaction - if transaction and self.execution_context.operation_name: - transaction.name = self.execution_context.operation_name - transaction.source = TRANSACTION_SOURCE_COMPONENT - transaction.op = op + sentry_sdk.get_current_scope().set_transaction_name( + self._operation_name, + source=TRANSACTION_SOURCE_COMPONENT, + ) - self.graphql_span.finish() + root_span = graphql_span.root_span + if root_span: + root_span.op = op def on_validate(self): # type: () -> Generator[None, None, None] - self.validation_span = self.graphql_span.start_child( + with sentry_sdk.start_span( op=OP.GRAPHQL_VALIDATE, name="validation", origin=StrawberryIntegration.origin, - ) - - yield - - self.validation_span.finish() + ): + yield def on_parse(self): # type: () -> Generator[None, None, None] - self.parsing_span = self.graphql_span.start_child( + with sentry_sdk.start_span( op=OP.GRAPHQL_PARSE, name="parsing", origin=StrawberryIntegration.origin, - ) - - yield - - self.parsing_span.finish() + ): + yield def should_skip_tracing(self, _next, info): # type: (Callable[[Any, GraphQLResolveInfo, Any, Any], Any], GraphQLResolveInfo) -> bool @@ -253,7 +244,7 @@ async def resolve(self, _next, root, info, *args, **kwargs): field_path = "{}.{}".format(info.parent_type, info.field_name) - with self.graphql_span.start_child( + with sentry_sdk.start_span( op=OP.GRAPHQL_RESOLVE, name="resolving {}".format(field_path), origin=StrawberryIntegration.origin, @@ -274,7 +265,7 @@ def resolve(self, _next, root, info, *args, **kwargs): field_path = "{}.{}".format(info.parent_type, info.field_name) - with self.graphql_span.start_child( + with sentry_sdk.start_span( op=OP.GRAPHQL_RESOLVE, name="resolving {}".format(field_path), origin=StrawberryIntegration.origin, diff --git a/tests/integrations/strawberry/test_strawberry.py b/tests/integrations/strawberry/test_strawberry.py index 7b40b238d2..fdf7ff71bb 100644 --- a/tests/integrations/strawberry/test_strawberry.py +++ b/tests/integrations/strawberry/test_strawberry.py @@ -505,7 +505,7 @@ def test_transaction_no_operation_name( query_span = query_spans[0] assert query_span["description"] == "query" assert query_span["data"]["graphql.operation.type"] == "query" - assert query_span["data"]["graphql.operation.name"] is None + assert "graphql.operation.name" not in query_span["data"] assert query_span["data"]["graphql.document"] == query assert query_span["data"]["graphql.resource_name"] @@ -582,7 +582,7 @@ def test_transaction_mutation( query_span = query_spans[0] assert query_span["description"] == "mutation" assert query_span["data"]["graphql.operation.type"] == "mutation" - assert query_span["data"]["graphql.operation.name"] is None + assert query_span["data"]["graphql.operation.name"] == "Change" assert query_span["data"]["graphql.document"] == query assert query_span["data"]["graphql.resource_name"] From 60d6333efaf1332e653ef653195907fb2d211a43 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 18 Dec 2024 09:30:17 +0100 Subject: [PATCH 09/19] Fix arq tests in POTel (#3875) Make sure OK status is set, only when there has not been a error status set before. --- sentry_sdk/integrations/arq.py | 7 +++- sentry_sdk/tracing.py | 19 +++++++++++ .../integrations/opentelemetry/test_potel.py | 33 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/arq.py b/sentry_sdk/integrations/arq.py index 5aa0ba7302..c26db4520c 100644 --- a/sentry_sdk/integrations/arq.py +++ b/sentry_sdk/integrations/arq.py @@ -1,5 +1,7 @@ import sys +from opentelemetry.trace.status import StatusCode + import sentry_sdk from sentry_sdk.consts import OP, SPANSTATUS from sentry_sdk.integrations import DidNotEnable, Integration @@ -116,7 +118,10 @@ async def _sentry_run_job(self, job_id, score): origin=ArqIntegration.origin, ) as span: return_value = await old_run_job(self, job_id, score) - span.set_status(SPANSTATUS.OK) + + if span.status is None: + span.set_status(SPANSTATUS.OK) + return return_value Worker.run_job = _sentry_run_job diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 7686dcf052..a0b9439dc8 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1583,6 +1583,25 @@ def set_attribute(self, key, value): self._otel_span.set_attribute(key, _serialize_span_attribute(value)) + @property + def status(self): + # type: () -> Optional[str] + """ + Return the Sentry `SPANSTATUS` corresponding to the underlying OTel status. + Because differences in possible values in OTel `StatusCode` and + Sentry `SPANSTATUS` it can not be guaranteed that the status + set in `set_status()` will be the same as the one returned here. + """ + if not hasattr(self._otel_span, "status"): + return None + + if self._otel_span.status.status_code == StatusCode.UNSET: + return None + elif self._otel_span.status.status_code == StatusCode.OK: + return SPANSTATUS.OK + else: + return SPANSTATUS.UNKNOWN_ERROR + def set_status(self, status): # type: (str) -> None if status == SPANSTATUS.OK: diff --git a/tests/integrations/opentelemetry/test_potel.py b/tests/integrations/opentelemetry/test_potel.py index 39c48f8cc8..2d1d66c6d0 100644 --- a/tests/integrations/opentelemetry/test_potel.py +++ b/tests/integrations/opentelemetry/test_potel.py @@ -2,6 +2,7 @@ from opentelemetry import trace import sentry_sdk +from sentry_sdk.consts import SPANSTATUS from tests.conftest import ApproxDict @@ -331,3 +332,35 @@ def test_potel_span_root_span_references(): with sentry_sdk.start_span(description="http") as http_span: assert not http_span.is_root_span assert http_span.root_span == request_span + + +@pytest.mark.parametrize( + "status_in,status_out", + [ + (None, None), + ("", SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.OK, SPANSTATUS.OK), + (SPANSTATUS.ABORTED, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.ALREADY_EXISTS, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.CANCELLED, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.DATA_LOSS, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.DEADLINE_EXCEEDED, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.FAILED_PRECONDITION, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.INTERNAL_ERROR, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.INVALID_ARGUMENT, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.NOT_FOUND, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.OUT_OF_RANGE, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.PERMISSION_DENIED, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.RESOURCE_EXHAUSTED, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.UNAUTHENTICATED, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.UNAVAILABLE, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.UNIMPLEMENTED, SPANSTATUS.UNKNOWN_ERROR), + (SPANSTATUS.UNKNOWN_ERROR, SPANSTATUS.UNKNOWN_ERROR), + ], +) +def test_potel_span_status(status_in, status_out): + span = sentry_sdk.start_span(name="test") + if status_in is not None: + span.set_status(status_in) + + assert span.status == status_out From f6e91621ae772fb28b1b29df4df47236f3959c2d Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 18 Dec 2024 10:11:09 +0100 Subject: [PATCH 10/19] Fix ray tests (#3877) Make sure there is a transaction name --- sentry_sdk/integrations/ray.py | 17 +++++++--- tests/integrations/ray/test_ray.py | 50 ++++++++++++++++-------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/sentry_sdk/integrations/ray.py b/sentry_sdk/integrations/ray.py index 0290bdf1ef..0503d27bdb 100644 --- a/sentry_sdk/integrations/ray.py +++ b/sentry_sdk/integrations/ray.py @@ -26,6 +26,8 @@ from typing import Any, Optional from sentry_sdk.utils import ExcInfo +DEFAULT_TRANSACTION_NAME = "unknown Ray function" + def _check_sentry_initialized(): # type: () -> None @@ -58,18 +60,23 @@ def _f(*f_args, _tracing=None, **f_kwargs): """ _check_sentry_initialized() + root_span_name = qualname_from_function(f) or DEFAULT_TRANSACTION_NAME + sentry_sdk.get_current_scope().set_transaction_name( + root_span_name, + source=TRANSACTION_SOURCE_TASK, + ) with sentry_sdk.continue_trace(_tracing or {}): - with sentry_sdk.start_transaction( + with sentry_sdk.start_span( op=OP.QUEUE_TASK_RAY, - name=qualname_from_function(f) or "unknown Ray function", + name=root_span_name, origin=RayIntegration.origin, source=TRANSACTION_SOURCE_TASK, - ) as transaction: + ) as root_span: try: result = f(*f_args, **f_kwargs) - transaction.set_status(SPANSTATUS.OK) + root_span.set_status(SPANSTATUS.OK) except Exception: - transaction.set_status(SPANSTATUS.INTERNAL_ERROR) + root_span.set_status(SPANSTATUS.INTERNAL_ERROR) exc_info = sys.exc_info() _capture_exception(exc_info) reraise(*exc_info) diff --git a/tests/integrations/ray/test_ray.py b/tests/integrations/ray/test_ray.py index 95ab4ad0fa..a8c752269a 100644 --- a/tests/integrations/ray/test_ray.py +++ b/tests/integrations/ray/test_ray.py @@ -77,42 +77,42 @@ def example_task(): return sentry_sdk.get_client().transport.envelopes - with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + with sentry_sdk.start_span(op="test", name="ray client root span"): worker_envelopes = ray.get(example_task.remote()) client_envelope = sentry_sdk.get_client().transport.envelopes[0] - client_transaction = client_envelope.get_transaction_event() - assert client_transaction["transaction"] == "ray test transaction" - assert client_transaction["transaction_info"] == {"source": "custom"} + client_root_span = client_envelope.get_transaction_event() + assert client_root_span["transaction"] == "ray client root span" + assert client_root_span["transaction_info"] == {"source": "custom"} worker_envelope = worker_envelopes[0] - worker_transaction = worker_envelope.get_transaction_event() + worker_root_span = worker_envelope.get_transaction_event() assert ( - worker_transaction["transaction"] + worker_root_span["transaction"] == "tests.integrations.ray.test_ray.test_tracing_in_ray_tasks..example_task" ) - assert worker_transaction["transaction_info"] == {"source": "task"} + assert worker_root_span["transaction_info"] == {"source": "task"} - (span,) = client_transaction["spans"] + (span,) = client_root_span["spans"] assert span["op"] == "queue.submit.ray" assert span["origin"] == "auto.queue.ray" assert ( span["description"] == "tests.integrations.ray.test_ray.test_tracing_in_ray_tasks..example_task" ) - assert span["parent_span_id"] == client_transaction["contexts"]["trace"]["span_id"] - assert span["trace_id"] == client_transaction["contexts"]["trace"]["trace_id"] + assert span["parent_span_id"] == client_root_span["contexts"]["trace"]["span_id"] + assert span["trace_id"] == client_root_span["contexts"]["trace"]["trace_id"] - (span,) = worker_transaction["spans"] + (span,) = worker_root_span["spans"] assert span["op"] == "task" assert span["origin"] == "manual" assert span["description"] == "example task step" - assert span["parent_span_id"] == worker_transaction["contexts"]["trace"]["span_id"] - assert span["trace_id"] == worker_transaction["contexts"]["trace"]["trace_id"] + assert span["parent_span_id"] == worker_root_span["contexts"]["trace"]["span_id"] + assert span["trace_id"] == worker_root_span["contexts"]["trace"]["trace_id"] assert ( - client_transaction["contexts"]["trace"]["trace_id"] - == worker_transaction["contexts"]["trace"]["trace_id"] + client_root_span["contexts"]["trace"]["trace_id"] + == worker_root_span["contexts"]["trace"]["trace_id"] ) @@ -132,7 +132,7 @@ def test_errors_in_ray_tasks(): def example_task(): 1 / 0 - with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + with sentry_sdk.start_span(op="test", name="ray client root span"): with pytest.raises(ZeroDivisionError): future = example_task.remote() ray.get(future) @@ -167,22 +167,24 @@ def __init__(self): self.n = 0 def increment(self): - with sentry_sdk.start_span(op="task", name="example actor execution"): + with sentry_sdk.start_span( + op="test", name="custom span in actor execution", only_if_parent=True + ): self.n += 1 return sentry_sdk.get_client().transport.envelopes - with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + with sentry_sdk.start_span(op="test", name="ray client root span"): counter = Counter.remote() worker_envelopes = ray.get(counter.increment.remote()) client_envelope = sentry_sdk.get_client().transport.envelopes[0] - client_transaction = client_envelope.get_transaction_event() + client_root_span = client_envelope.get_transaction_event() # Spans for submitting the actor task are not created (actors are not supported yet) - assert client_transaction["spans"] == [] + assert client_root_span["spans"] == [] - # Transaction are not yet created when executing ray actors (actors are not supported yet) + # Root spans are not yet automatically created when executing ray actors (actors are not supported yet) assert worker_envelopes == [] @@ -204,12 +206,14 @@ def __init__(self): self.n = 0 def increment(self): - with sentry_sdk.start_span(op="task", name="example actor execution"): + with sentry_sdk.start_span( + op="test", name="custom span in actor execution", only_if_parent=True + ): 1 / 0 return sentry_sdk.get_client().transport.envelopes - with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + with sentry_sdk.start_span(op="test", name="ray client root span"): with pytest.raises(ZeroDivisionError): counter = Counter.remote() future = counter.increment.remote() From 4e69cb7f56880ba5f1a0041c80cdf2b773ed7deb Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Wed, 18 Dec 2024 10:52:05 +0000 Subject: [PATCH 11/19] =?UTF-8?q?=E2=9C=A8=20Add=20Typer=20integration=20(?= =?UTF-8?q?#3869)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --------- Co-authored-by: Ivana Kellyer --- .github/workflows/test-integrations-misc.yml | 10 +++- requirements-linting.txt | 1 + .../split-tox-gh-actions.py | 1 + sentry_sdk/integrations/typer.py | 60 +++++++++++++++++++ tests/integrations/typer/__init__.py | 3 + tests/integrations/typer/test_typer.py | 52 ++++++++++++++++ tox.ini | 9 +++ 7 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 sentry_sdk/integrations/typer.py create mode 100644 tests/integrations/typer/__init__.py create mode 100644 tests/integrations/typer/test_typer.py diff --git a/.github/workflows/test-integrations-misc.yml b/.github/workflows/test-integrations-misc.yml index fb76a854fb..b88b256384 100644 --- a/.github/workflows/test-integrations-misc.yml +++ b/.github/workflows/test-integrations-misc.yml @@ -27,7 +27,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.6","3.8","3.12","3.13"] + python-version: ["3.6","3.7","3.8","3.12","3.13"] # python3.6 reached EOL and is no longer being supported on # new versions of hosted runners on Github Actions # ubuntu-20.04 is the last version that supported python3.6 @@ -73,6 +73,10 @@ jobs: run: | set -x # print commands that are executed ./scripts/runtox.sh "py${{ matrix.python-version }}-trytond-latest" + - name: Test typer latest + run: | + set -x # print commands that are executed + ./scripts/runtox.sh "py${{ matrix.python-version }}-typer-latest" - name: Generate coverage XML (Python 3.6) if: ${{ !cancelled() && matrix.python-version == '3.6' }} run: | @@ -153,6 +157,10 @@ jobs: run: | set -x # print commands that are executed ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-trytond" + - name: Test typer pinned + run: | + set -x # print commands that are executed + ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-typer" - name: Generate coverage XML (Python 3.6) if: ${{ !cancelled() && matrix.python-version == '3.6' }} run: | diff --git a/requirements-linting.txt b/requirements-linting.txt index c9d4bd7f5c..c3f39ecd1f 100644 --- a/requirements-linting.txt +++ b/requirements-linting.txt @@ -17,3 +17,4 @@ pre-commit # local linting httpcore openfeature-sdk launchdarkly-server-sdk +typer diff --git a/scripts/split-tox-gh-actions/split-tox-gh-actions.py b/scripts/split-tox-gh-actions/split-tox-gh-actions.py index c4b8f3e5e5..26d13390c2 100755 --- a/scripts/split-tox-gh-actions/split-tox-gh-actions.py +++ b/scripts/split-tox-gh-actions/split-tox-gh-actions.py @@ -132,6 +132,7 @@ "potel", "pure_eval", "trytond", + "typer", ], } diff --git a/sentry_sdk/integrations/typer.py b/sentry_sdk/integrations/typer.py new file mode 100644 index 0000000000..8879d6d0d0 --- /dev/null +++ b/sentry_sdk/integrations/typer.py @@ -0,0 +1,60 @@ +import sentry_sdk +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, +) +from sentry_sdk.integrations import Integration, DidNotEnable + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Callable + from typing import Any + from typing import Type + from typing import Optional + + from types import TracebackType + + Excepthook = Callable[ + [Type[BaseException], BaseException, Optional[TracebackType]], + Any, + ] + +try: + import typer +except ImportError: + raise DidNotEnable("Typer not installed") + + +class TyperIntegration(Integration): + identifier = "typer" + + @staticmethod + def setup_once(): + # type: () -> None + typer.main.except_hook = _make_excepthook(typer.main.except_hook) # type: ignore + + +def _make_excepthook(old_excepthook): + # type: (Excepthook) -> Excepthook + def sentry_sdk_excepthook(type_, value, traceback): + # type: (Type[BaseException], BaseException, Optional[TracebackType]) -> None + integration = sentry_sdk.get_client().get_integration(TyperIntegration) + + # Note: If we replace this with ensure_integration_enabled then + # we break the exceptiongroup backport; + # See: https://github.com/getsentry/sentry-python/issues/3097 + if integration is None: + return old_excepthook(type_, value, traceback) + + with capture_internal_exceptions(): + event, hint = event_from_exception( + (type_, value, traceback), + client_options=sentry_sdk.get_client().options, + mechanism={"type": "typer", "handled": False}, + ) + sentry_sdk.capture_event(event, hint=hint) + + return old_excepthook(type_, value, traceback) + + return sentry_sdk_excepthook diff --git a/tests/integrations/typer/__init__.py b/tests/integrations/typer/__init__.py new file mode 100644 index 0000000000..3b7c8011ea --- /dev/null +++ b/tests/integrations/typer/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("typer") diff --git a/tests/integrations/typer/test_typer.py b/tests/integrations/typer/test_typer.py new file mode 100644 index 0000000000..34ac0a7c8c --- /dev/null +++ b/tests/integrations/typer/test_typer.py @@ -0,0 +1,52 @@ +import subprocess +import sys +from textwrap import dedent +import pytest + +from typer.testing import CliRunner + +runner = CliRunner() + + +def test_catch_exceptions(tmpdir): + app = tmpdir.join("app.py") + + app.write( + dedent( + """ + import typer + from unittest import mock + + from sentry_sdk import init, transport + from sentry_sdk.integrations.typer import TyperIntegration + + def capture_envelope(self, envelope): + print("capture_envelope was called") + event = envelope.get_event() + if event is not None: + print(event) + + transport.HttpTransport.capture_envelope = capture_envelope + + init("http://foobar@localhost/123", integrations=[TyperIntegration()]) + + app = typer.Typer() + + @app.command() + def test(): + print("test called") + raise Exception("pollo") + + app() + """ + ) + ) + + with pytest.raises(subprocess.CalledProcessError) as excinfo: + subprocess.check_output([sys.executable, str(app)], stderr=subprocess.STDOUT) + + output = excinfo.value.output + + assert b"capture_envelope was called" in output + assert b"test called" in output + assert b"pollo" in output diff --git a/tox.ini b/tox.ini index 9ccc4dc0eb..717ea62141 100644 --- a/tox.ini +++ b/tox.ini @@ -287,6 +287,10 @@ envlist = {py3.8,py3.11,py3.12}-trytond-v{7} {py3.8,py3.12,py3.13}-trytond-latest + # Typer + {py3.7,py3.12,py3.13}-typer-v{0.15} + {py3.7,py3.12,py3.13}-typer-latest + [testenv] deps = # if you change requirements-testing.txt and your change is not being reflected @@ -724,6 +728,10 @@ deps = trytond-v7: trytond~=7.0 trytond-latest: trytond + # Typer + typer-v0.15: typer~=0.15.0 + typer-latest: typer + setenv = PYTHONDONTWRITEBYTECODE=1 OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES @@ -786,6 +794,7 @@ setenv = strawberry: TESTPATH=tests/integrations/strawberry tornado: TESTPATH=tests/integrations/tornado trytond: TESTPATH=tests/integrations/trytond + typer: TESTPATH=tests/integrations/typer socket: TESTPATH=tests/integrations/socket passenv = From 7fca789025ecc6980d537502027f417c55d5c8c2 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 18 Dec 2024 17:30:45 +0530 Subject: [PATCH 12/19] Ignore opentelemetry logger in logging integration (#3880) Without this, internal otel logs (especially `logger.exception`s) will show up as events / breadcrumbs in the payload. --- sentry_sdk/integrations/logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index b792510d6c..314780cabd 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -40,7 +40,7 @@ # Note: Ignoring by logger name here is better than mucking with thread-locals. # We do not necessarily know whether thread-locals work 100% correctly in the user's environment. _IGNORED_LOGGERS = set( - ["sentry_sdk.errors", "urllib3.connectionpool", "urllib3.connection"] + ["sentry_sdk.errors", "urllib3.connectionpool", "urllib3.connection", "opentelemetry.*"] ) From 858cacdcfe9d702c9f7ef1ecde83b098c91b2891 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 18 Dec 2024 20:41:55 +0530 Subject: [PATCH 13/19] Implement span limits on span processor (#3881) --- sentry_sdk/integrations/arq.py | 2 -- sentry_sdk/integrations/logging.py | 7 ++++++- .../opentelemetry/span_processor.py | 19 ++++++++++++++++++- tests/tracing/test_misc.py | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/integrations/arq.py b/sentry_sdk/integrations/arq.py index c26db4520c..0f42050cf2 100644 --- a/sentry_sdk/integrations/arq.py +++ b/sentry_sdk/integrations/arq.py @@ -1,7 +1,5 @@ import sys -from opentelemetry.trace.status import StatusCode - import sentry_sdk from sentry_sdk.consts import OP, SPANSTATUS from sentry_sdk.integrations import DidNotEnable, Integration diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index 314780cabd..52c56a8e60 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -40,7 +40,12 @@ # Note: Ignoring by logger name here is better than mucking with thread-locals. # We do not necessarily know whether thread-locals work 100% correctly in the user's environment. _IGNORED_LOGGERS = set( - ["sentry_sdk.errors", "urllib3.connectionpool", "urllib3.connection", "opentelemetry.*"] + [ + "sentry_sdk.errors", + "urllib3.connectionpool", + "urllib3.connection", + "opentelemetry.*", + ] ) diff --git a/sentry_sdk/integrations/opentelemetry/span_processor.py b/sentry_sdk/integrations/opentelemetry/span_processor.py index 0b4c3387df..42ad32a5ea 100644 --- a/sentry_sdk/integrations/opentelemetry/span_processor.py +++ b/sentry_sdk/integrations/opentelemetry/span_processor.py @@ -43,6 +43,9 @@ from sentry_sdk._types import Event +DEFAULT_MAX_SPANS = 1000 + + class SentrySpanProcessor(SpanProcessor): """ Converts OTel spans into Sentry spans so they can be sent to the Sentry backend. @@ -79,7 +82,7 @@ def on_end(self, span): # if have a root span ending, we build a transaction and send it self._flush_root_span(span) else: - self._children_spans[span.parent.span_id].append(span) + self._append_child_span(span) # TODO-neel-potel not sure we need a clear like JS def shutdown(self): @@ -150,6 +153,20 @@ def _flush_root_span(self, span): sentry_sdk.capture_event(transaction_event) + def _append_child_span(self, span): + # type: (ReadableSpan) -> None + if not span.parent: + return + + max_spans = ( + sentry_sdk.get_client().options["_experiments"].get("max_spans") + or DEFAULT_MAX_SPANS + ) + + children_spans = self._children_spans[span.parent.span_id] + if len(children_spans) < max_spans: + children_spans.append(span) + def _collect_children(self, span): # type: (ReadableSpan) -> List[ReadableSpan] if not span.context: diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index 0d12acc617..dc5754cfd2 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -8,6 +8,25 @@ from sentry_sdk.utils import Dsn +def test_span_trimming(sentry_init, capture_events): + sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3}) + events = capture_events() + + with start_span(name="hi"): + for i in range(10): + with start_span(op="foo{}".format(i)): + pass + + (event,) = events + + assert len(event["spans"]) == 3 + + span1, span2, span3 = event["spans"] + assert span1["op"] == "foo0" + assert span2["op"] == "foo1" + assert span3["op"] == "foo2" + + def test_transaction_naming(sentry_init, capture_events): sentry_init(traces_sample_rate=1.0) events = capture_events() From 50222ca2a6c680bb0e712b3bc8a1813d83fa55a0 Mon Sep 17 00:00:00 2001 From: Andrew Liu <159852527+aliu39@users.noreply.github.com> Date: Wed, 18 Dec 2024 20:33:36 -0800 Subject: [PATCH 14/19] feat(flags): Add integration for custom tracking of flag evaluations (#3860) * Add new integration and unit tests * Test flag values for LD and OF threaded/asyncio, not just flag names * update ffIntegration test to be e2e, and fix LRU copy bug * make a helper fixture and test error processor in original thread * Move api to top-level, rename to add_flag * Add docstrs * Rename to add_feature_flag * Rm extra import in test_lru_cache * Revert lru comment * Type annotate * Review comments * Update launchdarkly and openfeature tests to be e2e * Update docstrs * Skip threading test for <3.7 * Skip ffs asyncio test if 3.6 * undo 'skip threading test' * Try commenting out asyncio * Use importorskip * Import order --------- Co-authored-by: Anton Pirker --- sentry_sdk/integrations/featureflags.py | 44 ++++++ tests/conftest.py | 11 ++ tests/integrations/featureflags/__init__.py | 0 .../featureflags/test_featureflags.py | 133 ++++++++++++++++++ .../launchdarkly/test_launchdarkly.py | 119 +++++++++++++--- .../openfeature/test_openfeature.py | 113 ++++++++++++--- 6 files changed, 377 insertions(+), 43 deletions(-) create mode 100644 sentry_sdk/integrations/featureflags.py create mode 100644 tests/integrations/featureflags/__init__.py create mode 100644 tests/integrations/featureflags/test_featureflags.py diff --git a/sentry_sdk/integrations/featureflags.py b/sentry_sdk/integrations/featureflags.py new file mode 100644 index 0000000000..46947eec72 --- /dev/null +++ b/sentry_sdk/integrations/featureflags.py @@ -0,0 +1,44 @@ +from sentry_sdk.flag_utils import flag_error_processor + +import sentry_sdk +from sentry_sdk.integrations import Integration + + +class FeatureFlagsIntegration(Integration): + """ + Sentry integration for capturing feature flags on error events. To manually buffer flag data, + call `integrations.featureflags.add_feature_flag`. We recommend you do this on each flag + evaluation. + + See the [feature flag documentation](https://develop.sentry.dev/sdk/expected-features/#feature-flags) + for more information. + + @example + ``` + import sentry_sdk + from sentry_sdk.integrations.featureflags import FeatureFlagsIntegration, add_feature_flag + + sentry_sdk.init(dsn="my_dsn", integrations=[FeatureFlagsIntegration()]); + + add_feature_flag('my-flag', true); + sentry_sdk.capture_exception(Exception('broke')); // 'my-flag' should be captured on this Sentry event. + ``` + """ + + identifier = "featureflags" + + @staticmethod + def setup_once(): + # type: () -> None + scope = sentry_sdk.get_current_scope() + scope.add_error_processor(flag_error_processor) + + +def add_feature_flag(flag, result): + # type: (str, bool) -> None + """ + Records a flag and its value to be sent on subsequent error events by FeatureFlagsIntegration. + We recommend you do this on flag evaluations. Flags are buffered per Sentry scope. + """ + flags = sentry_sdk.get_current_scope().flags + flags.set(flag, result) diff --git a/tests/conftest.py b/tests/conftest.py index 64527c1e36..c0383d94b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -184,6 +184,17 @@ def reset_integrations(): _installed_integrations.clear() +@pytest.fixture +def uninstall_integration(): + """Use to force the next call to sentry_init to re-install/setup an integration.""" + + def inner(identifier): + _processed_integrations.discard(identifier) + _installed_integrations.discard(identifier) + + return inner + + @pytest.fixture def sentry_init(request): def inner(*a, **kw): diff --git a/tests/integrations/featureflags/__init__.py b/tests/integrations/featureflags/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integrations/featureflags/test_featureflags.py b/tests/integrations/featureflags/test_featureflags.py new file mode 100644 index 0000000000..539e910607 --- /dev/null +++ b/tests/integrations/featureflags/test_featureflags.py @@ -0,0 +1,133 @@ +import concurrent.futures as cf +import sys + +import pytest + +import sentry_sdk +from sentry_sdk.integrations.featureflags import ( + FeatureFlagsIntegration, + add_feature_flag, +) + + +def test_featureflags_integration(sentry_init, capture_events, uninstall_integration): + uninstall_integration(FeatureFlagsIntegration.identifier) + sentry_init(integrations=[FeatureFlagsIntegration()]) + + add_feature_flag("hello", False) + add_feature_flag("world", True) + add_feature_flag("other", False) + + events = capture_events() + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 1 + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": False}, + {"flag": "world", "result": True}, + {"flag": "other", "result": False}, + ] + } + + +def test_featureflags_integration_threaded( + sentry_init, capture_events, uninstall_integration +): + uninstall_integration(FeatureFlagsIntegration.identifier) + sentry_init(integrations=[FeatureFlagsIntegration()]) + events = capture_events() + + # Capture an eval before we split isolation scopes. + add_feature_flag("hello", False) + + def task(flag_key): + # Creates a new isolation scope for the thread. + # This means the evaluations in each task are captured separately. + with sentry_sdk.isolation_scope(): + add_feature_flag(flag_key, False) + # use a tag to identify to identify events later on + sentry_sdk.set_tag("task_id", flag_key) + sentry_sdk.capture_exception(Exception("something wrong!")) + + # Run tasks in separate threads + with cf.ThreadPoolExecutor(max_workers=2) as pool: + pool.map(task, ["world", "other"]) + + # Capture error in original scope + sentry_sdk.set_tag("task_id", "0") + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 3 + events.sort(key=lambda e: e["tags"]["task_id"]) + + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": False}, + ] + } + assert events[1]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": False}, + {"flag": "other", "result": False}, + ] + } + assert events[2]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": False}, + {"flag": "world", "result": False}, + ] + } + + +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") +def test_featureflags_integration_asyncio( + sentry_init, capture_events, uninstall_integration +): + asyncio = pytest.importorskip("asyncio") + + uninstall_integration(FeatureFlagsIntegration.identifier) + sentry_init(integrations=[FeatureFlagsIntegration()]) + events = capture_events() + + # Capture an eval before we split isolation scopes. + add_feature_flag("hello", False) + + async def task(flag_key): + # Creates a new isolation scope for the thread. + # This means the evaluations in each task are captured separately. + with sentry_sdk.isolation_scope(): + add_feature_flag(flag_key, False) + # use a tag to identify to identify events later on + sentry_sdk.set_tag("task_id", flag_key) + sentry_sdk.capture_exception(Exception("something wrong!")) + + async def runner(): + return asyncio.gather(task("world"), task("other")) + + asyncio.run(runner()) + + # Capture error in original scope + sentry_sdk.set_tag("task_id", "0") + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 3 + events.sort(key=lambda e: e["tags"]["task_id"]) + + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": False}, + ] + } + assert events[1]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": False}, + {"flag": "other", "result": False}, + ] + } + assert events[2]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": False}, + {"flag": "world", "result": False}, + ] + } diff --git a/tests/integrations/launchdarkly/test_launchdarkly.py b/tests/integrations/launchdarkly/test_launchdarkly.py index acbe764104..f66a4219ec 100644 --- a/tests/integrations/launchdarkly/test_launchdarkly.py +++ b/tests/integrations/launchdarkly/test_launchdarkly.py @@ -1,9 +1,7 @@ -import asyncio import concurrent.futures as cf +import sys import ldclient - -import sentry_sdk import pytest from ldclient import LDClient @@ -11,6 +9,7 @@ from ldclient.context import Context from ldclient.integrations.test_data import TestData +import sentry_sdk from sentry_sdk.integrations import DidNotEnable from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration @@ -19,9 +18,13 @@ "use_global_client", (False, True), ) -def test_launchdarkly_integration(sentry_init, use_global_client): +def test_launchdarkly_integration( + sentry_init, use_global_client, capture_events, uninstall_integration +): td = TestData.data_source() config = Config("sdk-key", update_processor_class=td) + + uninstall_integration(LaunchDarklyIntegration.identifier) if use_global_client: ldclient.set_config(config) sentry_init(integrations=[LaunchDarklyIntegration()]) @@ -39,25 +42,38 @@ def test_launchdarkly_integration(sentry_init, use_global_client): client.variation("world", Context.create("user1", "user"), False) client.variation("other", Context.create("user2", "user"), False) - assert sentry_sdk.get_current_scope().flags.get() == [ - {"flag": "hello", "result": True}, - {"flag": "world", "result": True}, - {"flag": "other", "result": False}, - ] + events = capture_events() + sentry_sdk.capture_exception(Exception("something wrong!")) + assert len(events) == 1 + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": True}, + {"flag": "other", "result": False}, + ] + } -def test_launchdarkly_integration_threaded(sentry_init): + +def test_launchdarkly_integration_threaded( + sentry_init, capture_events, uninstall_integration +): td = TestData.data_source() client = LDClient(config=Config("sdk-key", update_processor_class=td)) - sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) context = Context.create("user1") + uninstall_integration(LaunchDarklyIntegration.identifier) + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) + events = capture_events() + def task(flag_key): # Creates a new isolation scope for the thread. # This means the evaluations in each task are captured separately. with sentry_sdk.isolation_scope(): client.variation(flag_key, context, False) - return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + # use a tag to identify to identify events later on + sentry_sdk.set_tag("task_id", flag_key) + sentry_sdk.capture_exception(Exception("something wrong!")) td.update(td.flag("hello").variation_for_all(True)) td.update(td.flag("world").variation_for_all(False)) @@ -65,34 +81,91 @@ def task(flag_key): client.variation("hello", context, False) with cf.ThreadPoolExecutor(max_workers=2) as pool: - results = list(pool.map(task, ["world", "other"])) - - assert results[0] == ["hello", "world"] - assert results[1] == ["hello", "other"] + pool.map(task, ["world", "other"]) + + # Capture error in original scope + sentry_sdk.set_tag("task_id", "0") + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 3 + events.sort(key=lambda e: e["tags"]["task_id"]) + + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + ] + } + assert events[1]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "other", "result": False}, + ] + } + assert events[2]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": False}, + ] + } + + +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") +def test_launchdarkly_integration_asyncio( + sentry_init, capture_events, uninstall_integration +): + """Assert concurrently evaluated flags do not pollute one another.""" + asyncio = pytest.importorskip("asyncio") -def test_launchdarkly_integration_asyncio(sentry_init): - """Assert concurrently evaluated flags do not pollute one another.""" td = TestData.data_source() client = LDClient(config=Config("sdk-key", update_processor_class=td)) - sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) context = Context.create("user1") + uninstall_integration(LaunchDarklyIntegration.identifier) + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) + events = capture_events() + async def task(flag_key): with sentry_sdk.isolation_scope(): client.variation(flag_key, context, False) - return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + # use a tag to identify to identify events later on + sentry_sdk.set_tag("task_id", flag_key) + sentry_sdk.capture_exception(Exception("something wrong!")) async def runner(): return asyncio.gather(task("world"), task("other")) td.update(td.flag("hello").variation_for_all(True)) td.update(td.flag("world").variation_for_all(False)) + # Capture an eval before we split isolation scopes. client.variation("hello", context, False) - results = asyncio.run(runner()).result() - assert results[0] == ["hello", "world"] - assert results[1] == ["hello", "other"] + asyncio.run(runner()) + + # Capture error in original scope + sentry_sdk.set_tag("task_id", "0") + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 3 + events.sort(key=lambda e: e["tags"]["task_id"]) + + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + ] + } + assert events[1]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "other", "result": False}, + ] + } + assert events[2]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": False}, + ] + } def test_launchdarkly_integration_did_not_enable(monkeypatch): diff --git a/tests/integrations/openfeature/test_openfeature.py b/tests/integrations/openfeature/test_openfeature.py index 24e7857f9a..c180211c3f 100644 --- a/tests/integrations/openfeature/test_openfeature.py +++ b/tests/integrations/openfeature/test_openfeature.py @@ -1,13 +1,17 @@ -import asyncio import concurrent.futures as cf -import sentry_sdk +import sys + +import pytest from openfeature import api from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider + +import sentry_sdk from sentry_sdk.integrations.openfeature import OpenFeatureIntegration -def test_openfeature_integration(sentry_init): +def test_openfeature_integration(sentry_init, capture_events, uninstall_integration): + uninstall_integration(OpenFeatureIntegration.identifier) sentry_init(integrations=[OpenFeatureIntegration()]) flags = { @@ -21,15 +25,25 @@ def test_openfeature_integration(sentry_init): client.get_boolean_value("world", default_value=False) client.get_boolean_value("other", default_value=True) - assert sentry_sdk.get_current_scope().flags.get() == [ - {"flag": "hello", "result": True}, - {"flag": "world", "result": False}, - {"flag": "other", "result": True}, - ] + events = capture_events() + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 1 + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": False}, + {"flag": "other", "result": True}, + ] + } -def test_openfeature_integration_threaded(sentry_init): +def test_openfeature_integration_threaded( + sentry_init, capture_events, uninstall_integration +): + uninstall_integration(OpenFeatureIntegration.identifier) sentry_init(integrations=[OpenFeatureIntegration()]) + events = capture_events() flags = { "hello": InMemoryFlag("on", {"on": True, "off": False}), @@ -37,6 +51,7 @@ def test_openfeature_integration_threaded(sentry_init): } api.set_provider(InMemoryProvider(flags)) + # Capture an eval before we split isolation scopes. client = api.get_client() client.get_boolean_value("hello", default_value=False) @@ -44,37 +59,95 @@ def task(flag): # Create a new isolation scope for the thread. This means the flags with sentry_sdk.isolation_scope(): client.get_boolean_value(flag, default_value=False) - return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + # use a tag to identify to identify events later on + sentry_sdk.set_tag("task_id", flag) + sentry_sdk.capture_exception(Exception("something wrong!")) + # Run tasks in separate threads with cf.ThreadPoolExecutor(max_workers=2) as pool: - results = list(pool.map(task, ["world", "other"])) + pool.map(task, ["world", "other"]) - assert results[0] == ["hello", "world"] - assert results[1] == ["hello", "other"] + # Capture error in original scope + sentry_sdk.set_tag("task_id", "0") + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 3 + events.sort(key=lambda e: e["tags"]["task_id"]) + + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + ] + } + assert events[1]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "other", "result": False}, + ] + } + assert events[2]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": False}, + ] + } -def test_openfeature_integration_asyncio(sentry_init): +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") +def test_openfeature_integration_asyncio( + sentry_init, capture_events, uninstall_integration +): """Assert concurrently evaluated flags do not pollute one another.""" + asyncio = pytest.importorskip("asyncio") + + uninstall_integration(OpenFeatureIntegration.identifier) + sentry_init(integrations=[OpenFeatureIntegration()]) + events = capture_events() + async def task(flag): with sentry_sdk.isolation_scope(): client.get_boolean_value(flag, default_value=False) - return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] + # use a tag to identify to identify events later on + sentry_sdk.set_tag("task_id", flag) + sentry_sdk.capture_exception(Exception("something wrong!")) async def runner(): return asyncio.gather(task("world"), task("other")) - sentry_init(integrations=[OpenFeatureIntegration()]) - flags = { "hello": InMemoryFlag("on", {"on": True, "off": False}), "world": InMemoryFlag("off", {"on": True, "off": False}), } api.set_provider(InMemoryProvider(flags)) + # Capture an eval before we split isolation scopes. client = api.get_client() client.get_boolean_value("hello", default_value=False) - results = asyncio.run(runner()).result() - assert results[0] == ["hello", "world"] - assert results[1] == ["hello", "other"] + asyncio.run(runner()) + + # Capture error in original scope + sentry_sdk.set_tag("task_id", "0") + sentry_sdk.capture_exception(Exception("something wrong!")) + + assert len(events) == 3 + events.sort(key=lambda e: e["tags"]["task_id"]) + + assert events[0]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + ] + } + assert events[1]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "other", "result": False}, + ] + } + assert events[2]["contexts"]["flags"] == { + "values": [ + {"flag": "hello", "result": True}, + {"flag": "world", "result": False}, + ] + } From fe4b88b8505376ace7c6f8750f83fd2af383190f Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 19 Dec 2024 14:00:09 +0100 Subject: [PATCH 15/19] Add github workflow to comment on issues when a fix was released (#3866) --- .github/workflows/release-comment-issues.yml | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/release-comment-issues.yml diff --git a/.github/workflows/release-comment-issues.yml b/.github/workflows/release-comment-issues.yml new file mode 100644 index 0000000000..d31c61dced --- /dev/null +++ b/.github/workflows/release-comment-issues.yml @@ -0,0 +1,31 @@ +name: "Automation: Notify issues for release" +on: + release: + types: + - published + workflow_dispatch: + inputs: + version: + description: Which version to notify issues for + required: false + +# This workflow is triggered when a release is published +jobs: + release-comment-issues: + runs-on: ubuntu-20.04 + name: Notify issues + steps: + - name: Get version + id: get_version + run: echo "version=${{ github.event.inputs.version || github.event.release.tag_name }}" >> $GITHUB_OUTPUT + + - name: Comment on linked issues that are mentioned in release + if: | + steps.get_version.outputs.version != '' + && !contains(steps.get_version.outputs.version, 'a') + && !contains(steps.get_version.outputs.version, 'b') + && !contains(steps.get_version.outputs.version, 'rc') + uses: getsentry/release-comment-issues-gh-action@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + version: ${{ steps.get_version.outputs.version }} \ No newline at end of file From 54aede36f9d3942c1069b47b20b88f01cb461fb5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:34:49 +0100 Subject: [PATCH 16/19] build(deps): bump codecov/codecov-action from 5.0.7 to 5.1.1 (#3867) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 5.0.7 to 5.1.1. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v5.0.7...v5.1.1) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Anton Pirker --- .github/workflows/test-integrations-ai.yml | 4 ++-- .github/workflows/test-integrations-aws.yml | 2 +- .github/workflows/test-integrations-cloud.yml | 4 ++-- .github/workflows/test-integrations-common.yml | 2 +- .github/workflows/test-integrations-dbs.yml | 4 ++-- .github/workflows/test-integrations-graphql.yml | 4 ++-- .github/workflows/test-integrations-misc.yml | 4 ++-- .github/workflows/test-integrations-network.yml | 4 ++-- .github/workflows/test-integrations-tasks.yml | 4 ++-- .github/workflows/test-integrations-web-1.yml | 4 ++-- .github/workflows/test-integrations-web-2.yml | 4 ++-- scripts/split-tox-gh-actions/templates/test_group.jinja | 2 +- 12 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test-integrations-ai.yml b/.github/workflows/test-integrations-ai.yml index 5d1b05add8..8be64736c1 100644 --- a/.github/workflows/test-integrations-ai.yml +++ b/.github/workflows/test-integrations-ai.yml @@ -78,7 +78,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -150,7 +150,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-aws.yml b/.github/workflows/test-integrations-aws.yml index d2ce22f326..6eed3a3ab1 100644 --- a/.github/workflows/test-integrations-aws.yml +++ b/.github/workflows/test-integrations-aws.yml @@ -97,7 +97,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-cloud.yml b/.github/workflows/test-integrations-cloud.yml index 8fdd4a0649..677385e405 100644 --- a/.github/workflows/test-integrations-cloud.yml +++ b/.github/workflows/test-integrations-cloud.yml @@ -74,7 +74,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -142,7 +142,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-common.yml b/.github/workflows/test-integrations-common.yml index 8294b9480e..9c476553f5 100644 --- a/.github/workflows/test-integrations-common.yml +++ b/.github/workflows/test-integrations-common.yml @@ -62,7 +62,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-dbs.yml b/.github/workflows/test-integrations-dbs.yml index 0d9a7bbd7d..cbaa2c32d2 100644 --- a/.github/workflows/test-integrations-dbs.yml +++ b/.github/workflows/test-integrations-dbs.yml @@ -101,7 +101,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -196,7 +196,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-graphql.yml b/.github/workflows/test-integrations-graphql.yml index 30480efe2e..d582717fff 100644 --- a/.github/workflows/test-integrations-graphql.yml +++ b/.github/workflows/test-integrations-graphql.yml @@ -74,7 +74,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -142,7 +142,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-misc.yml b/.github/workflows/test-integrations-misc.yml index b88b256384..00b1286362 100644 --- a/.github/workflows/test-integrations-misc.yml +++ b/.github/workflows/test-integrations-misc.yml @@ -90,7 +90,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -174,7 +174,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-network.yml b/.github/workflows/test-integrations-network.yml index 0a51866164..8f6bd9fd61 100644 --- a/.github/workflows/test-integrations-network.yml +++ b/.github/workflows/test-integrations-network.yml @@ -74,7 +74,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -142,7 +142,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-tasks.yml b/.github/workflows/test-integrations-tasks.yml index 695c338721..74c868d9b9 100644 --- a/.github/workflows/test-integrations-tasks.yml +++ b/.github/workflows/test-integrations-tasks.yml @@ -92,7 +92,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -178,7 +178,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-web-1.yml b/.github/workflows/test-integrations-web-1.yml index 6e172182b3..5be067a36b 100644 --- a/.github/workflows/test-integrations-web-1.yml +++ b/.github/workflows/test-integrations-web-1.yml @@ -92,7 +92,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -178,7 +178,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-web-2.yml b/.github/workflows/test-integrations-web-2.yml index f9f2651cb8..7ce0399a13 100644 --- a/.github/workflows/test-integrations-web-2.yml +++ b/.github/workflows/test-integrations-web-2.yml @@ -98,7 +98,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -190,7 +190,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/scripts/split-tox-gh-actions/templates/test_group.jinja b/scripts/split-tox-gh-actions/templates/test_group.jinja index 522be6dc5c..7225bbbfe5 100644 --- a/scripts/split-tox-gh-actions/templates/test_group.jinja +++ b/scripts/split-tox-gh-actions/templates/test_group.jinja @@ -92,7 +92,7 @@ - name: Upload coverage to Codecov if: {% raw %}${{ !cancelled() }}{% endraw %} - uses: codecov/codecov-action@v5.0.7 + uses: codecov/codecov-action@v5.1.1 with: token: {% raw %}${{ secrets.CODECOV_TOKEN }}{% endraw %} files: coverage.xml From 6e4cc36fbb66a09f4272176fc8972368e1028ae8 Mon Sep 17 00:00:00 2001 From: seyoon-lim Date: Fri, 20 Dec 2024 16:43:19 +0900 Subject: [PATCH 17/19] Support SparkIntegration activation after SparkContext created (#3411) --------- Co-authored-by: Anton Pirker --- sentry_sdk/integrations/spark/spark_driver.py | 121 +++++++---- tests/integrations/asgi/test_asgi.py | 1 - tests/integrations/spark/test_spark.py | 202 ++++++++++-------- 3 files changed, 189 insertions(+), 135 deletions(-) diff --git a/sentry_sdk/integrations/spark/spark_driver.py b/sentry_sdk/integrations/spark/spark_driver.py index c6470f2302..a86f16344d 100644 --- a/sentry_sdk/integrations/spark/spark_driver.py +++ b/sentry_sdk/integrations/spark/spark_driver.py @@ -9,6 +9,7 @@ from typing import Optional from sentry_sdk._types import Event, Hint + from pyspark import SparkContext class SparkIntegration(Integration): @@ -17,7 +18,7 @@ class SparkIntegration(Integration): @staticmethod def setup_once(): # type: () -> None - patch_spark_context_init() + _setup_sentry_tracing() def _set_app_properties(): @@ -37,7 +38,7 @@ def _set_app_properties(): def _start_sentry_listener(sc): - # type: (Any) -> None + # type: (SparkContext) -> None """ Start java gateway server to add custom `SparkListener` """ @@ -49,7 +50,51 @@ def _start_sentry_listener(sc): sc._jsc.sc().addSparkListener(listener) -def patch_spark_context_init(): +def _add_event_processor(sc): + # type: (SparkContext) -> None + scope = sentry_sdk.get_isolation_scope() + + @scope.add_event_processor + def process_event(event, hint): + # type: (Event, Hint) -> Optional[Event] + with capture_internal_exceptions(): + if sentry_sdk.get_client().get_integration(SparkIntegration) is None: + return event + + if sc._active_spark_context is None: + return event + + event.setdefault("user", {}).setdefault("id", sc.sparkUser()) + + event.setdefault("tags", {}).setdefault( + "executor.id", sc._conf.get("spark.executor.id") + ) + event["tags"].setdefault( + "spark-submit.deployMode", + sc._conf.get("spark.submit.deployMode"), + ) + event["tags"].setdefault("driver.host", sc._conf.get("spark.driver.host")) + event["tags"].setdefault("driver.port", sc._conf.get("spark.driver.port")) + event["tags"].setdefault("spark_version", sc.version) + event["tags"].setdefault("app_name", sc.appName) + event["tags"].setdefault("application_id", sc.applicationId) + event["tags"].setdefault("master", sc.master) + event["tags"].setdefault("spark_home", sc.sparkHome) + + event.setdefault("extra", {}).setdefault("web_url", sc.uiWebUrl) + + return event + + +def _activate_integration(sc): + # type: (SparkContext) -> None + + _start_sentry_listener(sc) + _set_app_properties() + _add_event_processor(sc) + + +def _patch_spark_context_init(): # type: () -> None from pyspark import SparkContext @@ -59,51 +104,22 @@ def patch_spark_context_init(): def _sentry_patched_spark_context_init(self, *args, **kwargs): # type: (SparkContext, *Any, **Any) -> Optional[Any] rv = spark_context_init(self, *args, **kwargs) - _start_sentry_listener(self) - _set_app_properties() - - scope = sentry_sdk.get_isolation_scope() - - @scope.add_event_processor - def process_event(event, hint): - # type: (Event, Hint) -> Optional[Event] - with capture_internal_exceptions(): - if sentry_sdk.get_client().get_integration(SparkIntegration) is None: - return event - - if self._active_spark_context is None: - return event - - event.setdefault("user", {}).setdefault("id", self.sparkUser()) - - event.setdefault("tags", {}).setdefault( - "executor.id", self._conf.get("spark.executor.id") - ) - event["tags"].setdefault( - "spark-submit.deployMode", - self._conf.get("spark.submit.deployMode"), - ) - event["tags"].setdefault( - "driver.host", self._conf.get("spark.driver.host") - ) - event["tags"].setdefault( - "driver.port", self._conf.get("spark.driver.port") - ) - event["tags"].setdefault("spark_version", self.version) - event["tags"].setdefault("app_name", self.appName) - event["tags"].setdefault("application_id", self.applicationId) - event["tags"].setdefault("master", self.master) - event["tags"].setdefault("spark_home", self.sparkHome) - - event.setdefault("extra", {}).setdefault("web_url", self.uiWebUrl) - - return event - + _activate_integration(self) return rv SparkContext._do_init = _sentry_patched_spark_context_init +def _setup_sentry_tracing(): + # type: () -> None + from pyspark import SparkContext + + if SparkContext._active_spark_context is not None: + _activate_integration(SparkContext._active_spark_context) + return + _patch_spark_context_init() + + class SparkListener: def onApplicationEnd(self, applicationEnd): # noqa: N802,N803 # type: (Any) -> None @@ -208,10 +224,21 @@ class Java: class SentryListener(SparkListener): + def _add_breadcrumb( + self, + level, # type: str + message, # type: str + data=None, # type: Optional[dict[str, Any]] + ): + # type: (...) -> None + sentry_sdk.get_global_scope().add_breadcrumb( + level=level, message=message, data=data + ) + def onJobStart(self, jobStart): # noqa: N802,N803 # type: (Any) -> None message = "Job {} Started".format(jobStart.jobId()) - sentry_sdk.add_breadcrumb(level="info", message=message) + self._add_breadcrumb(level="info", message=message) _set_app_properties() def onJobEnd(self, jobEnd): # noqa: N802,N803 @@ -227,14 +254,14 @@ def onJobEnd(self, jobEnd): # noqa: N802,N803 level = "warning" message = "Job {} Failed".format(jobEnd.jobId()) - sentry_sdk.add_breadcrumb(level=level, message=message, data=data) + self._add_breadcrumb(level=level, message=message, data=data) def onStageSubmitted(self, stageSubmitted): # noqa: N802,N803 # type: (Any) -> None stage_info = stageSubmitted.stageInfo() message = "Stage {} Submitted".format(stage_info.stageId()) data = {"attemptId": stage_info.attemptId(), "name": stage_info.name()} - sentry_sdk.add_breadcrumb(level="info", message=message, data=data) + self._add_breadcrumb(level="info", message=message, data=data) _set_app_properties() def onStageCompleted(self, stageCompleted): # noqa: N802,N803 @@ -255,4 +282,4 @@ def onStageCompleted(self, stageCompleted): # noqa: N802,N803 message = "Stage {} Completed".format(stage_info.stageId()) level = "info" - sentry_sdk.add_breadcrumb(level=level, message=message, data=data) + self._add_breadcrumb(level=level, message=message, data=data) diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index e0a3900a38..f3bc7147bf 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -128,7 +128,6 @@ async def app(scope, receive, send): @pytest.fixture def asgi3_custom_transaction_app(): - async def app(scope, receive, send): sentry_sdk.get_current_scope().set_transaction_name("foobar", source="custom") await send( diff --git a/tests/integrations/spark/test_spark.py b/tests/integrations/spark/test_spark.py index 58c8862ee2..44ba9f8728 100644 --- a/tests/integrations/spark/test_spark.py +++ b/tests/integrations/spark/test_spark.py @@ -1,6 +1,7 @@ import pytest import sys from unittest.mock import patch + from sentry_sdk.integrations.spark.spark_driver import ( _set_app_properties, _start_sentry_listener, @@ -18,8 +19,22 @@ ################ -def test_set_app_properties(): - spark_context = SparkContext(appName="Testing123") +@pytest.fixture(scope="function") +def sentry_init_with_reset(sentry_init): + from sentry_sdk.integrations import _processed_integrations + + yield lambda: sentry_init(integrations=[SparkIntegration()]) + _processed_integrations.remove("spark") + + +@pytest.fixture(scope="function") +def create_spark_context(): + yield lambda: SparkContext(appName="Testing123") + SparkContext._active_spark_context.stop() + + +def test_set_app_properties(create_spark_context): + spark_context = create_spark_context() _set_app_properties() assert spark_context.getLocalProperty("sentry_app_name") == "Testing123" @@ -30,9 +45,8 @@ def test_set_app_properties(): ) -def test_start_sentry_listener(): - spark_context = SparkContext.getOrCreate() - +def test_start_sentry_listener(create_spark_context): + spark_context = create_spark_context() gateway = spark_context._gateway assert gateway._callback_server is None @@ -41,9 +55,28 @@ def test_start_sentry_listener(): assert gateway._callback_server is not None -def test_initialize_spark_integration(sentry_init): - sentry_init(integrations=[SparkIntegration()]) - SparkContext.getOrCreate() +@patch("sentry_sdk.integrations.spark.spark_driver._patch_spark_context_init") +def test_initialize_spark_integration_before_spark_context_init( + mock_patch_spark_context_init, + sentry_init_with_reset, + create_spark_context, +): + sentry_init_with_reset() + create_spark_context() + + mock_patch_spark_context_init.assert_called_once() + + +@patch("sentry_sdk.integrations.spark.spark_driver._activate_integration") +def test_initialize_spark_integration_after_spark_context_init( + mock_activate_integration, + create_spark_context, + sentry_init_with_reset, +): + create_spark_context() + sentry_init_with_reset() + + mock_activate_integration.assert_called_once() @pytest.fixture @@ -54,88 +87,83 @@ def sentry_listener(): return listener -@pytest.fixture -def mock_add_breadcrumb(): - with patch("sentry_sdk.add_breadcrumb") as mock: - yield mock - - -def test_sentry_listener_on_job_start(sentry_listener, mock_add_breadcrumb): +def test_sentry_listener_on_job_start(sentry_listener): listener = sentry_listener + with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb: - class MockJobStart: - def jobId(self): # noqa: N802 - return "sample-job-id-start" + class MockJobStart: + def jobId(self): # noqa: N802 + return "sample-job-id-start" - mock_job_start = MockJobStart() - listener.onJobStart(mock_job_start) + mock_job_start = MockJobStart() + listener.onJobStart(mock_job_start) - mock_add_breadcrumb.assert_called_once() - mock_hub = mock_add_breadcrumb.call_args + mock_add_breadcrumb.assert_called_once() + mock_hub = mock_add_breadcrumb.call_args - assert mock_hub.kwargs["level"] == "info" - assert "sample-job-id-start" in mock_hub.kwargs["message"] + assert mock_hub.kwargs["level"] == "info" + assert "sample-job-id-start" in mock_hub.kwargs["message"] @pytest.mark.parametrize( "job_result, level", [("JobSucceeded", "info"), ("JobFailed", "warning")] ) -def test_sentry_listener_on_job_end( - sentry_listener, mock_add_breadcrumb, job_result, level -): +def test_sentry_listener_on_job_end(sentry_listener, job_result, level): listener = sentry_listener + with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb: - class MockJobResult: - def toString(self): # noqa: N802 - return job_result + class MockJobResult: + def toString(self): # noqa: N802 + return job_result - class MockJobEnd: - def jobId(self): # noqa: N802 - return "sample-job-id-end" + class MockJobEnd: + def jobId(self): # noqa: N802 + return "sample-job-id-end" - def jobResult(self): # noqa: N802 - result = MockJobResult() - return result + def jobResult(self): # noqa: N802 + result = MockJobResult() + return result - mock_job_end = MockJobEnd() - listener.onJobEnd(mock_job_end) + mock_job_end = MockJobEnd() + listener.onJobEnd(mock_job_end) - mock_add_breadcrumb.assert_called_once() - mock_hub = mock_add_breadcrumb.call_args + mock_add_breadcrumb.assert_called_once() + mock_hub = mock_add_breadcrumb.call_args - assert mock_hub.kwargs["level"] == level - assert mock_hub.kwargs["data"]["result"] == job_result - assert "sample-job-id-end" in mock_hub.kwargs["message"] + assert mock_hub.kwargs["level"] == level + assert mock_hub.kwargs["data"]["result"] == job_result + assert "sample-job-id-end" in mock_hub.kwargs["message"] -def test_sentry_listener_on_stage_submitted(sentry_listener, mock_add_breadcrumb): +def test_sentry_listener_on_stage_submitted(sentry_listener): listener = sentry_listener + with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb: - class StageInfo: - def stageId(self): # noqa: N802 - return "sample-stage-id-submit" + class StageInfo: + def stageId(self): # noqa: N802 + return "sample-stage-id-submit" - def name(self): - return "run-job" + def name(self): + return "run-job" - def attemptId(self): # noqa: N802 - return 14 + def attemptId(self): # noqa: N802 + return 14 - class MockStageSubmitted: - def stageInfo(self): # noqa: N802 - stageinf = StageInfo() - return stageinf + class MockStageSubmitted: + def stageInfo(self): # noqa: N802 + stageinf = StageInfo() + return stageinf - mock_stage_submitted = MockStageSubmitted() - listener.onStageSubmitted(mock_stage_submitted) + mock_stage_submitted = MockStageSubmitted() + listener.onStageSubmitted(mock_stage_submitted) - mock_add_breadcrumb.assert_called_once() - mock_hub = mock_add_breadcrumb.call_args + mock_add_breadcrumb.assert_called_once() + mock_hub = mock_add_breadcrumb.call_args - assert mock_hub.kwargs["level"] == "info" - assert "sample-stage-id-submit" in mock_hub.kwargs["message"] - assert mock_hub.kwargs["data"]["attemptId"] == 14 - assert mock_hub.kwargs["data"]["name"] == "run-job" + assert mock_hub.kwargs["level"] == "info" + assert "sample-stage-id-submit" in mock_hub.kwargs["message"] + assert mock_hub.kwargs["data"]["attemptId"] == 14 + assert mock_hub.kwargs["data"]["name"] == "run-job" @pytest.fixture @@ -175,39 +203,39 @@ def stageInfo(self): # noqa: N802 def test_sentry_listener_on_stage_completed_success( - sentry_listener, mock_add_breadcrumb, get_mock_stage_completed + sentry_listener, get_mock_stage_completed ): listener = sentry_listener + with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb: + mock_stage_completed = get_mock_stage_completed(failure_reason=False) + listener.onStageCompleted(mock_stage_completed) - mock_stage_completed = get_mock_stage_completed(failure_reason=False) - listener.onStageCompleted(mock_stage_completed) - - mock_add_breadcrumb.assert_called_once() - mock_hub = mock_add_breadcrumb.call_args + mock_add_breadcrumb.assert_called_once() + mock_hub = mock_add_breadcrumb.call_args - assert mock_hub.kwargs["level"] == "info" - assert "sample-stage-id-submit" in mock_hub.kwargs["message"] - assert mock_hub.kwargs["data"]["attemptId"] == 14 - assert mock_hub.kwargs["data"]["name"] == "run-job" - assert "reason" not in mock_hub.kwargs["data"] + assert mock_hub.kwargs["level"] == "info" + assert "sample-stage-id-submit" in mock_hub.kwargs["message"] + assert mock_hub.kwargs["data"]["attemptId"] == 14 + assert mock_hub.kwargs["data"]["name"] == "run-job" + assert "reason" not in mock_hub.kwargs["data"] def test_sentry_listener_on_stage_completed_failure( - sentry_listener, mock_add_breadcrumb, get_mock_stage_completed + sentry_listener, get_mock_stage_completed ): listener = sentry_listener - - mock_stage_completed = get_mock_stage_completed(failure_reason=True) - listener.onStageCompleted(mock_stage_completed) - - mock_add_breadcrumb.assert_called_once() - mock_hub = mock_add_breadcrumb.call_args - - assert mock_hub.kwargs["level"] == "warning" - assert "sample-stage-id-submit" in mock_hub.kwargs["message"] - assert mock_hub.kwargs["data"]["attemptId"] == 14 - assert mock_hub.kwargs["data"]["name"] == "run-job" - assert mock_hub.kwargs["data"]["reason"] == "failure-reason" + with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb: + mock_stage_completed = get_mock_stage_completed(failure_reason=True) + listener.onStageCompleted(mock_stage_completed) + + mock_add_breadcrumb.assert_called_once() + mock_hub = mock_add_breadcrumb.call_args + + assert mock_hub.kwargs["level"] == "warning" + assert "sample-stage-id-submit" in mock_hub.kwargs["message"] + assert mock_hub.kwargs["data"]["attemptId"] == 14 + assert mock_hub.kwargs["data"]["name"] == "run-job" + assert mock_hub.kwargs["data"]["reason"] == "failure-reason" ################ From 8ced6609e6fcc95855f43cf9fc1d94b59836b57f Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 20 Dec 2024 10:15:48 +0100 Subject: [PATCH 18/19] Rename scripts (#3885) --- .github/workflows/ci.yml | 4 ++-- .github/workflows/test-integrations-ai.yml | 6 ++++-- .github/workflows/test-integrations-aws.yml | 6 ++++-- .github/workflows/test-integrations-cloud.yml | 6 ++++-- .github/workflows/test-integrations-common.yml | 6 ++++-- .github/workflows/test-integrations-dbs.yml | 6 ++++-- .github/workflows/test-integrations-graphql.yml | 6 ++++-- .github/workflows/test-integrations-misc.yml | 6 ++++-- .github/workflows/test-integrations-network.yml | 6 ++++-- .github/workflows/test-integrations-tasks.yml | 6 ++++-- .github/workflows/test-integrations-web-1.yml | 6 ++++-- .github/workflows/test-integrations-web-2.yml | 6 ++++-- ...er-versions.sh => aws-delete-lambda-layer-versions.sh} | 0 scripts/split_tox_gh_actions/__init__.py | 0 scripts/split_tox_gh_actions/requirements.txt | 1 + .../split_tox_gh_actions.py} | 8 ++++---- .../templates/base.jinja | 6 ++++-- .../templates/check_permissions.jinja | 0 .../templates/check_required.jinja | 0 .../templates/test_group.jinja | 0 20 files changed, 55 insertions(+), 30 deletions(-) rename scripts/{aws-delete-lamba-layer-versions.sh => aws-delete-lambda-layer-versions.sh} (100%) create mode 100644 scripts/split_tox_gh_actions/__init__.py create mode 100644 scripts/split_tox_gh_actions/requirements.txt rename scripts/{split-tox-gh-actions/split-tox-gh-actions.py => split_tox_gh_actions/split_tox_gh_actions.py} (96%) rename scripts/{split-tox-gh-actions => split_tox_gh_actions}/templates/base.jinja (87%) rename scripts/{split-tox-gh-actions => split_tox_gh_actions}/templates/check_permissions.jinja (100%) rename scripts/{split-tox-gh-actions => split_tox_gh_actions}/templates/check_required.jinja (100%) rename scripts/{split-tox-gh-actions => split_tox_gh_actions}/templates/test_group.jinja (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed035b4ab0..7ef6604e39 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,8 +45,8 @@ jobs: python-version: 3.12 - run: | - pip install jinja2 - python scripts/split-tox-gh-actions/split-tox-gh-actions.py --fail-on-changes + pip install -r scripts/split_tox_gh_actions/requirements.txt + python scripts/split_tox_gh_actions/split_tox_gh_actions.py --fail-on-changes build_lambda_layer: name: Build Package diff --git a/.github/workflows/test-integrations-ai.yml b/.github/workflows/test-integrations-ai.yml index 8be64736c1..c5e1f6b87e 100644 --- a/.github/workflows/test-integrations-ai.yml +++ b/.github/workflows/test-integrations-ai.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test AI on: push: diff --git a/.github/workflows/test-integrations-aws.yml b/.github/workflows/test-integrations-aws.yml index 6eed3a3ab1..54610f1abd 100644 --- a/.github/workflows/test-integrations-aws.yml +++ b/.github/workflows/test-integrations-aws.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test AWS on: push: diff --git a/.github/workflows/test-integrations-cloud.yml b/.github/workflows/test-integrations-cloud.yml index 677385e405..f72fec9f9f 100644 --- a/.github/workflows/test-integrations-cloud.yml +++ b/.github/workflows/test-integrations-cloud.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test Cloud on: push: diff --git a/.github/workflows/test-integrations-common.yml b/.github/workflows/test-integrations-common.yml index 9c476553f5..0837c60c30 100644 --- a/.github/workflows/test-integrations-common.yml +++ b/.github/workflows/test-integrations-common.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test Common on: push: diff --git a/.github/workflows/test-integrations-dbs.yml b/.github/workflows/test-integrations-dbs.yml index cbaa2c32d2..a4aefa6a51 100644 --- a/.github/workflows/test-integrations-dbs.yml +++ b/.github/workflows/test-integrations-dbs.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test DBs on: push: diff --git a/.github/workflows/test-integrations-graphql.yml b/.github/workflows/test-integrations-graphql.yml index d582717fff..ab7e81dcd6 100644 --- a/.github/workflows/test-integrations-graphql.yml +++ b/.github/workflows/test-integrations-graphql.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test GraphQL on: push: diff --git a/.github/workflows/test-integrations-misc.yml b/.github/workflows/test-integrations-misc.yml index 00b1286362..1a4e910383 100644 --- a/.github/workflows/test-integrations-misc.yml +++ b/.github/workflows/test-integrations-misc.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test Misc on: push: diff --git a/.github/workflows/test-integrations-network.yml b/.github/workflows/test-integrations-network.yml index 8f6bd9fd61..f41fd86b29 100644 --- a/.github/workflows/test-integrations-network.yml +++ b/.github/workflows/test-integrations-network.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test Network on: push: diff --git a/.github/workflows/test-integrations-tasks.yml b/.github/workflows/test-integrations-tasks.yml index 74c868d9b9..9910b75568 100644 --- a/.github/workflows/test-integrations-tasks.yml +++ b/.github/workflows/test-integrations-tasks.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test Tasks on: push: diff --git a/.github/workflows/test-integrations-web-1.yml b/.github/workflows/test-integrations-web-1.yml index 5be067a36b..fb7a9247d5 100644 --- a/.github/workflows/test-integrations-web-1.yml +++ b/.github/workflows/test-integrations-web-1.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test Web 1 on: push: diff --git a/.github/workflows/test-integrations-web-2.yml b/.github/workflows/test-integrations-web-2.yml index 7ce0399a13..1910d5999e 100644 --- a/.github/workflows/test-integrations-web-2.yml +++ b/.github/workflows/test-integrations-web-2.yml @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja name: Test Web 2 on: push: diff --git a/scripts/aws-delete-lamba-layer-versions.sh b/scripts/aws-delete-lambda-layer-versions.sh similarity index 100% rename from scripts/aws-delete-lamba-layer-versions.sh rename to scripts/aws-delete-lambda-layer-versions.sh diff --git a/scripts/split_tox_gh_actions/__init__.py b/scripts/split_tox_gh_actions/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/scripts/split_tox_gh_actions/requirements.txt b/scripts/split_tox_gh_actions/requirements.txt new file mode 100644 index 0000000000..7f7afbf3bf --- /dev/null +++ b/scripts/split_tox_gh_actions/requirements.txt @@ -0,0 +1 @@ +jinja2 diff --git a/scripts/split-tox-gh-actions/split-tox-gh-actions.py b/scripts/split_tox_gh_actions/split_tox_gh_actions.py similarity index 96% rename from scripts/split-tox-gh-actions/split-tox-gh-actions.py rename to scripts/split_tox_gh_actions/split_tox_gh_actions.py index 26d13390c2..1b53093c5e 100755 --- a/scripts/split-tox-gh-actions/split-tox-gh-actions.py +++ b/scripts/split_tox_gh_actions/split_tox_gh_actions.py @@ -8,7 +8,7 @@ Whenever tox.ini is changed, this script needs to be run. Usage: - python split-tox-gh-actions.py [--fail-on-changes] + python split_tox_gh_actions.py [--fail-on-changes] If the parameter `--fail-on-changes` is set, the script will raise a RuntimeError in case the yaml files have been changed by the scripts execution. This is used in CI to check if the yaml files @@ -158,7 +158,7 @@ def main(fail_on_changes): if missing_frameworks: raise RuntimeError( "Please add the following frameworks to the corresponding group " - "in `GROUPS` in `scripts/split-tox-gh-actions/split-tox-gh-actions.py: " + "in `GROUPS` in `scripts/split_tox_gh_actions/split_tox_gh_actions.py: " + ", ".join(missing_frameworks) ) @@ -176,9 +176,9 @@ def main(fail_on_changes): if old_hash != new_hash: raise RuntimeError( "The yaml configuration files have changed. This means that either `tox.ini` " - "or one of the constants in `split-tox-gh-actions.py` has changed " + "or one of the constants in `split_tox_gh_actions.py` has changed " "but the changes have not been propagated to the GitHub actions config files. " - "Please run `python scripts/split-tox-gh-actions/split-tox-gh-actions.py` " + "Please run `python scripts/split_tox_gh_actions/split_tox_gh_actions.py` " "locally and commit the changes of the yaml configuration files to continue. " ) diff --git a/scripts/split-tox-gh-actions/templates/base.jinja b/scripts/split_tox_gh_actions/templates/base.jinja similarity index 87% rename from scripts/split-tox-gh-actions/templates/base.jinja rename to scripts/split_tox_gh_actions/templates/base.jinja index 23f051de42..16dbc04a76 100644 --- a/scripts/split-tox-gh-actions/templates/base.jinja +++ b/scripts/split_tox_gh_actions/templates/base.jinja @@ -1,5 +1,7 @@ -# Do not edit this file. This file is generated automatically by executing -# python scripts/split-tox-gh-actions/split-tox-gh-actions.py +# Do not edit this YAML file. This file is generated automatically by executing +# python scripts/split_tox_gh_actions/split_tox_gh_actions.py +# The template responsible for it is in +# scripts/split_tox_gh_actions/templates/base.jinja {% with lowercase_group=group | replace(" ", "_") | lower %} name: Test {{ group }} diff --git a/scripts/split-tox-gh-actions/templates/check_permissions.jinja b/scripts/split_tox_gh_actions/templates/check_permissions.jinja similarity index 100% rename from scripts/split-tox-gh-actions/templates/check_permissions.jinja rename to scripts/split_tox_gh_actions/templates/check_permissions.jinja diff --git a/scripts/split-tox-gh-actions/templates/check_required.jinja b/scripts/split_tox_gh_actions/templates/check_required.jinja similarity index 100% rename from scripts/split-tox-gh-actions/templates/check_required.jinja rename to scripts/split_tox_gh_actions/templates/check_required.jinja diff --git a/scripts/split-tox-gh-actions/templates/test_group.jinja b/scripts/split_tox_gh_actions/templates/test_group.jinja similarity index 100% rename from scripts/split-tox-gh-actions/templates/test_group.jinja rename to scripts/split_tox_gh_actions/templates/test_group.jinja From f6281f557fe62c847a0aca95eb666129e893cf32 Mon Sep 17 00:00:00 2001 From: ffelixg <142172984+ffelixg@users.noreply.github.com> Date: Fri, 20 Dec 2024 12:34:12 +0100 Subject: [PATCH 19/19] Fix lru cache copying (#3883) A simpler and better LRU Cache implementation that prevents data leaking between copied caches. Fixes #3852 --------- Co-authored-by: Anton Pirker --- sentry_sdk/_lru_cache.py | 195 +++++++-------------------------------- tests/test_lru_cache.py | 37 +++++++- tests/test_scope.py | 22 +++++ 3 files changed, 93 insertions(+), 161 deletions(-) diff --git a/sentry_sdk/_lru_cache.py b/sentry_sdk/_lru_cache.py index 825c773529..09eae27df2 100644 --- a/sentry_sdk/_lru_cache.py +++ b/sentry_sdk/_lru_cache.py @@ -1,181 +1,56 @@ -""" -A fork of Python 3.6's stdlib lru_cache (found in Python's 'cpython/Lib/functools.py') -adapted into a data structure for single threaded uses. +from typing import TYPE_CHECKING -https://github.com/python/cpython/blob/v3.6.12/Lib/functools.py +if TYPE_CHECKING: + from typing import Any -Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, -2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Python Software Foundation; - -All Rights Reserved - - -PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 --------------------------------------------- - -1. This LICENSE AGREEMENT is between the Python Software Foundation -("PSF"), and the Individual or Organization ("Licensee") accessing and -otherwise using this software ("Python") in source or binary form and -its associated documentation. - -2. Subject to the terms and conditions of this License Agreement, PSF hereby -grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, -analyze, test, perform and/or display publicly, prepare derivative works, -distribute, and otherwise use Python alone or in any derivative version, -provided, however, that PSF's License Agreement and PSF's notice of copyright, -i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, -2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Python Software Foundation; -All Rights Reserved" are retained in Python alone or in any derivative version -prepared by Licensee. - -3. In the event Licensee prepares a derivative work that is based on -or incorporates Python or any part thereof, and wants to make -the derivative work available to others as provided herein, then -Licensee hereby agrees to include in any such work a brief summary of -the changes made to Python. - -4. PSF is making Python available to Licensee on an "AS IS" -basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR -IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND -DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS -FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT -INFRINGE ANY THIRD PARTY RIGHTS. - -5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON -FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS -A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, -OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. - -6. This License Agreement will automatically terminate upon a material -breach of its terms and conditions. - -7. Nothing in this License Agreement shall be deemed to create any -relationship of agency, partnership, or joint venture between PSF and -Licensee. This License Agreement does not grant permission to use PSF -trademarks or trade name in a trademark sense to endorse or promote -products or services of Licensee, or any third party. - -8. By copying, installing or otherwise using Python, Licensee -agrees to be bound by the terms and conditions of this License -Agreement. - -""" - -from copy import copy, deepcopy - -SENTINEL = object() - - -# aliases to the entries in a node -PREV = 0 -NEXT = 1 -KEY = 2 -VALUE = 3 +_SENTINEL = object() class LRUCache: def __init__(self, max_size): - assert max_size > 0 - + # type: (int) -> None + if max_size <= 0: + raise AssertionError(f"invalid max_size: {max_size}") self.max_size = max_size - self.full = False - - self.cache = {} - - # root of the circularly linked list to keep track of - # the least recently used key - self.root = [] # type: ignore - # the node looks like [PREV, NEXT, KEY, VALUE] - self.root[:] = [self.root, self.root, None, None] - + self._data = {} # type: dict[Any, Any] self.hits = self.misses = 0 + self.full = False def __copy__(self): - cache = LRUCache(self.max_size) - cache.full = self.full - cache.cache = copy(self.cache) - cache.root = deepcopy(self.root) - return cache + # type: () -> LRUCache + new = LRUCache(max_size=self.max_size) + new.hits = self.hits + new.misses = self.misses + new.full = self.full + new._data = self._data.copy() + return new def set(self, key, value): - link = self.cache.get(key, SENTINEL) - - if link is not SENTINEL: - # have to move the node to the front of the linked list - link_prev, link_next, _key, _value = link - - # first remove the node from the lsnked list - link_prev[NEXT] = link_next - link_next[PREV] = link_prev - - # insert the node between the root and the last - last = self.root[PREV] - last[NEXT] = self.root[PREV] = link - link[PREV] = last - link[NEXT] = self.root - - # update the value - link[VALUE] = value - + # type: (Any, Any) -> None + current = self._data.pop(key, _SENTINEL) + if current is not _SENTINEL: + self._data[key] = value elif self.full: - # reuse the root node, so update its key/value - old_root = self.root - old_root[KEY] = key - old_root[VALUE] = value - - self.root = old_root[NEXT] - old_key = self.root[KEY] - - self.root[KEY] = self.root[VALUE] = None - - del self.cache[old_key] - - self.cache[key] = old_root - + self._data.pop(next(iter(self._data))) + self._data[key] = value else: - # insert new node after last - last = self.root[PREV] - link = [last, self.root, key, value] - last[NEXT] = self.root[PREV] = self.cache[key] = link - self.full = len(self.cache) >= self.max_size + self._data[key] = value + self.full = len(self._data) >= self.max_size def get(self, key, default=None): - link = self.cache.get(key, SENTINEL) - - if link is SENTINEL: + # type: (Any, Any) -> Any + try: + ret = self._data.pop(key) + except KeyError: self.misses += 1 - return default - - # have to move the node to the front of the linked list - link_prev, link_next, _key, _value = link - - # first remove the node from the lsnked list - link_prev[NEXT] = link_next - link_next[PREV] = link_prev - - # insert the node between the root and the last - last = self.root[PREV] - last[NEXT] = self.root[PREV] = link - link[PREV] = last - link[NEXT] = self.root - - self.hits += 1 + ret = default + else: + self.hits += 1 + self._data[key] = ret - return link[VALUE] + return ret def get_all(self): - nodes = [] - node = self.root[NEXT] - - # To ensure the loop always terminates we iterate to the maximum - # size of the LRU cache. - for _ in range(self.max_size): - # The cache may not be full. We exit early if we've wrapped - # around to the head. - if node is self.root: - break - nodes.append((node[KEY], node[VALUE])) - node = node[NEXT] - - return nodes + # type: () -> list[tuple[Any, Any]] + return list(self._data.items()) diff --git a/tests/test_lru_cache.py b/tests/test_lru_cache.py index cab9bbc7eb..1a54ed83d3 100644 --- a/tests/test_lru_cache.py +++ b/tests/test_lru_cache.py @@ -1,5 +1,5 @@ import pytest -from copy import copy +from copy import copy, deepcopy from sentry_sdk._lru_cache import LRUCache @@ -76,3 +76,38 @@ def test_cache_copy(): cache.get(1) assert copied.get_all() == [(1, 1), (2, 2), (3, 3)] assert cache.get_all() == [(2, 2), (3, 3), (1, 1)] + + +def test_cache_deepcopy(): + cache = LRUCache(3) + cache.set(0, 0) + cache.set(1, 1) + + copied = deepcopy(cache) + cache.set(2, 2) + cache.set(3, 3) + assert copied.get_all() == [(0, 0), (1, 1)] + assert cache.get_all() == [(1, 1), (2, 2), (3, 3)] + + copied = deepcopy(cache) + cache.get(1) + assert copied.get_all() == [(1, 1), (2, 2), (3, 3)] + assert cache.get_all() == [(2, 2), (3, 3), (1, 1)] + + +def test_cache_pollution(): + cache1 = LRUCache(max_size=2) + cache1.set(1, True) + cache2 = copy(cache1) + cache2.set(1, False) + assert cache1.get(1) is True + assert cache2.get(1) is False + + +def test_cache_pollution_deepcopy(): + cache1 = LRUCache(max_size=2) + cache1.set(1, True) + cache2 = deepcopy(cache1) + cache2.set(1, False) + assert cache1.get(1) is True + assert cache2.get(1) is False diff --git a/tests/test_scope.py b/tests/test_scope.py index a03eb07a99..9b16dc4344 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -43,6 +43,28 @@ def test_all_slots_copied(): assert getattr(scope_copy, attr) == getattr(scope, attr) +def test_scope_flags_copy(): + # Assert forking creates a deepcopy of the flag buffer. The new + # scope is free to mutate without consequence to the old scope. The + # old scope is free to mutate without consequence to the new scope. + old_scope = Scope() + old_scope.flags.set("a", True) + + new_scope = old_scope.fork() + new_scope.flags.set("a", False) + old_scope.flags.set("b", True) + new_scope.flags.set("c", True) + + assert old_scope.flags.get() == [ + {"flag": "a", "result": True}, + {"flag": "b", "result": True}, + ] + assert new_scope.flags.get() == [ + {"flag": "a", "result": False}, + {"flag": "c", "result": True}, + ] + + def test_merging(sentry_init, capture_events): sentry_init()