From 0e21fa2f0fbec007563f22d10bf6ec4a128faa25 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 13 Jun 2025 12:45:15 +0200 Subject: [PATCH 01/14] tests: Regenerate tox (#4457) (Yeah I'll automate this at some point) --- tox.ini | 78 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/tox.ini b/tox.ini index 2fe4977a1c..32e16dac3d 100644 --- a/tox.ini +++ b/tox.ini @@ -10,7 +10,7 @@ # The file (and all resulting CI YAMLs) then need to be regenerated via # "scripts/generate-test-files.sh". # -# Last generated: 2025-06-03T13:53:53.259798+00:00 +# Last generated: 2025-06-11T12:20:52.494394+00:00 [tox] requires = @@ -136,9 +136,9 @@ envlist = # ~~~ AI ~~~ {py3.8,py3.11,py3.12}-anthropic-v0.16.0 - {py3.8,py3.11,py3.12}-anthropic-v0.28.1 - {py3.8,py3.11,py3.12}-anthropic-v0.40.0 - {py3.8,py3.11,py3.12}-anthropic-v0.52.2 + {py3.8,py3.11,py3.12}-anthropic-v0.29.2 + {py3.8,py3.11,py3.12}-anthropic-v0.42.0 + {py3.8,py3.11,py3.12}-anthropic-v0.54.0 {py3.9,py3.10,py3.11}-cohere-v5.4.0 {py3.9,py3.11,py3.12}-cohere-v5.8.1 @@ -148,7 +148,7 @@ envlist = {py3.8,py3.10,py3.11}-huggingface_hub-v0.22.2 {py3.8,py3.10,py3.11}-huggingface_hub-v0.25.2 {py3.8,py3.12,py3.13}-huggingface_hub-v0.28.1 - {py3.8,py3.12,py3.13}-huggingface_hub-v0.32.4 + {py3.8,py3.12,py3.13}-huggingface_hub-v0.32.6 # ~~~ DBs ~~~ @@ -180,7 +180,7 @@ envlist = {py3.7,py3.12,py3.13}-statsig-v0.55.3 {py3.7,py3.12,py3.13}-statsig-v0.56.0 {py3.7,py3.12,py3.13}-statsig-v0.57.3 - {py3.7,py3.12,py3.13}-statsig-v0.58.0 + {py3.7,py3.12,py3.13}-statsig-v0.58.1 {py3.8,py3.12,py3.13}-unleash-v6.0.1 {py3.8,py3.12,py3.13}-unleash-v6.1.0 @@ -201,17 +201,16 @@ envlist = {py3.8,py3.12,py3.13}-graphene-v3.4.3 {py3.8,py3.10,py3.11}-strawberry-v0.209.8 - {py3.8,py3.11,py3.12}-strawberry-v0.229.2 - {py3.8,py3.12,py3.13}-strawberry-v0.249.0 - {py3.9,py3.12,py3.13}-strawberry-v0.270.5 + {py3.8,py3.11,py3.12}-strawberry-v0.230.0 + {py3.8,py3.12,py3.13}-strawberry-v0.251.0 + {py3.9,py3.12,py3.13}-strawberry-v0.273.0 # ~~~ Network ~~~ {py3.7,py3.8}-grpc-v1.32.0 {py3.7,py3.9,py3.10}-grpc-v1.46.5 {py3.7,py3.11,py3.12}-grpc-v1.60.2 - {py3.9,py3.12,py3.13}-grpc-v1.72.1 - {py3.9,py3.12,py3.13}-grpc-v1.73.0rc1 + {py3.9,py3.12,py3.13}-grpc-v1.73.0 # ~~~ Tasks ~~~ @@ -238,9 +237,9 @@ envlist = {py3.6,py3.7}-django-v1.11.29 {py3.6,py3.8,py3.9}-django-v2.2.28 {py3.6,py3.9,py3.10}-django-v3.2.25 - {py3.8,py3.11,py3.12}-django-v4.2.21 + {py3.8,py3.11,py3.12}-django-v4.2.23 {py3.10,py3.11,py3.12}-django-v5.0.14 - {py3.10,py3.12,py3.13}-django-v5.2.1 + {py3.10,py3.12,py3.13}-django-v5.2.3 {py3.6,py3.7,py3.8}-flask-v1.1.4 {py3.8,py3.12,py3.13}-flask-v2.3.3 @@ -262,7 +261,7 @@ envlist = {py3.7}-aiohttp-v3.4.4 {py3.7,py3.8,py3.9}-aiohttp-v3.7.4 {py3.8,py3.12,py3.13}-aiohttp-v3.10.11 - {py3.9,py3.12,py3.13}-aiohttp-v3.12.7 + {py3.9,py3.12,py3.13}-aiohttp-v3.12.12 {py3.6,py3.7}-bottle-v0.12.25 {py3.8,py3.12,py3.13}-bottle-v0.13.3 @@ -299,8 +298,8 @@ envlist = {py3.6}-trytond-v4.8.18 {py3.6,py3.7,py3.8}-trytond-v5.8.16 {py3.8,py3.10,py3.11}-trytond-v6.8.17 - {py3.8,py3.11,py3.12}-trytond-v7.0.31 - {py3.9,py3.12,py3.13}-trytond-v7.6.1 + {py3.8,py3.11,py3.12}-trytond-v7.0.32 + {py3.9,py3.12,py3.13}-trytond-v7.6.2 {py3.7,py3.12,py3.13}-typer-v0.15.4 {py3.7,py3.12,py3.13}-typer-v0.16.0 @@ -502,13 +501,13 @@ deps = # ~~~ AI ~~~ anthropic-v0.16.0: anthropic==0.16.0 - anthropic-v0.28.1: anthropic==0.28.1 - anthropic-v0.40.0: anthropic==0.40.0 - anthropic-v0.52.2: anthropic==0.52.2 + anthropic-v0.29.2: anthropic==0.29.2 + anthropic-v0.42.0: anthropic==0.42.0 + anthropic-v0.54.0: anthropic==0.54.0 anthropic: pytest-asyncio anthropic-v0.16.0: httpx<0.28.0 - anthropic-v0.28.1: httpx<0.28.0 - anthropic-v0.40.0: httpx<0.28.0 + anthropic-v0.29.2: httpx<0.28.0 + anthropic-v0.42.0: httpx<0.28.0 cohere-v5.4.0: cohere==5.4.0 cohere-v5.8.1: cohere==5.8.1 @@ -518,7 +517,7 @@ deps = huggingface_hub-v0.22.2: huggingface_hub==0.22.2 huggingface_hub-v0.25.2: huggingface_hub==0.25.2 huggingface_hub-v0.28.1: huggingface_hub==0.28.1 - huggingface_hub-v0.32.4: huggingface_hub==0.32.4 + huggingface_hub-v0.32.6: huggingface_hub==0.32.6 # ~~~ DBs ~~~ @@ -551,7 +550,7 @@ deps = statsig-v0.55.3: statsig==0.55.3 statsig-v0.56.0: statsig==0.56.0 statsig-v0.57.3: statsig==0.57.3 - statsig-v0.58.0: statsig==0.58.0 + statsig-v0.58.1: statsig==0.58.1 statsig: typing_extensions unleash-v6.0.1: UnleashClient==6.0.1 @@ -581,21 +580,20 @@ deps = py3.6-graphene: aiocontextvars strawberry-v0.209.8: strawberry-graphql[fastapi,flask]==0.209.8 - strawberry-v0.229.2: strawberry-graphql[fastapi,flask]==0.229.2 - strawberry-v0.249.0: strawberry-graphql[fastapi,flask]==0.249.0 - strawberry-v0.270.5: strawberry-graphql[fastapi,flask]==0.270.5 + strawberry-v0.230.0: strawberry-graphql[fastapi,flask]==0.230.0 + strawberry-v0.251.0: strawberry-graphql[fastapi,flask]==0.251.0 + strawberry-v0.273.0: strawberry-graphql[fastapi,flask]==0.273.0 strawberry: httpx strawberry-v0.209.8: pydantic<2.11 - strawberry-v0.229.2: pydantic<2.11 - strawberry-v0.249.0: pydantic<2.11 + strawberry-v0.230.0: pydantic<2.11 + strawberry-v0.251.0: pydantic<2.11 # ~~~ Network ~~~ grpc-v1.32.0: grpcio==1.32.0 grpc-v1.46.5: grpcio==1.46.5 grpc-v1.60.2: grpcio==1.60.2 - grpc-v1.72.1: grpcio==1.72.1 - grpc-v1.73.0rc1: grpcio==1.73.0rc1 + grpc-v1.73.0: grpcio==1.73.0 grpc: protobuf grpc: mypy-protobuf grpc: types-protobuf @@ -629,23 +627,23 @@ deps = django-v1.11.29: django==1.11.29 django-v2.2.28: django==2.2.28 django-v3.2.25: django==3.2.25 - django-v4.2.21: django==4.2.21 + django-v4.2.23: django==4.2.23 django-v5.0.14: django==5.0.14 - django-v5.2.1: django==5.2.1 + django-v5.2.3: django==5.2.3 django: psycopg2-binary django: djangorestframework django: pytest-django django: Werkzeug django-v2.2.28: channels[daphne] django-v3.2.25: channels[daphne] - django-v4.2.21: channels[daphne] + django-v4.2.23: channels[daphne] django-v5.0.14: channels[daphne] - django-v5.2.1: channels[daphne] + django-v5.2.3: channels[daphne] django-v2.2.28: six django-v3.2.25: pytest-asyncio - django-v4.2.21: pytest-asyncio + django-v4.2.23: pytest-asyncio django-v5.0.14: pytest-asyncio - django-v5.2.1: pytest-asyncio + django-v5.2.3: pytest-asyncio django-v1.11.29: djangorestframework>=3.0,<4.0 django-v1.11.29: Werkzeug<2.1.0 django-v2.2.28: djangorestframework>=3.0,<4.0 @@ -698,10 +696,10 @@ deps = aiohttp-v3.4.4: aiohttp==3.4.4 aiohttp-v3.7.4: aiohttp==3.7.4 aiohttp-v3.10.11: aiohttp==3.10.11 - aiohttp-v3.12.7: aiohttp==3.12.7 + aiohttp-v3.12.12: aiohttp==3.12.12 aiohttp: pytest-aiohttp aiohttp-v3.10.11: pytest-asyncio - aiohttp-v3.12.7: pytest-asyncio + aiohttp-v3.12.12: pytest-asyncio bottle-v0.12.25: bottle==0.12.25 bottle-v0.13.3: bottle==0.13.3 @@ -756,8 +754,8 @@ deps = trytond-v4.8.18: trytond==4.8.18 trytond-v5.8.16: trytond==5.8.16 trytond-v6.8.17: trytond==6.8.17 - trytond-v7.0.31: trytond==7.0.31 - trytond-v7.6.1: trytond==7.6.1 + trytond-v7.0.32: trytond==7.0.32 + trytond-v7.6.2: trytond==7.6.2 trytond: werkzeug trytond-v4.6.22: werkzeug<1.0 trytond-v4.8.18: werkzeug<1.0 From f71d223c6cecd4e99565c5e8e919ce9cf81707ff Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 16 Jun 2025 10:17:57 -0400 Subject: [PATCH 02/14] feat(logs): Add support for dict args (#4478) resolves https://github.com/getsentry/sentry-python/issues/4477 This PR adds support for dict log arguments and adds (cursor generated) tests accordingly. --- sentry_sdk/integrations/logging.py | 7 ++ tests/integrations/logging/test_logging.py | 79 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index 62b1e09d64..a50512f622 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -367,6 +367,13 @@ def _capture_log_from_record(self, client, record): if isinstance(arg, (str, float, int, bool)) else safe_repr(arg) ) + elif isinstance(record.args, dict): + for key, value in record.args.items(): + attrs[f"sentry.message.parameter.{key}"] = ( + value + if isinstance(value, (str, float, int, bool)) + else safe_repr(value) + ) if record.lineno: attrs["code.line.number"] = record.lineno if record.pathname: diff --git a/tests/integrations/logging/test_logging.py b/tests/integrations/logging/test_logging.py index 237373fc91..6ef4ae371b 100644 --- a/tests/integrations/logging/test_logging.py +++ b/tests/integrations/logging/test_logging.py @@ -492,3 +492,82 @@ def test_logger_with_all_attributes(sentry_init, capture_envelopes): "sentry.severity_number": 13, "sentry.severity_text": "warn", } + + +def test_sentry_logs_named_parameters(sentry_init, capture_envelopes): + """ + The python logger module should capture named parameters from dictionary arguments in Sentry logs. + """ + sentry_init(_experiments={"enable_logs": True}) + envelopes = capture_envelopes() + + python_logger = logging.Logger("test-logger") + python_logger.info( + "%(source)s call completed, %(input_tk)i input tk, %(output_tk)i output tk (model %(model)s, cost $%(cost).4f)", + { + "source": "test_source", + "input_tk": 100, + "output_tk": 50, + "model": "gpt-4", + "cost": 0.0234, + }, + ) + + get_client().flush() + logs = envelopes_to_logs(envelopes) + + assert len(logs) == 1 + attrs = logs[0]["attributes"] + + # Check that the template is captured + assert ( + attrs["sentry.message.template"] + == "%(source)s call completed, %(input_tk)i input tk, %(output_tk)i output tk (model %(model)s, cost $%(cost).4f)" + ) + + # Check that dictionary arguments are captured as named parameters + assert attrs["sentry.message.parameter.source"] == "test_source" + assert attrs["sentry.message.parameter.input_tk"] == 100 + assert attrs["sentry.message.parameter.output_tk"] == 50 + assert attrs["sentry.message.parameter.model"] == "gpt-4" + assert attrs["sentry.message.parameter.cost"] == 0.0234 + + # Check other standard attributes + assert attrs["logger.name"] == "test-logger" + assert attrs["sentry.origin"] == "auto.logger.log" + assert logs[0]["severity_number"] == 9 # info level + assert logs[0]["severity_text"] == "info" + + +def test_sentry_logs_named_parameters_complex_values(sentry_init, capture_envelopes): + """ + The python logger module should handle complex values in named parameters using safe_repr. + """ + sentry_init(_experiments={"enable_logs": True}) + envelopes = capture_envelopes() + + python_logger = logging.Logger("test-logger") + complex_object = {"nested": {"data": [1, 2, 3]}, "tuple": (4, 5, 6)} + python_logger.warning( + "Processing %(simple)s with %(complex)s data", + { + "simple": "simple_value", + "complex": complex_object, + }, + ) + + get_client().flush() + logs = envelopes_to_logs(envelopes) + + assert len(logs) == 1 + attrs = logs[0]["attributes"] + + # Check that simple values are kept as-is + assert attrs["sentry.message.parameter.simple"] == "simple_value" + + # Check that complex values are converted using safe_repr + assert "sentry.message.parameter.complex" in attrs + complex_param = attrs["sentry.message.parameter.complex"] + assert isinstance(complex_param, str) + assert "nested" in complex_param + assert "data" in complex_param From fedcb07dcf60b318ec62e5cf833b3c16b70ba707 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 17 Jun 2025 10:40:33 +0200 Subject: [PATCH 03/14] tests: Upper bound on fakeredis on old Python versions (#4482) --- scripts/populate_tox/tox.jinja | 2 +- tox.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index 3f3691147e..45f56e2f1f 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -295,7 +295,7 @@ deps = # Redis redis: fakeredis!=1.7.4 redis: pytest<8.0.0 - {py3.6,py3.7}-redis: fakeredis!=2.26.0 # https://github.com/cunla/fakeredis-py/issues/341 + {py3.6,py3.7,py3.8}-redis: fakeredis<2.26.0 {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-redis: pytest-asyncio redis-v3: redis~=3.0 redis-v4: redis~=4.0 diff --git a/tox.ini b/tox.ini index 32e16dac3d..3ba62e1a5c 100644 --- a/tox.ini +++ b/tox.ini @@ -456,7 +456,7 @@ deps = # Redis redis: fakeredis!=1.7.4 redis: pytest<8.0.0 - {py3.6,py3.7}-redis: fakeredis!=2.26.0 # https://github.com/cunla/fakeredis-py/issues/341 + {py3.6,py3.7,py3.8}-redis: fakeredis<2.26.0 {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-redis: pytest-asyncio redis-v3: redis~=3.0 redis-v4: redis~=4.0 From 449b2fa49417d6bf87ee2d245c2a55cdd26d9fe1 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 17 Jun 2025 12:26:29 +0200 Subject: [PATCH 04/14] fix(scope): Handle token reset `LookupError`s gracefully (#4481) We're surfacing internal SDK errors to users in https://github.com/getsentry/sentry-python/issues/4410. --- sentry_sdk/scope.py | 36 +++++++++++++++++++------ tests/test_scope.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index f346569255..73bf43573e 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -1673,8 +1673,11 @@ def new_scope(): yield new_scope finally: - # restore original scope - _current_scope.reset(token) + try: + # restore original scope + _current_scope.reset(token) + except LookupError: + capture_internal_exception(sys.exc_info()) @contextmanager @@ -1708,8 +1711,11 @@ def use_scope(scope): yield scope finally: - # restore original scope - _current_scope.reset(token) + try: + # restore original scope + _current_scope.reset(token) + except LookupError: + capture_internal_exception(sys.exc_info()) @contextmanager @@ -1750,8 +1756,15 @@ def isolation_scope(): finally: # restore original scopes - _current_scope.reset(current_token) - _isolation_scope.reset(isolation_token) + try: + _current_scope.reset(current_token) + except LookupError: + capture_internal_exception(sys.exc_info()) + + try: + _isolation_scope.reset(isolation_token) + except LookupError: + capture_internal_exception(sys.exc_info()) @contextmanager @@ -1790,8 +1803,15 @@ def use_isolation_scope(isolation_scope): finally: # restore original scopes - _current_scope.reset(current_token) - _isolation_scope.reset(isolation_token) + try: + _current_scope.reset(current_token) + except LookupError: + capture_internal_exception(sys.exc_info()) + + try: + _isolation_scope.reset(isolation_token) + except LookupError: + capture_internal_exception(sys.exc_info()) def should_send_default_pii(): diff --git a/tests/test_scope.py b/tests/test_scope.py index 9b16dc4344..e645d84234 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -905,3 +905,67 @@ def test_last_event_id_cleared(sentry_init): Scope.get_isolation_scope().clear() assert Scope.last_event_id() is None, "last_event_id should be cleared" + + +@pytest.mark.tests_internal_exceptions +@pytest.mark.parametrize( + "scope_manager", + [ + new_scope, + use_scope, + ], +) +def test_handle_lookup_error_on_token_reset_current_scope(scope_manager): + with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture: + with mock.patch("sentry_sdk.scope._current_scope") as mock_token_var: + mock_token_var.reset.side_effect = LookupError() + + mock_token = mock.Mock() + mock_token_var.set.return_value = mock_token + + try: + if scope_manager == use_scope: + with scope_manager(Scope()): + pass + else: + with scope_manager(): + pass + + except Exception: + pytest.fail("Context manager should handle LookupError gracefully") + + mock_capture.assert_called_once() + mock_token_var.reset.assert_called_once_with(mock_token) + + +@pytest.mark.tests_internal_exceptions +@pytest.mark.parametrize( + "scope_manager", + [ + isolation_scope, + use_isolation_scope, + ], +) +def test_handle_lookup_error_on_token_reset_isolation_scope(scope_manager): + with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture: + with mock.patch("sentry_sdk.scope._current_scope") as mock_current_scope: + with mock.patch( + "sentry_sdk.scope._isolation_scope" + ) as mock_isolation_scope: + mock_isolation_scope.reset.side_effect = LookupError() + mock_current_token = mock.Mock() + mock_current_scope.set.return_value = mock_current_token + + try: + if scope_manager == use_isolation_scope: + with scope_manager(Scope()): + pass + else: + with scope_manager(): + pass + + except Exception: + pytest.fail("Context manager should handle LookupError gracefully") + + mock_capture.assert_called_once() + mock_current_scope.reset.assert_called_once_with(mock_current_token) From 6a58e5fb7cc8d6d794a70dc1f00761dce240e2b7 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 17 Jun 2025 15:31:53 +0200 Subject: [PATCH 05/14] tests: Regenerate tox (#4484) Regular tox update. This includes a fix for a new Bottle version which made one of our tests get stuck in a neverending `while` loop. --- tests/integrations/bottle/test_bottle.py | 57 ++++++++++-------------- tox.ini | 48 ++++++++++---------- 2 files changed, 47 insertions(+), 58 deletions(-) diff --git a/tests/integrations/bottle/test_bottle.py b/tests/integrations/bottle/test_bottle.py index 9cc436a229..363a9167e6 100644 --- a/tests/integrations/bottle/test_bottle.py +++ b/tests/integrations/bottle/test_bottle.py @@ -12,8 +12,6 @@ from werkzeug.test import Client from werkzeug.wrappers import Response -import sentry_sdk.integrations.bottle as bottle_sentry - @pytest.fixture(scope="function") def app(sentry_init): @@ -46,7 +44,7 @@ def inner(): def test_has_context(sentry_init, app, capture_events, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) events = capture_events() client = get_client() @@ -77,11 +75,7 @@ def test_transaction_style( capture_events, get_client, ): - sentry_init( - integrations=[ - bottle_sentry.BottleIntegration(transaction_style=transaction_style) - ] - ) + sentry_init(integrations=[BottleIntegration(transaction_style=transaction_style)]) events = capture_events() client = get_client() @@ -100,7 +94,7 @@ def test_transaction_style( def test_errors( sentry_init, capture_exceptions, capture_events, app, debug, catchall, get_client ): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) app.catchall = catchall set_debug(mode=debug) @@ -127,7 +121,7 @@ def index(): def test_large_json_request(sentry_init, capture_events, app, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) data = {"foo": {"bar": "a" * 2000}} @@ -157,7 +151,7 @@ def index(): @pytest.mark.parametrize("data", [{}, []], ids=["empty-dict", "empty-list"]) def test_empty_json_request(sentry_init, capture_events, app, data, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) @app.route("/", method="POST") def index(): @@ -180,7 +174,7 @@ def index(): def test_medium_formdata_request(sentry_init, capture_events, app, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) data = {"foo": "a" * 2000} @@ -209,9 +203,7 @@ def index(): def test_too_large_raw_request( sentry_init, input_char, capture_events, app, get_client ): - sentry_init( - integrations=[bottle_sentry.BottleIntegration()], max_request_body_size="small" - ) + sentry_init(integrations=[BottleIntegration()], max_request_body_size="small") data = input_char * 2000 @@ -239,9 +231,7 @@ def index(): def test_files_and_form(sentry_init, capture_events, app, get_client): - sentry_init( - integrations=[bottle_sentry.BottleIntegration()], max_request_body_size="always" - ) + sentry_init(integrations=[BottleIntegration()], max_request_body_size="always") data = {"foo": "a" * 2000, "file": (BytesIO(b"hello"), "hello.txt")} @@ -278,9 +268,7 @@ def index(): def test_json_not_truncated_if_max_request_body_size_is_always( sentry_init, capture_events, app, get_client ): - sentry_init( - integrations=[bottle_sentry.BottleIntegration()], max_request_body_size="always" - ) + sentry_init(integrations=[BottleIntegration()], max_request_body_size="always") data = { "key{}".format(i): "value{}".format(i) for i in range(MAX_DATABAG_BREADTH + 10) @@ -309,8 +297,8 @@ def index(): @pytest.mark.parametrize( "integrations", [ - [bottle_sentry.BottleIntegration()], - [bottle_sentry.BottleIntegration(), LoggingIntegration(event_level="ERROR")], + [BottleIntegration()], + [BottleIntegration(), LoggingIntegration(event_level="ERROR")], ], ) def test_errors_not_reported_twice( @@ -324,23 +312,24 @@ def test_errors_not_reported_twice( @app.route("/") def index(): - try: - 1 / 0 - except Exception as e: - logger.exception(e) - raise e + 1 / 0 events = capture_events() client = get_client() + with pytest.raises(ZeroDivisionError): - client.get("/") + try: + client.get("/") + except ZeroDivisionError as e: + logger.exception(e) + raise e assert len(events) == 1 def test_mount(app, capture_exceptions, capture_events, sentry_init, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) app.catchall = False @@ -367,7 +356,7 @@ def crashing_app(environ, start_response): def test_error_in_errorhandler(sentry_init, capture_events, app, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) set_debug(False) app.catchall = True @@ -397,7 +386,7 @@ def error_handler(err): def test_bad_request_not_captured(sentry_init, capture_events, app, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) events = capture_events() @app.route("/") @@ -412,7 +401,7 @@ def index(): def test_no_exception_on_redirect(sentry_init, capture_events, app, get_client): - sentry_init(integrations=[bottle_sentry.BottleIntegration()]) + sentry_init(integrations=[BottleIntegration()]) events = capture_events() @app.route("/") @@ -436,7 +425,7 @@ def test_span_origin( capture_events, ): sentry_init( - integrations=[bottle_sentry.BottleIntegration()], + integrations=[BottleIntegration()], traces_sample_rate=1.0, ) events = capture_events() diff --git a/tox.ini b/tox.ini index 3ba62e1a5c..c0c50c6029 100644 --- a/tox.ini +++ b/tox.ini @@ -10,7 +10,7 @@ # The file (and all resulting CI YAMLs) then need to be regenerated via # "scripts/generate-test-files.sh". # -# Last generated: 2025-06-11T12:20:52.494394+00:00 +# Last generated: 2025-06-17T08:49:27.078408+00:00 [tox] requires = @@ -146,9 +146,9 @@ envlist = {py3.9,py3.11,py3.12}-cohere-v5.15.0 {py3.8,py3.10,py3.11}-huggingface_hub-v0.22.2 - {py3.8,py3.10,py3.11}-huggingface_hub-v0.25.2 - {py3.8,py3.12,py3.13}-huggingface_hub-v0.28.1 - {py3.8,py3.12,py3.13}-huggingface_hub-v0.32.6 + {py3.8,py3.11,py3.12}-huggingface_hub-v0.26.5 + {py3.8,py3.12,py3.13}-huggingface_hub-v0.30.2 + {py3.8,py3.12,py3.13}-huggingface_hub-v0.33.0 # ~~~ DBs ~~~ @@ -157,7 +157,7 @@ envlist = {py3.6}-pymongo-v3.5.1 {py3.6,py3.10,py3.11}-pymongo-v3.13.0 {py3.6,py3.9,py3.10}-pymongo-v4.0.2 - {py3.9,py3.12,py3.13}-pymongo-v4.13.0 + {py3.9,py3.12,py3.13}-pymongo-v4.13.2 {py3.6}-redis_py_cluster_legacy-v1.3.6 {py3.6,py3.7}-redis_py_cluster_legacy-v2.0.0 @@ -180,7 +180,7 @@ envlist = {py3.7,py3.12,py3.13}-statsig-v0.55.3 {py3.7,py3.12,py3.13}-statsig-v0.56.0 {py3.7,py3.12,py3.13}-statsig-v0.57.3 - {py3.7,py3.12,py3.13}-statsig-v0.58.1 + {py3.7,py3.12,py3.13}-statsig-v0.58.2 {py3.8,py3.12,py3.13}-unleash-v6.0.1 {py3.8,py3.12,py3.13}-unleash-v6.1.0 @@ -201,9 +201,9 @@ envlist = {py3.8,py3.12,py3.13}-graphene-v3.4.3 {py3.8,py3.10,py3.11}-strawberry-v0.209.8 - {py3.8,py3.11,py3.12}-strawberry-v0.230.0 - {py3.8,py3.12,py3.13}-strawberry-v0.251.0 - {py3.9,py3.12,py3.13}-strawberry-v0.273.0 + {py3.8,py3.11,py3.12}-strawberry-v0.231.1 + {py3.8,py3.12,py3.13}-strawberry-v0.253.1 + {py3.9,py3.12,py3.13}-strawberry-v0.274.0 # ~~~ Network ~~~ @@ -261,10 +261,10 @@ envlist = {py3.7}-aiohttp-v3.4.4 {py3.7,py3.8,py3.9}-aiohttp-v3.7.4 {py3.8,py3.12,py3.13}-aiohttp-v3.10.11 - {py3.9,py3.12,py3.13}-aiohttp-v3.12.12 + {py3.9,py3.12,py3.13}-aiohttp-v3.12.13 {py3.6,py3.7}-bottle-v0.12.25 - {py3.8,py3.12,py3.13}-bottle-v0.13.3 + {py3.8,py3.12,py3.13}-bottle-v0.13.4 {py3.6}-falcon-v1.4.1 {py3.6,py3.7}-falcon-v2.0.0 @@ -515,9 +515,9 @@ deps = cohere-v5.15.0: cohere==5.15.0 huggingface_hub-v0.22.2: huggingface_hub==0.22.2 - huggingface_hub-v0.25.2: huggingface_hub==0.25.2 - huggingface_hub-v0.28.1: huggingface_hub==0.28.1 - huggingface_hub-v0.32.6: huggingface_hub==0.32.6 + huggingface_hub-v0.26.5: huggingface_hub==0.26.5 + huggingface_hub-v0.30.2: huggingface_hub==0.30.2 + huggingface_hub-v0.33.0: huggingface_hub==0.33.0 # ~~~ DBs ~~~ @@ -526,7 +526,7 @@ deps = pymongo-v3.5.1: pymongo==3.5.1 pymongo-v3.13.0: pymongo==3.13.0 pymongo-v4.0.2: pymongo==4.0.2 - pymongo-v4.13.0: pymongo==4.13.0 + pymongo-v4.13.2: pymongo==4.13.2 pymongo: mockupdb redis_py_cluster_legacy-v1.3.6: redis-py-cluster==1.3.6 @@ -550,7 +550,7 @@ deps = statsig-v0.55.3: statsig==0.55.3 statsig-v0.56.0: statsig==0.56.0 statsig-v0.57.3: statsig==0.57.3 - statsig-v0.58.1: statsig==0.58.1 + statsig-v0.58.2: statsig==0.58.2 statsig: typing_extensions unleash-v6.0.1: UnleashClient==6.0.1 @@ -580,13 +580,13 @@ deps = py3.6-graphene: aiocontextvars strawberry-v0.209.8: strawberry-graphql[fastapi,flask]==0.209.8 - strawberry-v0.230.0: strawberry-graphql[fastapi,flask]==0.230.0 - strawberry-v0.251.0: strawberry-graphql[fastapi,flask]==0.251.0 - strawberry-v0.273.0: strawberry-graphql[fastapi,flask]==0.273.0 + strawberry-v0.231.1: strawberry-graphql[fastapi,flask]==0.231.1 + strawberry-v0.253.1: strawberry-graphql[fastapi,flask]==0.253.1 + strawberry-v0.274.0: strawberry-graphql[fastapi,flask]==0.274.0 strawberry: httpx strawberry-v0.209.8: pydantic<2.11 - strawberry-v0.230.0: pydantic<2.11 - strawberry-v0.251.0: pydantic<2.11 + strawberry-v0.231.1: pydantic<2.11 + strawberry-v0.253.1: pydantic<2.11 # ~~~ Network ~~~ @@ -696,13 +696,13 @@ deps = aiohttp-v3.4.4: aiohttp==3.4.4 aiohttp-v3.7.4: aiohttp==3.7.4 aiohttp-v3.10.11: aiohttp==3.10.11 - aiohttp-v3.12.12: aiohttp==3.12.12 + aiohttp-v3.12.13: aiohttp==3.12.13 aiohttp: pytest-aiohttp aiohttp-v3.10.11: pytest-asyncio - aiohttp-v3.12.12: pytest-asyncio + aiohttp-v3.12.13: pytest-asyncio bottle-v0.12.25: bottle==0.12.25 - bottle-v0.13.3: bottle==0.13.3 + bottle-v0.13.4: bottle==0.13.4 bottle: werkzeug<2.1.0 falcon-v1.4.1: falcon==1.4.1 From 3f9acc4cf74da1543a2b8fc14799ed186ef58053 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Wed, 18 Jun 2025 17:29:32 +0200 Subject: [PATCH 06/14] fix(ci): Do not install newest tracerite (#4494) New release of `tracerite` (a transitive dependency in the sanic workflow) causes [this](https://github.com/getsentry/sentry-python/actions/runs/15735197921/job/44345942053?pr=4493) to happen. Related `tracerite` bug report: https://github.com/sanic-org/tracerite/issues/20 Once this is fixed, we can unpin. --- scripts/populate_tox/tox.jinja | 1 + tox.ini | 1 + 2 files changed, 2 insertions(+) diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index 45f56e2f1f..3386e2ae72 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -326,6 +326,7 @@ deps = # Sanic sanic: websockets<11.0 sanic: aiohttp + {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-sanic: tracerite<1.1.2 sanic-v{24.6}: sanic_testing sanic-latest: sanic_testing {py3.6}-sanic: aiocontextvars==0.2.1 diff --git a/tox.ini b/tox.ini index c0c50c6029..a94ecba825 100644 --- a/tox.ini +++ b/tox.ini @@ -487,6 +487,7 @@ deps = # Sanic sanic: websockets<11.0 sanic: aiohttp + {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-sanic: tracerite<1.1.2 sanic-v{24.6}: sanic_testing sanic-latest: sanic_testing {py3.6}-sanic: aiocontextvars==0.2.1 From d39599fc374b01a62fd702fa3adc59aac0f2b79c Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Thu, 19 Jun 2025 10:03:34 -0400 Subject: [PATCH 07/14] fix(profiling): Ensure profiler thread exits when needed (#4497) The soft exit wasn't properly shutting down the thread if another profiler started up too quickly. This ensures it is reused if possible but is properly shutdown if needed. Specifically, the shutdown allowed the profiler 1 cycle before actually shutting down. If another profiler is started during this cycle, it's possible the old profiler never shuts down. Resulting in multiple profilers running. Fixes #4489 --- sentry_sdk/profiler/continuous_profiler.py | 38 +++++++++++++++------- tests/profiler/test_continuous_profiler.py | 31 +++++++++++++++--- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/sentry_sdk/profiler/continuous_profiler.py b/sentry_sdk/profiler/continuous_profiler.py index 77ba60dbda..00dd29e36c 100644 --- a/sentry_sdk/profiler/continuous_profiler.py +++ b/sentry_sdk/profiler/continuous_profiler.py @@ -236,6 +236,7 @@ def __init__(self, frequency, options, sdk_info, capture_func): self.pid = None # type: Optional[int] self.running = False + self.soft_shutdown = False self.new_profiles = deque(maxlen=128) # type: Deque[ContinuousProfile] self.active_profiles = set() # type: Set[ContinuousProfile] @@ -317,7 +318,7 @@ def profiler_id(self): return self.buffer.profiler_id def make_sampler(self): - # type: () -> Callable[..., None] + # type: () -> Callable[..., bool] cwd = os.getcwd() cache = LRUCache(max_size=256) @@ -325,7 +326,7 @@ def make_sampler(self): if self.lifecycle == "trace": def _sample_stack(*args, **kwargs): - # type: (*Any, **Any) -> None + # type: (*Any, **Any) -> bool """ Take a sample of the stack on all the threads in the process. This should be called at a regular interval to collect samples. @@ -333,8 +334,7 @@ def _sample_stack(*args, **kwargs): # no profiles taking place, so we can stop early if not self.new_profiles and not self.active_profiles: - self.running = False - return + return True # This is the number of profiles we want to pop off. # It's possible another thread adds a new profile to @@ -357,7 +357,7 @@ def _sample_stack(*args, **kwargs): # For some reason, the frame we get doesn't have certain attributes. # When this happens, we abandon the current sample as it's bad. capture_internal_exception(sys.exc_info()) - return + return False # Move the new profiles into the active_profiles set. # @@ -374,9 +374,7 @@ def _sample_stack(*args, **kwargs): inactive_profiles = [] for profile in self.active_profiles: - if profile.active: - pass - else: + if not profile.active: # If a profile is marked inactive, we buffer it # to `inactive_profiles` so it can be removed. # We cannot remove it here as it would result @@ -389,10 +387,12 @@ def _sample_stack(*args, **kwargs): if self.buffer is not None: self.buffer.write(ts, sample) + return False + else: def _sample_stack(*args, **kwargs): - # type: (*Any, **Any) -> None + # type: (*Any, **Any) -> bool """ Take a sample of the stack on all the threads in the process. This should be called at a regular interval to collect samples. @@ -409,11 +409,13 @@ def _sample_stack(*args, **kwargs): # For some reason, the frame we get doesn't have certain attributes. # When this happens, we abandon the current sample as it's bad. capture_internal_exception(sys.exc_info()) - return + return False if self.buffer is not None: self.buffer.write(ts, sample) + return False + return _sample_stack def run(self): @@ -421,7 +423,7 @@ def run(self): last = time.perf_counter() while self.running: - self.sampler() + self.soft_shutdown = self.sampler() # some time may have elapsed since the last time # we sampled, so we need to account for that and @@ -430,6 +432,15 @@ def run(self): if elapsed < self.interval: thread_sleep(self.interval - elapsed) + # the soft shutdown happens here to give it a chance + # for the profiler to be reused + if self.soft_shutdown: + self.running = False + + # make sure to explicitly exit the profiler here or there might + # be multiple profilers at once + break + # after sleeping, make sure to take the current # timestamp so we can use it next iteration last = time.perf_counter() @@ -458,6 +469,8 @@ def __init__(self, frequency, options, sdk_info, capture_func): def ensure_running(self): # type: () -> None + self.soft_shutdown = False + pid = os.getpid() # is running on the right process @@ -532,6 +545,9 @@ def __init__(self, frequency, options, sdk_info, capture_func): def ensure_running(self): # type: () -> None + + self.soft_shutdown = False + pid = os.getpid() # is running on the right process diff --git a/tests/profiler/test_continuous_profiler.py b/tests/profiler/test_continuous_profiler.py index 991f8bda5d..7283ec7164 100644 --- a/tests/profiler/test_continuous_profiler.py +++ b/tests/profiler/test_continuous_profiler.py @@ -459,33 +459,54 @@ def test_continuous_profiler_auto_start_and_stop_sampled( thread = threading.current_thread() + all_profiler_ids = set() + for _ in range(3): envelopes.clear() + profiler_ids = set() + with sentry_sdk.start_transaction(name="profiling 1"): - assert get_profiler_id() is not None, "profiler should be running" + profiler_id = get_profiler_id() + assert profiler_id is not None, "profiler should be running" + profiler_ids.add(profiler_id) with sentry_sdk.start_span(op="op"): time.sleep(0.1) - assert get_profiler_id() is not None, "profiler should be running" + profiler_id = get_profiler_id() + assert profiler_id is not None, "profiler should be running" + profiler_ids.add(profiler_id) + + time.sleep(0.03) # the profiler takes a while to stop in auto mode so if we start # a transaction immediately, it'll be part of the same chunk - assert get_profiler_id() is not None, "profiler should be running" + profiler_id = get_profiler_id() + assert profiler_id is not None, "profiler should be running" + profiler_ids.add(profiler_id) with sentry_sdk.start_transaction(name="profiling 2"): - assert get_profiler_id() is not None, "profiler should be running" + profiler_id = get_profiler_id() + assert profiler_id is not None, "profiler should be running" + profiler_ids.add(profiler_id) with sentry_sdk.start_span(op="op"): time.sleep(0.1) - assert get_profiler_id() is not None, "profiler should be running" + profiler_id = get_profiler_id() + assert profiler_id is not None, "profiler should be running" + profiler_ids.add(profiler_id) # wait at least 1 cycle for the profiler to stop time.sleep(0.2) assert get_profiler_id() is None, "profiler should not be running" + assert len(profiler_ids) == 1 + all_profiler_ids.add(profiler_ids.pop()) + assert_single_transaction_with_profile_chunks( envelopes, thread, max_chunks=1, transactions=2 ) + assert len(all_profiler_ids) == 3 + @pytest.mark.parametrize( "mode", From ae06ef177b320a3fb1400c225105e4bf3503c987 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 23 Jun 2025 12:37:54 +0200 Subject: [PATCH 08/14] fix(ci): Remove tracerite pin (almost) (#4504) This reverts commit 3f9acc4cf74da1543a2b8fc14799ed186ef58053. - tracerite 1.12 contained syntax that did not work on Python 3.x - tracerite 1.13 was then released, containing a fix - however, on Python 3.8 newest tracerite seems to be using importlib features that were only added in 3.9 (see below), so still pinning it to an older version there ``` Traceback: .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/_pytest/python.py:493: in importtestmodule mod = import_path( .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/_pytest/pathlib.py:587: in import_path importlib.import_module(module_name) ../../.pyenv/versions/3.8.18/lib/python3.8/importlib/__init__.py:127: in import_module return _bootstrap._gcd_import(name[level:], package, level) :1014: in _gcd_import ??? :991: in _find_and_load ??? :961: in _find_and_load_unlocked ??? :219: in _call_with_frames_removed ??? :1014: in _gcd_import ??? :991: in _find_and_load ??? :975: in _find_and_load_unlocked ??? :671: in _load_unlocked ??? :843: in exec_module ??? :219: in _call_with_frames_removed ??? tests/integrations/sanic/__init__.py:3: in import sanic .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/__init__.py:6: in from sanic.app import Sanic .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/app.py:58: in from sanic.application.state import ApplicationState, ServerStage .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/application/state.py:13: in from sanic.server.async_server import AsyncioServer .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/server/__init__.py:5: in from sanic.server.runners import serve .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/server/runners.py:6: in from sanic.config import Config .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/config.py:13: in from sanic.errorpages import DEFAULT_FORMAT, check_error_format .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/errorpages.py:27: in from sanic.pages.error import ErrorPage .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/sanic/pages/error.py:3: in import tracerite.html .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/tracerite/__init__.py:1: in from .html import html_traceback .tox/py3.8-sanic-v24.6/lib/python3.8/site-packages/tracerite/html.py:5: in from importlib.resources import files E ImportError: cannot import name 'files' from 'importlib.resources' (/Users/ivana/.pyenv/versions/3.8.18/lib/python3.8/importlib/resources.py) ``` --- scripts/populate_tox/tox.jinja | 2 +- tox.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index 3386e2ae72..f95a913fd9 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -326,10 +326,10 @@ deps = # Sanic sanic: websockets<11.0 sanic: aiohttp - {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-sanic: tracerite<1.1.2 sanic-v{24.6}: sanic_testing sanic-latest: sanic_testing {py3.6}-sanic: aiocontextvars==0.2.1 + {py3.8}-sanic: tracerite<1.1.2 sanic-v0.8: sanic~=0.8.0 sanic-v20: sanic~=20.0 sanic-v24.6: sanic~=24.6.0 diff --git a/tox.ini b/tox.ini index a94ecba825..7efbcb6d55 100644 --- a/tox.ini +++ b/tox.ini @@ -487,10 +487,10 @@ deps = # Sanic sanic: websockets<11.0 sanic: aiohttp - {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-sanic: tracerite<1.1.2 sanic-v{24.6}: sanic_testing sanic-latest: sanic_testing {py3.6}-sanic: aiocontextvars==0.2.1 + {py3.8}-sanic: tracerite<1.1.2 sanic-v0.8: sanic~=0.8.0 sanic-v20: sanic~=20.0 sanic-v24.6: sanic~=24.6.0 From 3e2994800dc99b07b016053878e90d5b64dbdeae Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 24 Jun 2025 12:17:39 +0200 Subject: [PATCH 09/14] Cursor generated rules (#4493) It also added performance and aws related files but I want to keep it simple for now. This adds: * quick reference * testing guide * project overview * core architecture * integration guide --- .cursor/rules/core-architecture.mdc | 122 +++++++++++++++++++++ .cursor/rules/integrations-guide.mdc | 158 +++++++++++++++++++++++++++ .cursor/rules/project-overview.mdc | 47 ++++++++ .cursor/rules/quick-reference.mdc | 51 +++++++++ .cursor/rules/testing-guide.mdc | 93 ++++++++++++++++ 5 files changed, 471 insertions(+) create mode 100644 .cursor/rules/core-architecture.mdc create mode 100644 .cursor/rules/integrations-guide.mdc create mode 100644 .cursor/rules/project-overview.mdc create mode 100644 .cursor/rules/quick-reference.mdc create mode 100644 .cursor/rules/testing-guide.mdc diff --git a/.cursor/rules/core-architecture.mdc b/.cursor/rules/core-architecture.mdc new file mode 100644 index 0000000000..885773f16d --- /dev/null +++ b/.cursor/rules/core-architecture.mdc @@ -0,0 +1,122 @@ +--- +description: +globs: +alwaysApply: false +--- +# Core Architecture + +## Scope and Client Pattern + +The Sentry SDK uses a **Scope and Client** pattern for managing state and context: + +### Scope +- [sentry_sdk/scope.py](mdc:sentry_sdk/scope.py) - Holds contextual data +- Holds a reference to the Client +- Contains tags, extra data, user info, breadcrumbs +- Thread-local storage for isolation + +### Client +- [sentry_sdk/client.py](mdc:sentry_sdk/client.py) - Handles event processing +- Manages transport and event serialization +- Applies sampling and filtering + +## Key Components + +### API Layer +- [sentry_sdk/api.py](mdc:sentry_sdk/api.py) - Public API functions +- `init()` - Initialize the SDK +- `capture_exception()` - Capture exceptions +- `capture_message()` - Capture custom messages +- `set_tag()`, `set_user()`, `set_context()` - Add context +- `start_transaction()` - Performance monitoring + +### Transport +- [sentry_sdk/transport.py](mdc:sentry_sdk/transport.py) - Event delivery +- `HttpTransport` - HTTP transport to Sentry servers +- Handles retries, rate limiting, and queuing + +### Integrations System +- [sentry_sdk/integrations/__init__.py](mdc:sentry_sdk/integrations/__init__.py) - Integration registry +- Base `Integration` class for all integrations +- Automatic setup and teardown +- Integration-specific configuration + +## Data Flow + +### Event Capture Flow +1. **Exception occurs** or **manual capture** called +2. **get_current_scope** gets the active current scope +2. **get_isolation_scope** gets the active isolation scope +3. **Scope data** (tags, user, context) is attached +4. **Client.process_event()** processes the event +5. **Sampling** and **filtering** applied +6. **Transport** sends to Sentry servers + +### Performance Monitoring Flow +1. **Transaction started** with `start_transaction()` +2. **Spans** created for operations within transaction with `start_span()` +3. **Timing data** collected automatically +4. **Transaction finished** and sent to Sentry + +## Context Management + +### Scope Stack +- **Global scope**: Default scope for the process +- **Isolation scope**: Isolated scope for specific operations, manages concurrency isolation +- **Current scope**: Active scope for current execution context + +### Scope Operations +- `configure_scope()` - Modify current scope +- `new_scope()` - Create isolated scope + +## Integration Architecture + +### Integration Lifecycle +1. **Registration**: Integration registered during `init()` +2. **Setup**: `setup_once()` called to install hooks +3. **Runtime**: Integration monitors and captures events +4. **Teardown**: Integration cleaned up on shutdown + +### Common Integration Patterns +- **Monkey patching**: Replace functions/methods with instrumented versions +- **Signal handlers**: Hook into framework signals/events +- **Middleware**: Add middleware to web frameworks +- **Exception handlers**: Catch and process exceptions + +### Integration Configuration +```python +# Example integration setup +sentry_sdk.init( + dsn="your-dsn", + integrations=[ + DjangoIntegration(), + CeleryIntegration(), + RedisIntegration(), + ], + traces_sample_rate=1.0, +) +``` + +## Error Handling + +### Exception Processing +- **Automatic capture**: Unhandled exceptions captured automatically +- **Manual capture**: Use `capture_exception()` for handled exceptions +- **Context preservation**: Stack traces, local variables, and context preserved + +### Breadcrumbs +- **Automatic breadcrumbs**: Framework operations logged automatically +- **Manual breadcrumbs**: Use `add_breadcrumb()` for custom events +- **Breadcrumb categories**: HTTP, database, navigation, etc. + +## Performance Monitoring + +### Transaction Tracking +- **Automatic transactions**: Web requests, background tasks +- **Custom transactions**: Use `start_transaction()` for custom operations +- **Span tracking**: Database queries, HTTP requests, custom operations +- **Performance data**: Timing, resource usage, custom measurements + +### Sampling +- **Transaction sampling**: Control percentage of transactions captured +- **Dynamic sampling**: Adjust sampling based on context diff --git a/.cursor/rules/integrations-guide.mdc b/.cursor/rules/integrations-guide.mdc new file mode 100644 index 0000000000..869a7f742a --- /dev/null +++ b/.cursor/rules/integrations-guide.mdc @@ -0,0 +1,158 @@ +--- +description: +globs: +alwaysApply: false +--- +# Integrations Guide + +## Integration Categories + +The Sentry Python SDK includes integrations for popular frameworks, libraries, and services: + +### Web Frameworks +- [sentry_sdk/integrations/django/](mdc:sentry_sdk/integrations/django) - Django web framework +- [sentry_sdk/integrations/flask/](mdc:sentry_sdk/integrations/flask) - Flask microframework +- [sentry_sdk/integrations/fastapi/](mdc:sentry_sdk/integrations/fastapi) - FastAPI framework +- [sentry_sdk/integrations/starlette/](mdc:sentry_sdk/integrations/starlette) - Starlette ASGI framework +- [sentry_sdk/integrations/sanic/](mdc:sentry_sdk/integrations/sanic) - Sanic async framework +- [sentry_sdk/integrations/tornado/](mdc:sentry_sdk/integrations/tornado) - Tornado web framework +- [sentry_sdk/integrations/pyramid/](mdc:sentry_sdk/integrations/pyramid) - Pyramid framework +- [sentry_sdk/integrations/bottle/](mdc:sentry_sdk/integrations/bottle) - Bottle microframework +- [sentry_sdk/integrations/chalice/](mdc:sentry_sdk/integrations/chalice) - AWS Chalice +- [sentry_sdk/integrations/quart/](mdc:sentry_sdk/integrations/quart) - Quart async framework +- [sentry_sdk/integrations/falcon/](mdc:sentry_sdk/integrations/falcon) - Falcon framework +- [sentry_sdk/integrations/litestar/](mdc:sentry_sdk/integrations/litestar) - Litestar framework +- [sentry_sdk/integrations/starlite/](mdc:sentry_sdk/integrations/starlite) - Starlite framework + +### Task Queues and Background Jobs +- [sentry_sdk/integrations/celery/](mdc:sentry_sdk/integrations/celery) - Celery task queue +- [sentry_sdk/integrations/rq/](mdc:sentry_sdk/integrations/rq) - Redis Queue +- [sentry_sdk/integrations/huey/](mdc:sentry_sdk/integrations/huey) - Huey task queue +- [sentry_sdk/integrations/arq/](mdc:sentry_sdk/integrations/arq) - Arq async task queue +- [sentry_sdk/integrations/dramatiq/](mdc:sentry_sdk/integrations/dramatiq) - Dramatiq task queue + +### Databases and Data Stores +- [sentry_sdk/integrations/sqlalchemy/](mdc:sentry_sdk/integrations/sqlalchemy) - SQLAlchemy ORM +- [sentry_sdk/integrations/asyncpg/](mdc:sentry_sdk/integrations/asyncpg) - AsyncPG PostgreSQL +- [sentry_sdk/integrations/pymongo/](mdc:sentry_sdk/integrations/pymongo) - PyMongo MongoDB +- [sentry_sdk/integrations/redis/](mdc:sentry_sdk/integrations/redis) - Redis client +- [sentry_sdk/integrations/clickhouse_driver/](mdc:sentry_sdk/integrations/clickhouse_driver) - ClickHouse driver + +### Cloud and Serverless +- [sentry_sdk/integrations/aws_lambda/](mdc:sentry_sdk/integrations/aws_lambda) - AWS Lambda +- [sentry_sdk/integrations/gcp/](mdc:sentry_sdk/integrations/gcp) - Google Cloud Platform +- [sentry_sdk/integrations/serverless/](mdc:sentry_sdk/integrations/serverless) - Serverless framework + +### HTTP and Networking +- [sentry_sdk/integrations/requests/](mdc:sentry_sdk/integrations/requests) - Requests HTTP library +- [sentry_sdk/integrations/httpx/](mdc:sentry_sdk/integrations/httpx) - HTTPX async HTTP client +- [sentry_sdk/integrations/aiohttp/](mdc:sentry_sdk/integrations/aiohttp) - aiohttp async HTTP +- [sentry_sdk/integrations/grpc/](mdc:sentry_sdk/integrations/grpc) - gRPC framework + +### AI and Machine Learning +- [sentry_sdk/integrations/openai/](mdc:sentry_sdk/integrations/openai) - OpenAI API +- [sentry_sdk/integrations/anthropic/](mdc:sentry_sdk/integrations/anthropic) - Anthropic Claude +- [sentry_sdk/integrations/cohere/](mdc:sentry_sdk/integrations/cohere) - Cohere AI +- [sentry_sdk/integrations/huggingface_hub/](mdc:sentry_sdk/integrations/huggingface_hub) - Hugging Face Hub +- [sentry_sdk/integrations/langchain/](mdc:sentry_sdk/integrations/langchain) - LangChain framework + +### GraphQL +- [sentry_sdk/integrations/graphene/](mdc:sentry_sdk/integrations/graphene) - Graphene GraphQL +- [sentry_sdk/integrations/ariadne/](mdc:sentry_sdk/integrations/ariadne) - Ariadne GraphQL +- [sentry_sdk/integrations/strawberry/](mdc:sentry_sdk/integrations/strawberry) - Strawberry GraphQL +- [sentry_sdk/integrations/gql/](mdc:sentry_sdk/integrations/gql) - GQL GraphQL client + +### Feature Flags and Configuration +- [sentry_sdk/integrations/launchdarkly/](mdc:sentry_sdk/integrations/launchdarkly) - LaunchDarkly +- [sentry_sdk/integrations/unleash/](mdc:sentry_sdk/integrations/unleash) - Unleash +- [sentry_sdk/integrations/statsig/](mdc:sentry_sdk/integrations/statsig) - Statsig +- [sentry_sdk/integrations/openfeature/](mdc:sentry_sdk/integrations/openfeature) - OpenFeature + +### Other Integrations +- [sentry_sdk/integrations/logging/](mdc:sentry_sdk/integrations/logging) - Python logging +- [sentry_sdk/integrations/loguru/](mdc:sentry_sdk/integrations/loguru) - Loguru logging +- [sentry_sdk/integrations/opentelemetry/](mdc:sentry_sdk/integrations/opentelemetry) - OpenTelemetry +- [sentry_sdk/integrations/ray/](mdc:sentry_sdk/integrations/ray) - Ray distributed computing +- [sentry_sdk/integrations/spark/](mdc:sentry_sdk/integrations/spark) - Apache Spark +- [sentry_sdk/integrations/beam/](mdc:sentry_sdk/integrations/beam) - Apache Beam + +## Integration Usage + +### Basic Integration Setup +```python +import sentry_sdk +from sentry_sdk.integrations.django import DjangoIntegration +from sentry_sdk.integrations.celery import CeleryIntegration + +sentry_sdk.init( + dsn="your-dsn", + integrations=[ + DjangoIntegration(), + CeleryIntegration(), + ], + traces_sample_rate=1.0, +) +``` + +### Integration Configuration +Most integrations accept configuration parameters: +```python +from sentry_sdk.integrations.django import DjangoIntegration +from sentry_sdk.integrations.redis import RedisIntegration + +sentry_sdk.init( + dsn="your-dsn", + integrations=[ + DjangoIntegration( + transaction_style="url", # Customize transaction naming + ), + RedisIntegration( + cache_prefixes=["myapp:"], # Filter cache operations + ), + ], +) +``` + +### Integration Testing +Each integration has corresponding tests in [tests/integrations/](mdc:tests/integrations): +- [tests/integrations/django/](mdc:tests/integrations/django) - Django integration tests +- [tests/integrations/flask/](mdc:tests/integrations/flask) - Flask integration tests +- [tests/integrations/celery/](mdc:tests/integrations/celery) - Celery integration tests + +## Integration Development + +### Creating New Integrations +1. **Create integration file** in [sentry_sdk/integrations/](mdc:sentry_sdk/integrations) +2. **Inherit from Integration base class** +3. **Implement setup_once() method** +4. **Add to integration registry** + +### Integration Base Class +```python +from sentry_sdk.integrations import Integration + +class MyIntegration(Integration): + identifier = "my_integration" + + def __init__(self, param=None): + self.param = param + + @staticmethod + def setup_once(): + # Install hooks, monkey patches, etc. + pass +``` + +### Common Integration Patterns +- **Monkey patching**: Replace functions with instrumented versions +- **Middleware**: Add middleware to web frameworks +- **Signal handlers**: Hook into framework signals +- **Exception handlers**: Catch and process exceptions +- **Context managers**: Add context to operations + +### Integration Best Practices +- **Zero configuration**: Work without user setup +- **Check integration status**: Use `sentry_sdk.get_client().get_integration()` +- **No side effects**: Don't alter library behavior +- **Graceful degradation**: Handle missing dependencies +- **Comprehensive testing**: Test all integration features diff --git a/.cursor/rules/project-overview.mdc b/.cursor/rules/project-overview.mdc new file mode 100644 index 0000000000..13fad83ae7 --- /dev/null +++ b/.cursor/rules/project-overview.mdc @@ -0,0 +1,47 @@ +--- +description: +globs: +alwaysApply: false +--- +# Sentry Python SDK - Project Overview + +## What is this project? + +The Sentry Python SDK is the official Python SDK for [Sentry](mdc:https://sentry.io), an error monitoring and performance monitoring platform. It helps developers capture errors, exceptions, traces and profiles from Python applications. + +## Key Files and Directories + +### Core SDK +- [sentry_sdk/__init__.py](mdc:sentry_sdk/__init__.py) - Main entry point, exports all public APIs +- [sentry_sdk/api.py](mdc:sentry_sdk/api.py) - Public API functions (init, capture_exception, etc.) +- [sentry_sdk/client.py](mdc:sentry_sdk/client.py) - Core client implementation +- [sentry_sdk/scope.py](mdc:sentry_sdk/scope.py) - Scope holds contextual metadata such as tags that are applied automatically to events and envelopes +- [sentry_sdk/transport.py](mdc:sentry_sdk/transport.py) - HTTP Transport that sends the envelopes to Sentry's servers +- [sentry_sdk/worker.py](mdc:sentry_sdk/worker.py) - Background threaded worker with a queue to manage transport requests +- [sentry_sdk/serializer.py](mdc:sentry_sdk/serializer.py) - Serializes the payload along with truncation logic + +### Integrations +- [sentry_sdk/integrations/](mdc:sentry_sdk/integrations) - Framework and library integrations + - [sentry_sdk/integrations/__init__.py](mdc:sentry_sdk/integrations/__init__.py) - Integration registry + - [sentry_sdk/integrations/django/](mdc:sentry_sdk/integrations/django) - Django framework integration + - [sentry_sdk/integrations/flask/](mdc:sentry_sdk/integrations/flask) - Flask framework integration + - [sentry_sdk/integrations/fastapi/](mdc:sentry_sdk/integrations/fastapi) - FastAPI integration + - [sentry_sdk/integrations/celery/](mdc:sentry_sdk/integrations/celery) - Celery task queue integration + - [sentry_sdk/integrations/aws_lambda/](mdc:sentry_sdk/integrations/aws_lambda) - AWS Lambda integration + +### Configuration and Setup +- [setup.py](mdc:setup.py) - Package configuration and dependencies +- [pyproject.toml](mdc:pyproject.toml) - Modern Python project configuration +- [tox.ini](mdc:tox.ini) - Test matrix configuration for multiple Python versions and integrations +- [requirements-*.txt](mdc:requirements-testing.txt) - Various dependency requirements + +### Documentation and Guides +- [README.md](mdc:README.md) - Project overview and quick start +- [CONTRIBUTING.md](mdc:CONTRIBUTING.md) - Development and contribution guidelines +- [MIGRATION_GUIDE.md](mdc:MIGRATION_GUIDE.md) - Migration from older versions +- [CHANGELOG.md](mdc:CHANGELOG.md) - Version history and changes + +### Testing +- [tests/](mdc:tests) - Comprehensive test suite + - [tests/integrations/](mdc:tests/integrations) - Integration-specific tests + - [tests/conftest.py](mdc:tests/conftest.py) - Pytest configuration and fixtures diff --git a/.cursor/rules/quick-reference.mdc b/.cursor/rules/quick-reference.mdc new file mode 100644 index 0000000000..453869fa83 --- /dev/null +++ b/.cursor/rules/quick-reference.mdc @@ -0,0 +1,51 @@ +--- +description: +globs: +alwaysApply: false +--- +# Quick Reference + +## Common Commands + +### Development Setup +```bash +make .venv +source .venv/bin/activate # Windows: .venv\Scripts\activate +``` + +### Testing + +Our test matrix is implemented in [tox](mdc:https://tox.wiki). +The following runs the whole test suite and takes a long time. + +```bash +source .venv/bin/activate +tox +``` + +Prefer testing a single environment instead while developing. + +```bash +tox -e py3.12-common +``` + +For running a single test, use the pattern: + +```bash +tox -e py3.12-common -- project/tests/test_file.py::TestClassName::test_method +``` + +For testing specific integrations, refer to the test matrix in [sentry_sdk/tox.ini](mdc:sentry_sdk/tox.ini) for finding an entry. +For example, to test django, use: + +```bash +tox -e py3.12-django-v5.2.3 +``` + +### Code Quality + +Our `linters` tox environment runs `black` for formatting, `flake8` for linting and `mypy` for type checking. + +```bash +tox -e linters +``` diff --git a/.cursor/rules/testing-guide.mdc b/.cursor/rules/testing-guide.mdc new file mode 100644 index 0000000000..e336bb337a --- /dev/null +++ b/.cursor/rules/testing-guide.mdc @@ -0,0 +1,93 @@ +--- +description: +globs: +alwaysApply: false +--- +# Testing Guide + +## Test Structure + +### Test Organization +- [tests/](mdc:tests) - Main test directory +- [tests/conftest.py](mdc:tests/conftest.py) - Shared pytest fixtures and configuration +- [tests/integrations/](mdc:tests/integrations) - Integration-specific tests +- [tests/tracing/](mdc:tests/tracing) - Performance monitoring tests +- [tests/utils/](mdc:tests/utils) - Utility and helper tests + +### Integration Test Structure +Each integration has its own test directory: +- [tests/integrations/django/](mdc:tests/integrations/django) - Django integration tests +- [tests/integrations/flask/](mdc:tests/integrations/flask) - Flask integration tests +- [tests/integrations/celery/](mdc:tests/integrations/celery) - Celery integration tests +- [tests/integrations/aws_lambda/](mdc:tests/integrations/aws_lambda) - AWS Lambda tests + +## Running Tests + +### Tox Testing Matrix + +The [tox.ini](mdc:tox.ini) file defines comprehensive test environments. +Always run tests via `tox` from the main `.venv`. + +```bash +source .venv/bin/activate + +# Run all tox environments, takes a long time +tox + +# Run specific environment +tox -e py3.11-django-v4.2 + +# Run environments for specific Python version +tox -e py3.11-* + +# Run environments for specific integration +tox -e *-django-* + +# Run a single test +tox -e py3.12-common -- project/tests/test_file.py::TestClassName::test_method +``` + +### Test Environment Categories +- **Common tests**: `{py3.6,py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13}-common` +- **Integration tests**: `{python_version}-{integration}-v{framework_version}` +- **Gevent tests**: `{py3.6,py3.8,py3.10,py3.11,py3.12}-gevent` + +## Writing Tests + +### Test File Structure +```python +import pytest +import sentry_sdk +from sentry_sdk.integrations.flask import FlaskIntegration + +def test_flask_integration(sentry_init, capture_events): + """Test Flask integration captures exceptions.""" + # Test setup + sentry_init(integrations=[FlaskIntegration()]) + events = capture_events() + + # Test execution + # ... test code ... + + # Assertions + assert len(events) == 1 + assert events[0]["exception"]["values"][0]["type"] == "ValueError" +``` + +### Common Test Patterns + +## Test Best Practices + +### Test Organization +- **One test per function**: Each test should verify one specific behavior +- **Descriptive names**: Use clear, descriptive test function names +- **Arrange-Act-Assert**: Structure tests with setup, execution, and verification +- **Isolation**: Each test should be independent and not affect others +- **No mocking**: Never use mocks in tests +- **Cleanup**: Ensure tests clean up after themselves + +## Fixtures +The most important fixtures for testing are: +- `sentry_init`: Use in the beginning of a test to simulate initializing the SDK +- `capture_events`: Intercept the events for testing event payload +- `capture_envelopes`: Intercept the envelopes for testing envelope headers and payload From ad2bbff928bef9464dc13099dae1fb200717aaf4 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 24 Jun 2025 14:12:31 +0200 Subject: [PATCH 10/14] tests: Tox update (#4509) --- tox.ini | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tox.ini b/tox.ini index 7efbcb6d55..f4aee13d02 100644 --- a/tox.ini +++ b/tox.ini @@ -10,7 +10,7 @@ # The file (and all resulting CI YAMLs) then need to be regenerated via # "scripts/generate-test-files.sh". # -# Last generated: 2025-06-17T08:49:27.078408+00:00 +# Last generated: 2025-06-24T07:19:36.122984+00:00 [tox] requires = @@ -138,7 +138,7 @@ envlist = {py3.8,py3.11,py3.12}-anthropic-v0.16.0 {py3.8,py3.11,py3.12}-anthropic-v0.29.2 {py3.8,py3.11,py3.12}-anthropic-v0.42.0 - {py3.8,py3.11,py3.12}-anthropic-v0.54.0 + {py3.8,py3.11,py3.12}-anthropic-v0.55.0 {py3.9,py3.10,py3.11}-cohere-v5.4.0 {py3.9,py3.11,py3.12}-cohere-v5.8.1 @@ -180,7 +180,7 @@ envlist = {py3.7,py3.12,py3.13}-statsig-v0.55.3 {py3.7,py3.12,py3.13}-statsig-v0.56.0 {py3.7,py3.12,py3.13}-statsig-v0.57.3 - {py3.7,py3.12,py3.13}-statsig-v0.58.2 + {py3.7,py3.12,py3.13}-statsig-v0.58.3 {py3.8,py3.12,py3.13}-unleash-v6.0.1 {py3.8,py3.12,py3.13}-unleash-v6.1.0 @@ -203,7 +203,7 @@ envlist = {py3.8,py3.10,py3.11}-strawberry-v0.209.8 {py3.8,py3.11,py3.12}-strawberry-v0.231.1 {py3.8,py3.12,py3.13}-strawberry-v0.253.1 - {py3.9,py3.12,py3.13}-strawberry-v0.274.0 + {py3.9,py3.12,py3.13}-strawberry-v0.275.2 # ~~~ Network ~~~ @@ -249,12 +249,12 @@ envlist = {py3.6,py3.9,py3.10}-starlette-v0.16.0 {py3.7,py3.10,py3.11}-starlette-v0.26.1 {py3.8,py3.11,py3.12}-starlette-v0.36.3 - {py3.9,py3.12,py3.13}-starlette-v0.47.0 + {py3.9,py3.12,py3.13}-starlette-v0.47.1 {py3.6,py3.9,py3.10}-fastapi-v0.79.1 {py3.7,py3.10,py3.11}-fastapi-v0.91.0 {py3.7,py3.10,py3.11}-fastapi-v0.103.2 - {py3.8,py3.12,py3.13}-fastapi-v0.115.12 + {py3.8,py3.12,py3.13}-fastapi-v0.115.13 # ~~~ Web 2 ~~~ @@ -504,7 +504,7 @@ deps = anthropic-v0.16.0: anthropic==0.16.0 anthropic-v0.29.2: anthropic==0.29.2 anthropic-v0.42.0: anthropic==0.42.0 - anthropic-v0.54.0: anthropic==0.54.0 + anthropic-v0.55.0: anthropic==0.55.0 anthropic: pytest-asyncio anthropic-v0.16.0: httpx<0.28.0 anthropic-v0.29.2: httpx<0.28.0 @@ -551,7 +551,7 @@ deps = statsig-v0.55.3: statsig==0.55.3 statsig-v0.56.0: statsig==0.56.0 statsig-v0.57.3: statsig==0.57.3 - statsig-v0.58.2: statsig==0.58.2 + statsig-v0.58.3: statsig==0.58.3 statsig: typing_extensions unleash-v6.0.1: UnleashClient==6.0.1 @@ -583,7 +583,7 @@ deps = strawberry-v0.209.8: strawberry-graphql[fastapi,flask]==0.209.8 strawberry-v0.231.1: strawberry-graphql[fastapi,flask]==0.231.1 strawberry-v0.253.1: strawberry-graphql[fastapi,flask]==0.253.1 - strawberry-v0.274.0: strawberry-graphql[fastapi,flask]==0.274.0 + strawberry-v0.275.2: strawberry-graphql[fastapi,flask]==0.275.2 strawberry: httpx strawberry-v0.209.8: pydantic<2.11 strawberry-v0.231.1: pydantic<2.11 @@ -666,7 +666,7 @@ deps = starlette-v0.16.0: starlette==0.16.0 starlette-v0.26.1: starlette==0.26.1 starlette-v0.36.3: starlette==0.36.3 - starlette-v0.47.0: starlette==0.47.0 + starlette-v0.47.1: starlette==0.47.1 starlette: pytest-asyncio starlette: python-multipart starlette: requests @@ -681,7 +681,7 @@ deps = fastapi-v0.79.1: fastapi==0.79.1 fastapi-v0.91.0: fastapi==0.91.0 fastapi-v0.103.2: fastapi==0.103.2 - fastapi-v0.115.12: fastapi==0.115.12 + fastapi-v0.115.13: fastapi==0.115.13 fastapi: httpx fastapi: pytest-asyncio fastapi: python-multipart From 7f507fd4e0cdbb1f071766431a6dcf3db77b7bb1 Mon Sep 17 00:00:00 2001 From: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> Date: Tue, 24 Jun 2025 14:36:58 +0200 Subject: [PATCH 11/14] ref(langchain): Greatly simplify `_wrap_configure` (#4479) Resolving #4443 requires some changes to this method, but the current `args`/`kwargs` business makes the method difficult to reason through. This PR simplifies the logic by listing out the parameters we need to access, so we don't need to access them through `args` and `kwargs`. We also cut down on the amount of branching and the amount of variables (`new_callbacks` vs `existing_callbacks`). Behavior does not change in this PR; we fix the #4443 bug in #4485, which is based on this PR --- Thank you for contributing to `sentry-python`! Please add tests to validate your changes, and lint your code using `tox -e linters`. Running the test suite on your PR might require maintainer approval. --- sentry_sdk/integrations/langchain.py | 90 +++++++++++++++------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/sentry_sdk/integrations/langchain.py b/sentry_sdk/integrations/langchain.py index 431fc46bec..1064f29ffd 100644 --- a/sentry_sdk/integrations/langchain.py +++ b/sentry_sdk/integrations/langchain.py @@ -22,6 +22,7 @@ from langchain_core.callbacks import ( manager, BaseCallbackHandler, + Callbacks, ) from langchain_core.agents import AgentAction, AgentFinish except ImportError: @@ -416,50 +417,57 @@ def _wrap_configure(f): # type: (Callable[..., Any]) -> Callable[..., Any] @wraps(f) - def new_configure(*args, **kwargs): - # type: (Any, Any) -> Any + def new_configure( + callback_manager_cls, # type: type + inheritable_callbacks=None, # type: Callbacks + local_callbacks=None, # type: Callbacks + *args, # type: Any + **kwargs, # type: Any + ): + # type: (...) -> Any integration = sentry_sdk.get_client().get_integration(LangchainIntegration) if integration is None: - return f(*args, **kwargs) + return f( + callback_manager_cls, + inheritable_callbacks, + local_callbacks, + *args, + **kwargs, + ) - with capture_internal_exceptions(): - new_callbacks = [] # type: List[BaseCallbackHandler] - if "local_callbacks" in kwargs: - existing_callbacks = kwargs["local_callbacks"] - kwargs["local_callbacks"] = new_callbacks - elif len(args) > 2: - existing_callbacks = args[2] - args = ( - args[0], - args[1], - new_callbacks, - ) + args[3:] - else: - existing_callbacks = [] - - if existing_callbacks: - if isinstance(existing_callbacks, list): - for cb in existing_callbacks: - new_callbacks.append(cb) - elif isinstance(existing_callbacks, BaseCallbackHandler): - new_callbacks.append(existing_callbacks) - else: - logger.debug("Unknown callback type: %s", existing_callbacks) - - already_added = False - for callback in new_callbacks: - if isinstance(callback, SentryLangchainCallback): - already_added = True - - if not already_added: - new_callbacks.append( - SentryLangchainCallback( - integration.max_spans, - integration.include_prompts, - integration.tiktoken_encoding_name, - ) - ) - return f(*args, **kwargs) + callbacks_list = local_callbacks or [] + + if isinstance(callbacks_list, BaseCallbackHandler): + callbacks_list = [callbacks_list] + elif not isinstance(callbacks_list, list): + logger.debug("Unknown callback type: %s", callbacks_list) + # Just proceed with original function call + return f( + callback_manager_cls, + inheritable_callbacks, + local_callbacks, + *args, + **kwargs, + ) + + if not any(isinstance(cb, SentryLangchainCallback) for cb in callbacks_list): + # Avoid mutating the existing callbacks list + callbacks_list = [ + *callbacks_list, + SentryLangchainCallback( + integration.max_spans, + integration.include_prompts, + integration.tiktoken_encoding_name, + ), + ] + + return f( + callback_manager_cls, + inheritable_callbacks, + callbacks_list, + *args, + **kwargs, + ) return new_configure From 4a0e5ed544a37109da1a8b33bda50f4871920a85 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 24 Jun 2025 14:42:53 +0200 Subject: [PATCH 12/14] Support `openai-agents` (#4437) Add support for AI agents projects using `openai-agents` (https://pypi.org/project/openai-agents/) Docs PR is here: https://github.com/getsentry/sentry-docs/pull/14113 This integration: - records tracing data of agent invocation, tool execution, ai client requests to LLMs, and handoffs to other agents. - captures input and output to/from LLMs if `set_default_pii=True`. - is mostly compatible to the OpenTelememetry `gen_ai` semantic conventions. (input and output is not compatible because Sentry does not have Span events. This information is stored in arrays on the Span attributes. - Captures errors that happen during agent execution (like problems during interaction with the LLM. This integration does not: - Capture errors during function tool exection because this is very hard to patch (see comment in the code) Example span tree in Sentry.io: ![Screenshot 2025-06-24 at 12 15 17](https://github.com/user-attachments/assets/87199067-434f-4bb9-b563-5c6fc18c56cb) --------- Co-authored-by: Ivana Kellyer --- .github/workflows/test-integrations-ai.yml | 8 + pyproject.toml | 4 + scripts/populate_tox/config.py | 6 + scripts/populate_tox/tox.jinja | 1 + .../split_tox_gh_actions.py | 1 + sentry_sdk/consts.py | 421 +++++++++---- sentry_sdk/integrations/__init__.py | 1 + .../integrations/openai_agents/__init__.py | 53 ++ .../integrations/openai_agents/consts.py | 1 + .../openai_agents/patches/__init__.py | 4 + .../openai_agents/patches/agent_run.py | 143 +++++ .../openai_agents/patches/models.py | 50 ++ .../openai_agents/patches/runner.py | 42 ++ .../openai_agents/patches/tools.py | 77 +++ .../openai_agents/spans/__init__.py | 5 + .../openai_agents/spans/agent_workflow.py | 21 + .../openai_agents/spans/ai_client.py | 38 ++ .../openai_agents/spans/execute_tool.py | 43 ++ .../openai_agents/spans/handoff.py | 19 + .../openai_agents/spans/invoke_agent.py | 34 + .../integrations/openai_agents/utils.py | 209 +++++++ tests/integrations/openai_agents/__init__.py | 3 + .../openai_agents/test_openai_agents.py | 580 ++++++++++++++++++ tox.ini | 8 +- 24 files changed, 1638 insertions(+), 134 deletions(-) create mode 100644 sentry_sdk/integrations/openai_agents/__init__.py create mode 100644 sentry_sdk/integrations/openai_agents/consts.py create mode 100644 sentry_sdk/integrations/openai_agents/patches/__init__.py create mode 100644 sentry_sdk/integrations/openai_agents/patches/agent_run.py create mode 100644 sentry_sdk/integrations/openai_agents/patches/models.py create mode 100644 sentry_sdk/integrations/openai_agents/patches/runner.py create mode 100644 sentry_sdk/integrations/openai_agents/patches/tools.py create mode 100644 sentry_sdk/integrations/openai_agents/spans/__init__.py create mode 100644 sentry_sdk/integrations/openai_agents/spans/agent_workflow.py create mode 100644 sentry_sdk/integrations/openai_agents/spans/ai_client.py create mode 100644 sentry_sdk/integrations/openai_agents/spans/execute_tool.py create mode 100644 sentry_sdk/integrations/openai_agents/spans/handoff.py create mode 100644 sentry_sdk/integrations/openai_agents/spans/invoke_agent.py create mode 100644 sentry_sdk/integrations/openai_agents/utils.py create mode 100644 tests/integrations/openai_agents/__init__.py create mode 100644 tests/integrations/openai_agents/test_openai_agents.py diff --git a/.github/workflows/test-integrations-ai.yml b/.github/workflows/test-integrations-ai.yml index 4aa0f36b77..e81d507d27 100644 --- a/.github/workflows/test-integrations-ai.yml +++ b/.github/workflows/test-integrations-ai.yml @@ -66,6 +66,10 @@ jobs: run: | set -x # print commands that are executed ./scripts/runtox.sh "py${{ matrix.python-version }}-openai-latest" + - name: Test openai_agents latest + run: | + set -x # print commands that are executed + ./scripts/runtox.sh "py${{ matrix.python-version }}-openai_agents-latest" - name: Test huggingface_hub latest run: | set -x # print commands that are executed @@ -141,6 +145,10 @@ jobs: run: | set -x # print commands that are executed ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-openai" + - name: Test openai_agents pinned + run: | + set -x # print commands that are executed + ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-openai_agents" - name: Test huggingface_hub pinned run: | set -x # print commands that are executed diff --git a/pyproject.toml b/pyproject.toml index 5e16b30793..e5eae2c21f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -183,6 +183,10 @@ ignore_missing_imports = true module = "grpc.*" ignore_missing_imports = true +[[tool.mypy.overrides]] +module = "agents.*" +ignore_missing_imports = true + # # Tool: Flake8 # diff --git a/scripts/populate_tox/config.py b/scripts/populate_tox/config.py index 4664845c7b..411d7fe666 100644 --- a/scripts/populate_tox/config.py +++ b/scripts/populate_tox/config.py @@ -139,6 +139,12 @@ "loguru": { "package": "loguru", }, + "openai_agents": { + "package": "openai-agents", + "deps": { + "*": ["pytest-asyncio"], + }, + }, "openfeature": { "package": "openfeature-sdk", }, diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index f95a913fd9..ac14bdb02a 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -400,6 +400,7 @@ setenv = litestar: TESTPATH=tests/integrations/litestar loguru: TESTPATH=tests/integrations/loguru openai: TESTPATH=tests/integrations/openai + openai_agents: TESTPATH=tests/integrations/openai_agents openfeature: TESTPATH=tests/integrations/openfeature opentelemetry: TESTPATH=tests/integrations/opentelemetry potel: TESTPATH=tests/integrations/opentelemetry 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 3fbc0ec1c5..af1ff84cd6 100755 --- a/scripts/split_tox_gh_actions/split_tox_gh_actions.py +++ b/scripts/split_tox_gh_actions/split_tox_gh_actions.py @@ -63,6 +63,7 @@ "cohere", "langchain", "openai", + "openai_agents", "huggingface_hub", ], "Cloud": [ diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 34ae5bdfd8..53148a36df 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -108,16 +108,39 @@ class SPANDATA: See: https://develop.sentry.dev/sdk/performance/span-data-conventions/ """ + AI_CITATIONS = "ai.citations" + """ + References or sources cited by the AI model in its response. + Example: ["Smith et al. 2020", "Jones 2019"] + """ + + AI_DOCUMENTS = "ai.documents" + """ + Documents or content chunks used as context for the AI model. + Example: ["doc1.txt", "doc2.pdf"] + """ + + AI_FINISH_REASON = "ai.finish_reason" + """ + The reason why the model stopped generating. + Example: "length" + """ + AI_FREQUENCY_PENALTY = "ai.frequency_penalty" """ Used to reduce repetitiveness of generated tokens. Example: 0.5 """ - AI_PRESENCE_PENALTY = "ai.presence_penalty" + AI_FUNCTION_CALL = "ai.function_call" """ - Used to reduce repetitiveness of generated tokens. - Example: 0.5 + For an AI model call, the function that was called. This is deprecated for OpenAI, and replaced by tool_calls + """ + + AI_GENERATION_ID = "ai.generation_id" + """ + Unique identifier for the completion. + Example: "gen_123abc" """ AI_INPUT_MESSAGES = "ai.input_messages" @@ -126,10 +149,9 @@ class SPANDATA: Example: [{"role": "user", "message": "hello"}] """ - AI_MODEL_ID = "ai.model_id" + AI_LOGIT_BIAS = "ai.logit_bias" """ - The unique descriptor of the model being execugted - Example: gpt-4 + For an AI model call, the logit bias """ AI_METADATA = "ai.metadata" @@ -138,28 +160,94 @@ class SPANDATA: Example: {"executed_function": "add_integers"} """ - AI_TAGS = "ai.tags" + AI_MODEL_ID = "ai.model_id" """ - Tags that describe an AI pipeline step. - Example: {"executed_function": "add_integers"} + The unique descriptor of the model being execugted + Example: gpt-4 + """ + + AI_PIPELINE_NAME = "ai.pipeline.name" + """ + Name of the AI pipeline or chain being executed. + Example: "qa-pipeline" + """ + + AI_PREAMBLE = "ai.preamble" + """ + For an AI model call, the preamble parameter. + Preambles are a part of the prompt used to adjust the model's overall behavior and conversation style. + Example: "You are now a clown." + """ + + AI_PRESENCE_PENALTY = "ai.presence_penalty" + """ + Used to reduce repetitiveness of generated tokens. + Example: 0.5 + """ + + AI_RAW_PROMPTING = "ai.raw_prompting" + """ + Minimize pre-processing done to the prompt sent to the LLM. + Example: true + """ + + AI_RESPONSE_FORMAT = "ai.response_format" + """ + For an AI model call, the format of the response + """ + + AI_RESPONSES = "ai.responses" + """ + The responses to an AI model call. Always as a list. + Example: ["hello", "world"] + """ + + AI_SEARCH_QUERIES = "ai.search_queries" + """ + Queries used to search for relevant context or documents. + Example: ["climate change effects", "renewable energy"] + """ + + AI_SEARCH_REQUIRED = "ai.is_search_required" + """ + Boolean indicating if the model needs to perform a search. + Example: true + """ + + AI_SEARCH_RESULTS = "ai.search_results" + """ + Results returned from search queries for context. + Example: ["Result 1", "Result 2"] + """ + + AI_SEED = "ai.seed" + """ + The seed, ideally models given the same seed and same other parameters will produce the exact same output. + Example: 123.45 """ AI_STREAMING = "ai.streaming" """ - Whether or not the AI model call's repsonse was streamed back asynchronously + Whether or not the AI model call's response was streamed back asynchronously Example: true """ + AI_TAGS = "ai.tags" + """ + Tags that describe an AI pipeline step. + Example: {"executed_function": "add_integers"} + """ + AI_TEMPERATURE = "ai.temperature" """ For an AI model call, the temperature parameter. Temperature essentially means how random the output will be. Example: 0.5 """ - AI_TOP_P = "ai.top_p" + AI_TEXTS = "ai.texts" """ - For an AI model call, the top_p parameter. Top_p essentially controls how random the output will be. - Example: 0.5 + Raw text inputs provided to the model. + Example: ["What is machine learning?"] """ AI_TOP_K = "ai.top_k" @@ -168,9 +256,10 @@ class SPANDATA: Example: 35 """ - AI_FUNCTION_CALL = "ai.function_call" + AI_TOP_P = "ai.top_p" """ - For an AI model call, the function that was called. This is deprecated for OpenAI, and replaced by tool_calls + For an AI model call, the top_p parameter. Top_p essentially controls how random the output will be. + Example: 0.5 """ AI_TOOL_CALLS = "ai.tool_calls" @@ -183,168 +272,236 @@ class SPANDATA: For an AI model call, the functions that are available """ - AI_RESPONSE_FORMAT = "ai.response_format" + AI_WARNINGS = "ai.warnings" """ - For an AI model call, the format of the response + Warning messages generated during model execution. + Example: ["Token limit exceeded"] """ - AI_LOGIT_BIAS = "ai.logit_bias" + CACHE_HIT = "cache.hit" """ - For an AI model call, the logit bias + A boolean indicating whether the requested data was found in the cache. + Example: true """ - AI_PREAMBLE = "ai.preamble" + CACHE_ITEM_SIZE = "cache.item_size" """ - For an AI model call, the preamble parameter. - Preambles are a part of the prompt used to adjust the model's overall behavior and conversation style. - Example: "You are now a clown." + The size of the requested data in bytes. + Example: 58 """ - AI_RAW_PROMPTING = "ai.raw_prompting" + CACHE_KEY = "cache.key" """ - Minimize pre-processing done to the prompt sent to the LLM. - Example: true + The key of the requested data. + Example: template.cache.some_item.867da7e2af8e6b2f3aa7213a4080edb3 """ - AI_RESPONSES = "ai.responses" + + CODE_FILEPATH = "code.filepath" """ - The responses to an AI model call. Always as a list. - Example: ["hello", "world"] + The source code file name that identifies the code unit as uniquely as possible (preferably an absolute file path). + Example: "/app/myapplication/http/handler/server.py" """ - AI_SEED = "ai.seed" + CODE_FUNCTION = "code.function" """ - The seed, ideally models given the same seed and same other parameters will produce the exact same output. - Example: 123.45 + The method or function name, or equivalent (usually rightmost part of the code unit's name). + Example: "server_request" """ - AI_CITATIONS = "ai.citations" + CODE_LINENO = "code.lineno" """ - References or sources cited by the AI model in its response. - Example: ["Smith et al. 2020", "Jones 2019"] + The line number in `code.filepath` best representing the operation. It SHOULD point within the code unit named in `code.function`. + Example: 42 """ - AI_DOCUMENTS = "ai.documents" + CODE_NAMESPACE = "code.namespace" """ - Documents or content chunks used as context for the AI model. - Example: ["doc1.txt", "doc2.pdf"] + The "namespace" within which `code.function` is defined. Usually the qualified class or module name, such that `code.namespace` + some separator + `code.function` form a unique identifier for the code unit. + Example: "http.handler" """ - AI_SEARCH_QUERIES = "ai.search_queries" + DB_MONGODB_COLLECTION = "db.mongodb.collection" """ - Queries used to search for relevant context or documents. - Example: ["climate change effects", "renewable energy"] + The MongoDB collection being accessed within the database. + See: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/mongodb.md#attributes + Example: public.users; customers """ - AI_SEARCH_RESULTS = "ai.search_results" + DB_NAME = "db.name" """ - Results returned from search queries for context. - Example: ["Result 1", "Result 2"] + The name of the database being accessed. For commands that switch the database, this should be set to the target database (even if the command fails). + Example: myDatabase """ - AI_GENERATION_ID = "ai.generation_id" + DB_OPERATION = "db.operation" """ - Unique identifier for the completion. - Example: "gen_123abc" + The name of the operation being executed, e.g. the MongoDB command name such as findAndModify, or the SQL keyword. + See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md + Example: findAndModify, HMSET, SELECT """ - AI_SEARCH_REQUIRED = "ai.is_search_required" + DB_SYSTEM = "db.system" """ - Boolean indicating if the model needs to perform a search. - Example: true + An identifier for the database management system (DBMS) product being used. + See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md + Example: postgresql """ - AI_FINISH_REASON = "ai.finish_reason" + DB_USER = "db.user" """ - The reason why the model stopped generating. - Example: "length" + The name of the database user used for connecting to the database. + See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md + Example: my_user """ - AI_PIPELINE_NAME = "ai.pipeline.name" + GEN_AI_AGENT_NAME = "gen_ai.agent.name" """ - Name of the AI pipeline or chain being executed. - Example: "qa-pipeline" + The name of the agent being used. + Example: "ResearchAssistant" """ - AI_TEXTS = "ai.texts" + GEN_AI_CHOICE = "gen_ai.choice" """ - Raw text inputs provided to the model. - Example: ["What is machine learning?"] + The model's response message. + Example: "The weather in Paris is rainy and overcast, with temperatures around 57°F" """ - AI_WARNINGS = "ai.warnings" + GEN_AI_OPERATION_NAME = "gen_ai.operation.name" """ - Warning messages generated during model execution. - Example: ["Token limit exceeded"] + The name of the operation being performed. + Example: "chat" """ - DB_NAME = "db.name" + GEN_AI_RESPONSE_TEXT = "gen_ai.response.text" """ - The name of the database being accessed. For commands that switch the database, this should be set to the target database (even if the command fails). - Example: myDatabase + The model's response text messages. + Example: ["The weather in Paris is rainy and overcast, with temperatures around 57°F", "The weather in London is sunny and warm, with temperatures around 65°F"] """ - DB_USER = "db.user" + GEN_AI_RESPONSE_TOOL_CALLS = "gen_ai.response.tool_calls" """ - The name of the database user used for connecting to the database. - See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md - Example: my_user + The tool calls in the model's response. + Example: [{"name": "get_weather", "arguments": {"location": "Paris"}}] """ - DB_OPERATION = "db.operation" + GEN_AI_REQUEST_AVAILABLE_TOOLS = "gen_ai.request.available_tools" """ - The name of the operation being executed, e.g. the MongoDB command name such as findAndModify, or the SQL keyword. - See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md - Example: findAndModify, HMSET, SELECT + The available tools for the model. + Example: [{"name": "get_weather", "description": "Get the weather for a given location"}, {"name": "get_news", "description": "Get the news for a given topic"}] """ - DB_SYSTEM = "db.system" + GEN_AI_REQUEST_FREQUENCY_PENALTY = "gen_ai.request.frequency_penalty" """ - An identifier for the database management system (DBMS) product being used. - See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md - Example: postgresql + The frequency penalty parameter used to reduce repetitiveness of generated tokens. + Example: 0.1 """ - DB_MONGODB_COLLECTION = "db.mongodb.collection" + GEN_AI_REQUEST_MAX_TOKENS = "gen_ai.request.max_tokens" """ - The MongoDB collection being accessed within the database. - See: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/mongodb.md#attributes - Example: public.users; customers + The maximum number of tokens to generate in the response. + Example: 2048 """ - CACHE_HIT = "cache.hit" + GEN_AI_REQUEST_MESSAGES = "gen_ai.request.messages" """ - A boolean indicating whether the requested data was found in the cache. - Example: true + The messages passed to the model. The "content" can be a string or an array of objects. + Example: [{role: "system", "content: "Generate a random number."}, {"role": "user", "content": [{"text": "Generate a random number between 0 and 10.", "type": "text"}]}] """ - CACHE_ITEM_SIZE = "cache.item_size" + GEN_AI_REQUEST_MODEL = "gen_ai.request.model" """ - The size of the requested data in bytes. - Example: 58 + The model identifier being used for the request. + Example: "gpt-4-turbo-preview" """ - CACHE_KEY = "cache.key" + GEN_AI_REQUEST_PRESENCE_PENALTY = "gen_ai.request.presence_penalty" """ - The key of the requested data. - Example: template.cache.some_item.867da7e2af8e6b2f3aa7213a4080edb3 + The presence penalty parameter used to reduce repetitiveness of generated tokens. + Example: 0.1 """ - NETWORK_PEER_ADDRESS = "network.peer.address" + GEN_AI_REQUEST_TEMPERATURE = "gen_ai.request.temperature" """ - Peer address of the network connection - IP address or Unix domain socket name. - Example: 10.1.2.80, /tmp/my.sock, localhost + The temperature parameter used to control randomness in the output. + Example: 0.7 """ - NETWORK_PEER_PORT = "network.peer.port" + GEN_AI_REQUEST_TOP_P = "gen_ai.request.top_p" """ - Peer port number of the network connection. - Example: 6379 + The top_p parameter used to control diversity via nucleus sampling. + Example: 1.0 """ - HTTP_QUERY = "http.query" + GEN_AI_SYSTEM = "gen_ai.system" """ - The Query string present in the URL. - Example: ?foo=bar&bar=baz + The name of the AI system being used. + Example: "openai" + """ + + GEN_AI_TOOL_DESCRIPTION = "gen_ai.tool.description" + """ + The description of the tool being used. + Example: "Searches the web for current information about a topic" + """ + + GEN_AI_TOOL_INPUT = "gen_ai.tool.input" + """ + The input of the tool being used. + Example: {"location": "Paris"} + """ + + GEN_AI_TOOL_NAME = "gen_ai.tool.name" + """ + The name of the tool being used. + Example: "web_search" + """ + + GEN_AI_TOOL_OUTPUT = "gen_ai.tool.output" + """ + The output of the tool being used. + Example: "rainy, 57°F" + """ + + GEN_AI_TOOL_TYPE = "gen_ai.tool.type" + """ + The type of tool being used. + Example: "function" + """ + + GEN_AI_USAGE_INPUT_TOKENS = "gen_ai.usage.input_tokens" + """ + The number of tokens in the input. + Example: 150 + """ + + GEN_AI_USAGE_INPUT_TOKENS_CACHED = "gen_ai.usage.input_tokens.cached" + """ + The number of cached tokens in the input. + Example: 50 + """ + + GEN_AI_USAGE_OUTPUT_TOKENS = "gen_ai.usage.output_tokens" + """ + The number of tokens in the output. + Example: 250 + """ + + GEN_AI_USAGE_OUTPUT_TOKENS_REASONING = "gen_ai.usage.output_tokens.reasoning" + """ + The number of tokens used for reasoning in the output. + Example: 75 + """ + + GEN_AI_USAGE_TOTAL_TOKENS = "gen_ai.usage.total_tokens" + """ + The total number of tokens used (input + output). + Example: 400 + """ + + GEN_AI_USER_MESSAGE = "gen_ai.user.message" + """ + The user message passed to the model. + Example: "What's the weather in Paris?" """ HTTP_FRAGMENT = "http.fragment" @@ -359,6 +516,12 @@ class SPANDATA: Example: GET """ + HTTP_QUERY = "http.query" + """ + The Query string present in the URL. + Example: ?foo=bar&bar=baz + """ + HTTP_STATUS_CODE = "http.response.status_code" """ The HTTP status code as an integer. @@ -376,14 +539,14 @@ class SPANDATA: The message's identifier. """ - MESSAGING_MESSAGE_RETRY_COUNT = "messaging.message.retry.count" + MESSAGING_MESSAGE_RECEIVE_LATENCY = "messaging.message.receive.latency" """ - Number of retries/attempts to process a message. + The latency between when the task was enqueued and when it was started to be processed. """ - MESSAGING_MESSAGE_RECEIVE_LATENCY = "messaging.message.receive.latency" + MESSAGING_MESSAGE_RETRY_COUNT = "messaging.message.retry.count" """ - The latency between when the task was enqueued and when it was started to be processed. + Number of retries/attempts to process a message. """ MESSAGING_SYSTEM = "messaging.system" @@ -391,6 +554,24 @@ class SPANDATA: The messaging system's name, e.g. `kafka`, `aws_sqs` """ + NETWORK_PEER_ADDRESS = "network.peer.address" + """ + Peer address of the network connection - IP address or Unix domain socket name. + Example: 10.1.2.80, /tmp/my.sock, localhost + """ + + NETWORK_PEER_PORT = "network.peer.port" + """ + Peer port number of the network connection. + Example: 6379 + """ + + PROFILER_ID = "profiler_id" + """ + Label identifying the profiler id that the span occurred in. This should be a string. + Example: "5249fbada8d5416482c2f6e47e337372" + """ + SERVER_ADDRESS = "server.address" """ Name of the database host. @@ -416,30 +597,6 @@ class SPANDATA: Example: 16456 """ - CODE_FILEPATH = "code.filepath" - """ - The source code file name that identifies the code unit as uniquely as possible (preferably an absolute file path). - Example: "/app/myapplication/http/handler/server.py" - """ - - CODE_LINENO = "code.lineno" - """ - The line number in `code.filepath` best representing the operation. It SHOULD point within the code unit named in `code.function`. - Example: 42 - """ - - CODE_FUNCTION = "code.function" - """ - The method or function name, or equivalent (usually rightmost part of the code unit's name). - Example: "server_request" - """ - - CODE_NAMESPACE = "code.namespace" - """ - The "namespace" within which `code.function` is defined. Usually the qualified class or module name, such that `code.namespace` + some separator + `code.function` form a unique identifier for the code unit. - Example: "http.handler" - """ - THREAD_ID = "thread.id" """ Identifier of a thread from where the span originated. This should be a string. @@ -452,12 +609,6 @@ class SPANDATA: Example: "MainThread" """ - PROFILER_ID = "profiler_id" - """ - Label identifying the profiler id that the span occurred in. This should be a string. - Example: "5249fbada8d5416482c2f6e47e337372" - """ - class SPANSTATUS: """ @@ -497,6 +648,10 @@ class OP: FUNCTION = "function" FUNCTION_AWS = "function.aws" FUNCTION_GCP = "function.gcp" + GEN_AI_CHAT = "gen_ai.chat" + GEN_AI_EXECUTE_TOOL = "gen_ai.execute_tool" + GEN_AI_HANDOFF = "gen_ai.handoff" + GEN_AI_INVOKE_AGENT = "gen_ai.invoke_agent" GRAPHQL_EXECUTE = "graphql.execute" GRAPHQL_MUTATION = "graphql.mutation" GRAPHQL_PARSE = "graphql.parse" diff --git a/sentry_sdk/integrations/__init__.py b/sentry_sdk/integrations/__init__.py index 118289950c..e2eadd523d 100644 --- a/sentry_sdk/integrations/__init__.py +++ b/sentry_sdk/integrations/__init__.py @@ -145,6 +145,7 @@ def iter_default_integrations(with_auto_enabling_integrations): "launchdarkly": (9, 8, 0), "loguru": (0, 7, 0), "openai": (1, 0, 0), + "openai_agents": (0, 0, 19), "openfeature": (0, 7, 1), "quart": (0, 16, 0), "ray": (2, 7, 0), diff --git a/sentry_sdk/integrations/openai_agents/__init__.py b/sentry_sdk/integrations/openai_agents/__init__.py new file mode 100644 index 0000000000..06b6459441 --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/__init__.py @@ -0,0 +1,53 @@ +from sentry_sdk.integrations import DidNotEnable, Integration + +from .patches import ( + _create_get_model_wrapper, + _create_get_all_tools_wrapper, + _create_run_wrapper, + _patch_agent_run, +) + +try: + import agents + +except ImportError: + raise DidNotEnable("OpenAI Agents not installed") + + +def _patch_runner(): + # type: () -> None + # Create the root span for one full agent run (including eventual handoffs) + # Note agents.run.DEFAULT_AGENT_RUNNER.run_sync is a wrapper around + # agents.run.DEFAULT_AGENT_RUNNER.run. It does not need to be wrapped separately. + # TODO-anton: Also patch streaming runner: agents.Runner.run_streamed + agents.run.DEFAULT_AGENT_RUNNER.run = _create_run_wrapper( + agents.run.DEFAULT_AGENT_RUNNER.run + ) + + # Creating the actual spans for each agent run. + _patch_agent_run() + + +def _patch_model(): + # type: () -> None + agents.run.AgentRunner._get_model = classmethod( + _create_get_model_wrapper(agents.run.AgentRunner._get_model), + ) + + +def _patch_tools(): + # type: () -> None + agents.run.AgentRunner._get_all_tools = classmethod( + _create_get_all_tools_wrapper(agents.run.AgentRunner._get_all_tools), + ) + + +class OpenAIAgentsIntegration(Integration): + identifier = "openai_agents" + + @staticmethod + def setup_once(): + # type: () -> None + _patch_tools() + _patch_model() + _patch_runner() diff --git a/sentry_sdk/integrations/openai_agents/consts.py b/sentry_sdk/integrations/openai_agents/consts.py new file mode 100644 index 0000000000..f5de978be0 --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/consts.py @@ -0,0 +1 @@ +SPAN_ORIGIN = "auto.ai.openai_agents" diff --git a/sentry_sdk/integrations/openai_agents/patches/__init__.py b/sentry_sdk/integrations/openai_agents/patches/__init__.py new file mode 100644 index 0000000000..06bb1711f8 --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/patches/__init__.py @@ -0,0 +1,4 @@ +from .models import _create_get_model_wrapper # noqa: F401 +from .tools import _create_get_all_tools_wrapper # noqa: F401 +from .runner import _create_run_wrapper # noqa: F401 +from .agent_run import _patch_agent_run # noqa: F401 diff --git a/sentry_sdk/integrations/openai_agents/patches/agent_run.py b/sentry_sdk/integrations/openai_agents/patches/agent_run.py new file mode 100644 index 0000000000..084100878c --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/patches/agent_run.py @@ -0,0 +1,143 @@ +from functools import wraps + +from sentry_sdk.integrations import DidNotEnable + +from ..spans import invoke_agent_span, update_invoke_agent_span, handoff_span + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any, Optional + + +try: + import agents +except ImportError: + raise DidNotEnable("OpenAI Agents not installed") + + +def _patch_agent_run(): + # type: () -> None + """ + Patches AgentRunner methods to create agent invocation spans. + This directly patches the execution flow to track when agents start and stop. + """ + + # Store original methods + original_run_single_turn = agents.run.AgentRunner._run_single_turn + original_execute_handoffs = agents._run_impl.RunImpl.execute_handoffs + original_execute_final_output = agents._run_impl.RunImpl.execute_final_output + + def _start_invoke_agent_span(context_wrapper, agent): + # type: (agents.RunContextWrapper, agents.Agent) -> None + """Start an agent invocation span""" + # Store the agent on the context wrapper so we can access it later + context_wrapper._sentry_current_agent = agent + invoke_agent_span(context_wrapper, agent) + + def _end_invoke_agent_span(context_wrapper, agent, output=None): + # type: (agents.RunContextWrapper, agents.Agent, Optional[Any]) -> None + """End the agent invocation span""" + # Clear the stored agent + if hasattr(context_wrapper, "_sentry_current_agent"): + delattr(context_wrapper, "_sentry_current_agent") + + update_invoke_agent_span(context_wrapper, agent, output) + + def _has_active_agent_span(context_wrapper): + # type: (agents.RunContextWrapper) -> bool + """Check if there's an active agent span for this context""" + return getattr(context_wrapper, "_sentry_current_agent", None) is not None + + def _get_current_agent(context_wrapper): + # type: (agents.RunContextWrapper) -> Optional[agents.Agent] + """Get the current agent from context wrapper""" + return getattr(context_wrapper, "_sentry_current_agent", None) + + @wraps( + original_run_single_turn.__func__ + if hasattr(original_run_single_turn, "__func__") + else original_run_single_turn + ) + async def patched_run_single_turn(cls, *args, **kwargs): + # type: (agents.Runner, *Any, **Any) -> Any + """Patched _run_single_turn that creates agent invocation spans""" + + agent = kwargs.get("agent") + context_wrapper = kwargs.get("context_wrapper") + should_run_agent_start_hooks = kwargs.get("should_run_agent_start_hooks") + + # Start agent span when agent starts (but only once per agent) + if should_run_agent_start_hooks and agent and context_wrapper: + # End any existing span for a different agent + if _has_active_agent_span(context_wrapper): + current_agent = _get_current_agent(context_wrapper) + if current_agent and current_agent != agent: + _end_invoke_agent_span(context_wrapper, current_agent) + + _start_invoke_agent_span(context_wrapper, agent) + + # Call original method with all the correct parameters + result = await original_run_single_turn(*args, **kwargs) + + return result + + @wraps( + original_execute_handoffs.__func__ + if hasattr(original_execute_handoffs, "__func__") + else original_execute_handoffs + ) + async def patched_execute_handoffs(cls, *args, **kwargs): + # type: (agents.Runner, *Any, **Any) -> Any + """Patched execute_handoffs that creates handoff spans and ends agent span for handoffs""" + + context_wrapper = kwargs.get("context_wrapper") + run_handoffs = kwargs.get("run_handoffs") + agent = kwargs.get("agent") + + # Create Sentry handoff span for the first handoff (agents library only processes the first one) + if run_handoffs: + first_handoff = run_handoffs[0] + handoff_agent_name = first_handoff.handoff.agent_name + handoff_span(context_wrapper, agent, handoff_agent_name) + + # Call original method with all parameters + try: + result = await original_execute_handoffs(*args, **kwargs) + + finally: + # End span for current agent after handoff processing is complete + if agent and context_wrapper and _has_active_agent_span(context_wrapper): + _end_invoke_agent_span(context_wrapper, agent) + + return result + + @wraps( + original_execute_final_output.__func__ + if hasattr(original_execute_final_output, "__func__") + else original_execute_final_output + ) + async def patched_execute_final_output(cls, *args, **kwargs): + # type: (agents.Runner, *Any, **Any) -> Any + """Patched execute_final_output that ends agent span for final outputs""" + + agent = kwargs.get("agent") + context_wrapper = kwargs.get("context_wrapper") + final_output = kwargs.get("final_output") + + # Call original method with all parameters + try: + result = await original_execute_final_output(*args, **kwargs) + finally: + # End span for current agent after final output processing is complete + if agent and context_wrapper and _has_active_agent_span(context_wrapper): + _end_invoke_agent_span(context_wrapper, agent, final_output) + + return result + + # Apply patches + agents.run.AgentRunner._run_single_turn = classmethod(patched_run_single_turn) + agents._run_impl.RunImpl.execute_handoffs = classmethod(patched_execute_handoffs) + agents._run_impl.RunImpl.execute_final_output = classmethod( + patched_execute_final_output + ) diff --git a/sentry_sdk/integrations/openai_agents/patches/models.py b/sentry_sdk/integrations/openai_agents/patches/models.py new file mode 100644 index 0000000000..e6f24da6a1 --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/patches/models.py @@ -0,0 +1,50 @@ +from functools import wraps + +from sentry_sdk.integrations import DidNotEnable + +from ..spans import ai_client_span, update_ai_client_span + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any, Callable + + +try: + import agents +except ImportError: + raise DidNotEnable("OpenAI Agents not installed") + + +def _create_get_model_wrapper(original_get_model): + # type: (Callable[..., Any]) -> Callable[..., Any] + """ + Wraps the agents.Runner._get_model method to wrap the get_response method of the model to create a AI client span. + """ + + @wraps( + original_get_model.__func__ + if hasattr(original_get_model, "__func__") + else original_get_model + ) + def wrapped_get_model(cls, agent, run_config): + # type: (agents.Runner, agents.Agent, agents.RunConfig) -> agents.Model + + model = original_get_model(agent, run_config) + original_get_response = model.get_response + + @wraps(original_get_response) + async def wrapped_get_response(*args, **kwargs): + # type: (*Any, **Any) -> Any + with ai_client_span(agent, kwargs) as span: + result = await original_get_response(*args, **kwargs) + + update_ai_client_span(span, agent, kwargs, result) + + return result + + model.get_response = wrapped_get_response + + return model + + return wrapped_get_model diff --git a/sentry_sdk/integrations/openai_agents/patches/runner.py b/sentry_sdk/integrations/openai_agents/patches/runner.py new file mode 100644 index 0000000000..e1e9a3b50c --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/patches/runner.py @@ -0,0 +1,42 @@ +from functools import wraps + +import sentry_sdk + +from ..spans import agent_workflow_span +from ..utils import _capture_exception + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any, Callable + + +def _create_run_wrapper(original_func): + # type: (Callable[..., Any]) -> Callable[..., Any] + """ + Wraps the agents.Runner.run methods to create a root span for the agent workflow runs. + + Note agents.Runner.run_sync() is a wrapper around agents.Runner.run(), + so it does not need to be wrapped separately. + """ + + @wraps(original_func) + async def wrapper(*args, **kwargs): + # type: (*Any, **Any) -> Any + agent = args[0] + with agent_workflow_span(agent): + result = None + try: + result = await original_func(*args, **kwargs) + return result + except Exception as exc: + _capture_exception(exc) + + # It could be that there is a "invoke agent" span still open + current_span = sentry_sdk.get_current_span() + if current_span is not None and current_span.timestamp is None: + current_span.__exit__(None, None, None) + + raise exc from None + + return wrapper diff --git a/sentry_sdk/integrations/openai_agents/patches/tools.py b/sentry_sdk/integrations/openai_agents/patches/tools.py new file mode 100644 index 0000000000..b359d32678 --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/patches/tools.py @@ -0,0 +1,77 @@ +from functools import wraps + +from sentry_sdk.integrations import DidNotEnable + +from ..spans import execute_tool_span, update_execute_tool_span + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any, Callable + +try: + import agents +except ImportError: + raise DidNotEnable("OpenAI Agents not installed") + + +def _create_get_all_tools_wrapper(original_get_all_tools): + # type: (Callable[..., Any]) -> Callable[..., Any] + """ + Wraps the agents.Runner._get_all_tools method of the Runner class to wrap all function tools with Sentry instrumentation. + """ + + @wraps( + original_get_all_tools.__func__ + if hasattr(original_get_all_tools, "__func__") + else original_get_all_tools + ) + async def wrapped_get_all_tools(cls, agent, context_wrapper): + # type: (agents.Runner, agents.Agent, agents.RunContextWrapper) -> list[agents.Tool] + + # Get the original tools + tools = await original_get_all_tools(agent, context_wrapper) + + wrapped_tools = [] + for tool in tools: + # Wrap only the function tools (for now) + if tool.__class__.__name__ != "FunctionTool": + wrapped_tools.append(tool) + continue + + # Create a new FunctionTool with our wrapped invoke method + original_on_invoke = tool.on_invoke_tool + + def create_wrapped_invoke(current_tool, current_on_invoke): + # type: (agents.Tool, Callable[..., Any]) -> Callable[..., Any] + @wraps(current_on_invoke) + async def sentry_wrapped_on_invoke_tool(*args, **kwargs): + # type: (*Any, **Any) -> Any + with execute_tool_span(current_tool, *args, **kwargs) as span: + # We can not capture exceptions in tool execution here because + # `_on_invoke_tool` is swallowing the exception here: + # https://github.com/openai/openai-agents-python/blob/main/src/agents/tool.py#L409-L422 + # And because function_tool is a decorator with `default_tool_error_function` set as a default parameter + # I was unable to monkey patch it because those are evaluated at module import time + # and the SDK is too late to patch it. I was also unable to patch `_on_invoke_tool_impl` + # because it is nested inside this import time code. As if they made it hard to patch on purpose... + result = await current_on_invoke(*args, **kwargs) + update_execute_tool_span(span, agent, current_tool, result) + + return result + + return sentry_wrapped_on_invoke_tool + + wrapped_tool = agents.FunctionTool( + name=tool.name, + description=tool.description, + params_json_schema=tool.params_json_schema, + on_invoke_tool=create_wrapped_invoke(tool, original_on_invoke), + strict_json_schema=tool.strict_json_schema, + is_enabled=tool.is_enabled, + ) + wrapped_tools.append(wrapped_tool) + + return wrapped_tools + + return wrapped_get_all_tools diff --git a/sentry_sdk/integrations/openai_agents/spans/__init__.py b/sentry_sdk/integrations/openai_agents/spans/__init__.py new file mode 100644 index 0000000000..3bc453cafa --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/spans/__init__.py @@ -0,0 +1,5 @@ +from .agent_workflow import agent_workflow_span # noqa: F401 +from .ai_client import ai_client_span, update_ai_client_span # noqa: F401 +from .execute_tool import execute_tool_span, update_execute_tool_span # noqa: F401 +from .handoff import handoff_span # noqa: F401 +from .invoke_agent import invoke_agent_span, update_invoke_agent_span # noqa: F401 diff --git a/sentry_sdk/integrations/openai_agents/spans/agent_workflow.py b/sentry_sdk/integrations/openai_agents/spans/agent_workflow.py new file mode 100644 index 0000000000..de2f28d41e --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/spans/agent_workflow.py @@ -0,0 +1,21 @@ +import sentry_sdk + +from ..consts import SPAN_ORIGIN +from ..utils import _get_start_span_function + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import agents + + +def agent_workflow_span(agent): + # type: (agents.Agent) -> sentry_sdk.tracing.Span + + # Create a transaction or a span if an transaction is already active + span = _get_start_span_function()( + name=f"{agent.name} workflow", + origin=SPAN_ORIGIN, + ) + + return span diff --git a/sentry_sdk/integrations/openai_agents/spans/ai_client.py b/sentry_sdk/integrations/openai_agents/spans/ai_client.py new file mode 100644 index 0000000000..30c5fd1dac --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/spans/ai_client.py @@ -0,0 +1,38 @@ +import sentry_sdk +from sentry_sdk.consts import OP, SPANDATA + +from ..consts import SPAN_ORIGIN +from ..utils import ( + _set_agent_data, + _set_input_data, + _set_output_data, + _set_usage_data, +) + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from agents import Agent + from typing import Any + + +def ai_client_span(agent, get_response_kwargs): + # type: (Agent, dict[str, Any]) -> sentry_sdk.tracing.Span + # TODO-anton: implement other types of operations. Now "chat" is hardcoded. + span = sentry_sdk.start_span( + op=OP.GEN_AI_CHAT, + description=f"chat {agent.model}", + origin=SPAN_ORIGIN, + ) + # TODO-anton: remove hardcoded stuff and replace something that also works for embedding and so on + span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat") + + return span + + +def update_ai_client_span(span, agent, get_response_kwargs, result): + # type: (sentry_sdk.tracing.Span, Agent, dict[str, Any], Any) -> None + _set_agent_data(span, agent) + _set_usage_data(span, result.usage) + _set_input_data(span, get_response_kwargs) + _set_output_data(span, result) diff --git a/sentry_sdk/integrations/openai_agents/spans/execute_tool.py b/sentry_sdk/integrations/openai_agents/spans/execute_tool.py new file mode 100644 index 0000000000..e6e880b64c --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/spans/execute_tool.py @@ -0,0 +1,43 @@ +import sentry_sdk +from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.scope import should_send_default_pii + +from ..consts import SPAN_ORIGIN +from ..utils import _set_agent_data + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import agents + from typing import Any + + +def execute_tool_span(tool, *args, **kwargs): + # type: (agents.Tool, *Any, **Any) -> sentry_sdk.tracing.Span + span = sentry_sdk.start_span( + op=OP.GEN_AI_EXECUTE_TOOL, + name=f"execute_tool {tool.name}", + origin=SPAN_ORIGIN, + ) + + span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "execute_tool") + + if tool.__class__.__name__ == "FunctionTool": + span.set_data(SPANDATA.GEN_AI_TOOL_TYPE, "function") + + span.set_data(SPANDATA.GEN_AI_TOOL_NAME, tool.name) + span.set_data(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool.description) + + if should_send_default_pii(): + input = args[1] + span.set_data(SPANDATA.GEN_AI_TOOL_INPUT, input) + + return span + + +def update_execute_tool_span(span, agent, tool, result): + # type: (sentry_sdk.tracing.Span, agents.Agent, agents.Tool, Any) -> None + _set_agent_data(span, agent) + + if should_send_default_pii(): + span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, result) diff --git a/sentry_sdk/integrations/openai_agents/spans/handoff.py b/sentry_sdk/integrations/openai_agents/spans/handoff.py new file mode 100644 index 0000000000..78e6788c7d --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/spans/handoff.py @@ -0,0 +1,19 @@ +import sentry_sdk +from sentry_sdk.consts import OP, SPANDATA + +from ..consts import SPAN_ORIGIN + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import agents + + +def handoff_span(context, from_agent, to_agent_name): + # type: (agents.RunContextWrapper, agents.Agent, str) -> None + with sentry_sdk.start_span( + op=OP.GEN_AI_HANDOFF, + name=f"handoff from {from_agent.name} to {to_agent_name}", + origin=SPAN_ORIGIN, + ) as span: + span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "handoff") diff --git a/sentry_sdk/integrations/openai_agents/spans/invoke_agent.py b/sentry_sdk/integrations/openai_agents/spans/invoke_agent.py new file mode 100644 index 0000000000..549ade1246 --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/spans/invoke_agent.py @@ -0,0 +1,34 @@ +import sentry_sdk +from sentry_sdk.consts import OP, SPANDATA + +from ..consts import SPAN_ORIGIN +from ..utils import _set_agent_data + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import agents + from typing import Any + + +def invoke_agent_span(context, agent): + # type: (agents.RunContextWrapper, agents.Agent) -> sentry_sdk.tracing.Span + span = sentry_sdk.start_span( + op=OP.GEN_AI_INVOKE_AGENT, + name=f"invoke_agent {agent.name}", + origin=SPAN_ORIGIN, + ) + span.__enter__() + + span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent") + + _set_agent_data(span, agent) + + return span + + +def update_invoke_agent_span(context, agent, output): + # type: (agents.RunContextWrapper, agents.Agent, Any) -> None + current_span = sentry_sdk.get_current_span() + if current_span: + current_span.__exit__(None, None, None) diff --git a/sentry_sdk/integrations/openai_agents/utils.py b/sentry_sdk/integrations/openai_agents/utils.py new file mode 100644 index 0000000000..28dbd6bb75 --- /dev/null +++ b/sentry_sdk/integrations/openai_agents/utils.py @@ -0,0 +1,209 @@ +import json +import sentry_sdk +from sentry_sdk.consts import SPANDATA +from sentry_sdk.integrations import DidNotEnable +from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.utils import event_from_exception + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any + from typing import Callable + from typing import Union + from agents import Usage + +try: + import agents + +except ImportError: + raise DidNotEnable("OpenAI Agents not installed") + + +def _capture_exception(exc): + # type: (Any) -> None + event, hint = event_from_exception( + exc, + client_options=sentry_sdk.get_client().options, + mechanism={"type": "openai_agents", "handled": False}, + ) + sentry_sdk.capture_event(event, hint=hint) + + +def _get_start_span_function(): + # type: () -> Callable[..., Any] + current_span = sentry_sdk.get_current_span() + transaction_exists = ( + current_span is not None and current_span.containing_transaction == current_span + ) + return sentry_sdk.start_span if transaction_exists else sentry_sdk.start_transaction + + +def _set_agent_data(span, agent): + # type: (sentry_sdk.tracing.Span, agents.Agent) -> None + span.set_data( + SPANDATA.GEN_AI_SYSTEM, "openai" + ) # See footnote for https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/#gen-ai-system for explanation why. + + span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent.name) + + if agent.model_settings.max_tokens: + span.set_data( + SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens + ) + + if agent.model: + span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, agent.model) + + if agent.model_settings.presence_penalty: + span.set_data( + SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, + agent.model_settings.presence_penalty, + ) + + if agent.model_settings.temperature: + span.set_data( + SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature + ) + + if agent.model_settings.top_p: + span.set_data(SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p) + + if agent.model_settings.frequency_penalty: + span.set_data( + SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, + agent.model_settings.frequency_penalty, + ) + + if len(agent.tools) > 0: + span.set_data( + SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, + safe_serialize([vars(tool) for tool in agent.tools]), + ) + + +def _set_usage_data(span, usage): + # type: (sentry_sdk.tracing.Span, Usage) -> None + span.set_data(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage.input_tokens) + span.set_data( + SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED, + usage.input_tokens_details.cached_tokens, + ) + span.set_data(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, usage.output_tokens) + span.set_data( + SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, + usage.output_tokens_details.reasoning_tokens, + ) + span.set_data(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage.total_tokens) + + +def _set_input_data(span, get_response_kwargs): + # type: (sentry_sdk.tracing.Span, dict[str, Any]) -> None + if not should_send_default_pii(): + return + + messages_by_role = { + "system": [], + "user": [], + "assistant": [], + "tool": [], + } # type: (dict[str, list[Any]]) + system_instructions = get_response_kwargs.get("system_instructions") + if system_instructions: + messages_by_role["system"].append({"type": "text", "text": system_instructions}) + + for message in get_response_kwargs.get("input", []): + if "role" in message: + messages_by_role[message.get("role")].append( + {"type": "text", "text": message.get("content")} + ) + else: + if message.get("type") == "function_call": + messages_by_role["assistant"].append(message) + elif message.get("type") == "function_call_output": + messages_by_role["tool"].append(message) + + request_messages = [] + for role, messages in messages_by_role.items(): + if len(messages) > 0: + request_messages.append({"role": role, "content": messages}) + + span.set_data(SPANDATA.GEN_AI_REQUEST_MESSAGES, safe_serialize(request_messages)) + + +def _set_output_data(span, result): + # type: (sentry_sdk.tracing.Span, Any) -> None + if not should_send_default_pii(): + return + + output_messages = { + "response": [], + "tool": [], + } # type: (dict[str, list[Any]]) + + for output in result.output: + if output.type == "function_call": + output_messages["tool"].append(output.dict()) + elif output.type == "message": + for output_message in output.content: + try: + output_messages["response"].append(output_message.text) + except AttributeError: + # Unknown output message type, just return the json + output_messages["response"].append(output_message.dict()) + + if len(output_messages["tool"]) > 0: + span.set_data( + SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, safe_serialize(output_messages["tool"]) + ) + + if len(output_messages["response"]) > 0: + span.set_data( + SPANDATA.GEN_AI_RESPONSE_TEXT, safe_serialize(output_messages["response"]) + ) + + +def safe_serialize(data): + # type: (Any) -> str + """Safely serialize to a readable string.""" + + def serialize_item(item): + # type: (Any) -> Union[str, dict[Any, Any], list[Any], tuple[Any, ...]] + if callable(item): + try: + module = getattr(item, "__module__", None) + qualname = getattr(item, "__qualname__", None) + name = getattr(item, "__name__", "anonymous") + + if module and qualname: + full_path = f"{module}.{qualname}" + elif module and name: + full_path = f"{module}.{name}" + else: + full_path = name + + return f"" + except Exception: + return f"" + elif isinstance(item, dict): + return {k: serialize_item(v) for k, v in item.items()} + elif isinstance(item, (list, tuple)): + return [serialize_item(x) for x in item] + elif hasattr(item, "__dict__"): + try: + attrs = { + k: serialize_item(v) + for k, v in vars(item).items() + if not k.startswith("_") + } + return f"<{type(item).__name__} {attrs}>" + except Exception: + return repr(item) + else: + return item + + try: + serialized = serialize_item(data) + return json.dumps(serialized, default=str) + except Exception: + return str(data) diff --git a/tests/integrations/openai_agents/__init__.py b/tests/integrations/openai_agents/__init__.py new file mode 100644 index 0000000000..6940e2bbbe --- /dev/null +++ b/tests/integrations/openai_agents/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("agents") diff --git a/tests/integrations/openai_agents/test_openai_agents.py b/tests/integrations/openai_agents/test_openai_agents.py new file mode 100644 index 0000000000..ec606c8806 --- /dev/null +++ b/tests/integrations/openai_agents/test_openai_agents.py @@ -0,0 +1,580 @@ +import re +import pytest +from unittest.mock import MagicMock, patch +import os + +from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration +from sentry_sdk.integrations.openai_agents.utils import safe_serialize + +import agents +from agents import ( + Agent, + ModelResponse, + Usage, + ModelSettings, +) +from agents.items import ( + ResponseOutputMessage, + ResponseOutputText, + ResponseFunctionToolCall, +) + +test_run_config = agents.RunConfig(tracing_disabled=True) + + +@pytest.fixture +def mock_usage(): + return Usage( + requests=1, + input_tokens=10, + output_tokens=20, + total_tokens=30, + input_tokens_details=MagicMock(cached_tokens=0), + output_tokens_details=MagicMock(reasoning_tokens=5), + ) + + +@pytest.fixture +def mock_model_response(mock_usage): + return ModelResponse( + output=[ + ResponseOutputMessage( + id="msg_123", + type="message", + status="completed", + content=[ + ResponseOutputText( + text="Hello, how can I help you?", + type="output_text", + annotations=[], + ) + ], + role="assistant", + ) + ], + usage=mock_usage, + response_id="resp_123", + ) + + +@pytest.fixture +def test_agent(): + """Create a real Agent instance for testing.""" + return Agent( + name="test_agent", + instructions="You are a helpful test assistant.", + model="gpt-4", + model_settings=ModelSettings( + max_tokens=100, + temperature=0.7, + top_p=1.0, + presence_penalty=0.0, + frequency_penalty=0.0, + ), + ) + + +@pytest.mark.asyncio +async def test_agent_invocation_span( + sentry_init, capture_events, test_agent, mock_model_response +): + """ + Test that the integration creates spans for agent invocations. + """ + + with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): + with patch( + "agents.models.openai_responses.OpenAIResponsesModel.get_response" + ) as mock_get_response: + mock_get_response.return_value = mock_model_response + + sentry_init( + integrations=[OpenAIAgentsIntegration()], + traces_sample_rate=1.0, + ) + + events = capture_events() + + result = await agents.Runner.run( + test_agent, "Test input", run_config=test_run_config + ) + + assert result is not None + assert result.final_output == "Hello, how can I help you?" + + (transaction,) = events + spans = transaction["spans"] + invoke_agent_span, ai_client_span = spans + + assert transaction["transaction"] == "test_agent workflow" + assert transaction["contexts"]["trace"]["origin"] == "auto.ai.openai_agents" + + assert invoke_agent_span["description"] == "invoke_agent test_agent" + assert invoke_agent_span["data"]["gen_ai.operation.name"] == "invoke_agent" + assert invoke_agent_span["data"]["gen_ai.system"] == "openai" + assert invoke_agent_span["data"]["gen_ai.agent.name"] == "test_agent" + assert invoke_agent_span["data"]["gen_ai.request.max_tokens"] == 100 + assert invoke_agent_span["data"]["gen_ai.request.model"] == "gpt-4" + assert invoke_agent_span["data"]["gen_ai.request.temperature"] == 0.7 + assert invoke_agent_span["data"]["gen_ai.request.top_p"] == 1.0 + + assert ai_client_span["description"] == "chat gpt-4" + assert ai_client_span["data"]["gen_ai.operation.name"] == "chat" + assert ai_client_span["data"]["gen_ai.system"] == "openai" + assert ai_client_span["data"]["gen_ai.agent.name"] == "test_agent" + assert ai_client_span["data"]["gen_ai.request.max_tokens"] == 100 + assert ai_client_span["data"]["gen_ai.request.model"] == "gpt-4" + assert ai_client_span["data"]["gen_ai.request.temperature"] == 0.7 + assert ai_client_span["data"]["gen_ai.request.top_p"] == 1.0 + + +def test_agent_invocation_span_sync( + sentry_init, capture_events, test_agent, mock_model_response +): + """ + Test that the integration creates spans for agent invocations. + """ + + with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): + with patch( + "agents.models.openai_responses.OpenAIResponsesModel.get_response" + ) as mock_get_response: + mock_get_response.return_value = mock_model_response + + sentry_init( + integrations=[OpenAIAgentsIntegration()], + traces_sample_rate=1.0, + ) + + events = capture_events() + + result = agents.Runner.run_sync( + test_agent, "Test input", run_config=test_run_config + ) + + assert result is not None + assert result.final_output == "Hello, how can I help you?" + + (transaction,) = events + spans = transaction["spans"] + invoke_agent_span, ai_client_span = spans + + assert transaction["transaction"] == "test_agent workflow" + assert transaction["contexts"]["trace"]["origin"] == "auto.ai.openai_agents" + + assert invoke_agent_span["description"] == "invoke_agent test_agent" + assert invoke_agent_span["data"]["gen_ai.operation.name"] == "invoke_agent" + assert invoke_agent_span["data"]["gen_ai.system"] == "openai" + assert invoke_agent_span["data"]["gen_ai.agent.name"] == "test_agent" + assert invoke_agent_span["data"]["gen_ai.request.max_tokens"] == 100 + assert invoke_agent_span["data"]["gen_ai.request.model"] == "gpt-4" + assert invoke_agent_span["data"]["gen_ai.request.temperature"] == 0.7 + assert invoke_agent_span["data"]["gen_ai.request.top_p"] == 1.0 + + assert ai_client_span["description"] == "chat gpt-4" + assert ai_client_span["data"]["gen_ai.operation.name"] == "chat" + assert ai_client_span["data"]["gen_ai.system"] == "openai" + assert ai_client_span["data"]["gen_ai.agent.name"] == "test_agent" + assert ai_client_span["data"]["gen_ai.request.max_tokens"] == 100 + assert ai_client_span["data"]["gen_ai.request.model"] == "gpt-4" + assert ai_client_span["data"]["gen_ai.request.temperature"] == 0.7 + assert ai_client_span["data"]["gen_ai.request.top_p"] == 1.0 + + +@pytest.mark.asyncio +async def test_handoff_span(sentry_init, capture_events, mock_usage): + """ + Test that handoff spans are created when agents hand off to other agents. + """ + # Create two simple agents with a handoff relationship + secondary_agent = agents.Agent( + name="secondary_agent", + instructions="You are a secondary agent.", + model="gpt-4o-mini", + ) + + primary_agent = agents.Agent( + name="primary_agent", + instructions="You are a primary agent that hands off to secondary agent.", + model="gpt-4o-mini", + handoffs=[secondary_agent], + ) + + with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): + with patch( + "agents.models.openai_responses.OpenAIResponsesModel.get_response" + ) as mock_get_response: + # Mock two responses: + # 1. Primary agent calls handoff tool + # 2. Secondary agent provides final response + handoff_response = ModelResponse( + output=[ + ResponseFunctionToolCall( + id="call_handoff_123", + call_id="call_handoff_123", + name="transfer_to_secondary_agent", + type="function_call", + arguments="{}", + function=MagicMock( + name="transfer_to_secondary_agent", arguments="{}" + ), + ) + ], + usage=mock_usage, + response_id="resp_handoff_123", + ) + + final_response = ModelResponse( + output=[ + ResponseOutputMessage( + id="msg_final", + type="message", + status="completed", + content=[ + ResponseOutputText( + text="I'm the specialist and I can help with that!", + type="output_text", + annotations=[], + ) + ], + role="assistant", + ) + ], + usage=mock_usage, + response_id="resp_final_123", + ) + + mock_get_response.side_effect = [handoff_response, final_response] + + sentry_init( + integrations=[OpenAIAgentsIntegration()], + traces_sample_rate=1.0, + ) + + events = capture_events() + + result = await agents.Runner.run( + primary_agent, + "Please hand off to secondary agent", + run_config=test_run_config, + ) + + assert result is not None + + (transaction,) = events + spans = transaction["spans"] + handoff_span = spans[2] + + # Verify handoff span was created + assert handoff_span is not None + assert ( + handoff_span["description"] == "handoff from primary_agent to secondary_agent" + ) + assert handoff_span["data"]["gen_ai.operation.name"] == "handoff" + + +@pytest.mark.asyncio +async def test_tool_execution_span(sentry_init, capture_events, test_agent): + """ + Test tool execution span creation. + """ + + @agents.function_tool + def simple_test_tool(message: str) -> str: + """A simple tool""" + return f"Tool executed with: {message}" + + # Create agent with the tool + agent_with_tool = test_agent.clone(tools=[simple_test_tool]) + + with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): + with patch( + "agents.models.openai_responses.OpenAIResponsesModel.get_response" + ) as mock_get_response: + # Create a mock response that includes tool calls + tool_call = ResponseFunctionToolCall( + id="call_123", + call_id="call_123", + name="simple_test_tool", + type="function_call", + arguments='{"message": "hello"}', + function=MagicMock( + name="simple_test_tool", arguments='{"message": "hello"}' + ), + ) + + # First response with tool call + tool_response = ModelResponse( + output=[tool_call], + usage=Usage( + requests=1, input_tokens=10, output_tokens=5, total_tokens=15 + ), + response_id="resp_tool_123", + ) + + # Second response with final answer + final_response = ModelResponse( + output=[ + ResponseOutputMessage( + id="msg_final", + type="message", + status="completed", + content=[ + ResponseOutputText( + text="Task completed using the tool", + type="output_text", + annotations=[], + ) + ], + role="assistant", + ) + ], + usage=Usage( + requests=1, input_tokens=15, output_tokens=10, total_tokens=25 + ), + response_id="resp_final_123", + ) + + # Return different responses on successive calls + mock_get_response.side_effect = [tool_response, final_response] + + sentry_init( + integrations=[OpenAIAgentsIntegration()], + traces_sample_rate=1.0, + send_default_pii=True, + ) + + events = capture_events() + + await agents.Runner.run( + agent_with_tool, + "Please use the simple test tool", + run_config=test_run_config, + ) + + (transaction,) = events + spans = transaction["spans"] + ( + agent_span, + ai_client_span1, + tool_span, + ai_client_span2, + ) = spans + + available_tools = safe_serialize( + [ + { + "name": "simple_test_tool", + "description": "A simple tool", + "params_json_schema": { + "properties": {"message": {"title": "Message", "type": "string"}}, + "required": ["message"], + "title": "simple_test_tool_args", + "type": "object", + "additionalProperties": False, + }, + "on_invoke_tool": "._create_function_tool.._on_invoke_tool>", + "strict_json_schema": True, + "is_enabled": True, + } + ] + ) + + assert transaction["transaction"] == "test_agent workflow" + assert transaction["contexts"]["trace"]["origin"] == "auto.ai.openai_agents" + + assert agent_span["description"] == "invoke_agent test_agent" + assert agent_span["origin"] == "auto.ai.openai_agents" + assert agent_span["data"]["gen_ai.agent.name"] == "test_agent" + assert agent_span["data"]["gen_ai.operation.name"] == "invoke_agent" + assert agent_span["data"]["gen_ai.request.available_tools"] == available_tools + assert agent_span["data"]["gen_ai.request.max_tokens"] == 100 + assert agent_span["data"]["gen_ai.request.model"] == "gpt-4" + assert agent_span["data"]["gen_ai.request.temperature"] == 0.7 + assert agent_span["data"]["gen_ai.request.top_p"] == 1.0 + assert agent_span["data"]["gen_ai.system"] == "openai" + + assert ai_client_span1["description"] == "chat gpt-4" + assert ai_client_span1["data"]["gen_ai.operation.name"] == "chat" + assert ai_client_span1["data"]["gen_ai.system"] == "openai" + assert ai_client_span1["data"]["gen_ai.agent.name"] == "test_agent" + assert ai_client_span1["data"]["gen_ai.request.available_tools"] == available_tools + assert ai_client_span1["data"]["gen_ai.request.max_tokens"] == 100 + assert ai_client_span1["data"]["gen_ai.request.messages"] == safe_serialize( + [ + { + "role": "system", + "content": [ + {"type": "text", "text": "You are a helpful test assistant."} + ], + }, + { + "role": "user", + "content": [ + {"type": "text", "text": "Please use the simple test tool"} + ], + }, + ] + ) + assert ai_client_span1["data"]["gen_ai.request.model"] == "gpt-4" + assert ai_client_span1["data"]["gen_ai.request.temperature"] == 0.7 + assert ai_client_span1["data"]["gen_ai.request.top_p"] == 1.0 + assert ai_client_span1["data"]["gen_ai.usage.input_tokens"] == 10 + assert ai_client_span1["data"]["gen_ai.usage.input_tokens.cached"] == 0 + assert ai_client_span1["data"]["gen_ai.usage.output_tokens"] == 5 + assert ai_client_span1["data"]["gen_ai.usage.output_tokens.reasoning"] == 0 + assert ai_client_span1["data"]["gen_ai.usage.total_tokens"] == 15 + assert re.sub( + r"SerializationIterator\(.*\)", + "NOT_CHECKED", + ai_client_span1["data"]["gen_ai.response.tool_calls"], + ) == safe_serialize( + [ + { + "arguments": '{"message": "hello"}', + "call_id": "call_123", + "name": "simple_test_tool", + "type": "function_call", + "id": "call_123", + "status": None, + "function": "NOT_CHECKED", + } + ] + ) + + assert tool_span["description"] == "execute_tool simple_test_tool" + assert tool_span["data"]["gen_ai.agent.name"] == "test_agent" + assert tool_span["data"]["gen_ai.operation.name"] == "execute_tool" + assert ( + re.sub( + "<.*>(,)", + r"'NOT_CHECKED'\1", + agent_span["data"]["gen_ai.request.available_tools"], + ) + == available_tools + ) + assert tool_span["data"]["gen_ai.request.max_tokens"] == 100 + assert tool_span["data"]["gen_ai.request.model"] == "gpt-4" + assert tool_span["data"]["gen_ai.request.temperature"] == 0.7 + assert tool_span["data"]["gen_ai.request.top_p"] == 1.0 + assert tool_span["data"]["gen_ai.system"] == "openai" + assert tool_span["data"]["gen_ai.tool.description"] == "A simple tool" + assert tool_span["data"]["gen_ai.tool.input"] == '{"message": "hello"}' + assert tool_span["data"]["gen_ai.tool.name"] == "simple_test_tool" + assert tool_span["data"]["gen_ai.tool.output"] == "Tool executed with: hello" + assert tool_span["data"]["gen_ai.tool.type"] == "function" + + assert ai_client_span2["description"] == "chat gpt-4" + assert ai_client_span2["data"]["gen_ai.agent.name"] == "test_agent" + assert ai_client_span2["data"]["gen_ai.operation.name"] == "chat" + assert ( + re.sub( + "<.*>(,)", + r"'NOT_CHECKED'\1", + agent_span["data"]["gen_ai.request.available_tools"], + ) + == available_tools + ) + assert ai_client_span2["data"]["gen_ai.request.max_tokens"] == 100 + assert re.sub( + r"SerializationIterator\(.*\)", + "NOT_CHECKED", + ai_client_span2["data"]["gen_ai.request.messages"], + ) == safe_serialize( + [ + { + "role": "system", + "content": [ + {"type": "text", "text": "You are a helpful test assistant."} + ], + }, + { + "role": "user", + "content": [ + {"type": "text", "text": "Please use the simple test tool"} + ], + }, + { + "role": "assistant", + "content": [ + { + "arguments": '{"message": "hello"}', + "call_id": "call_123", + "name": "simple_test_tool", + "type": "function_call", + "id": "call_123", + "function": "NOT_CHECKED", + } + ], + }, + { + "role": "tool", + "content": [ + { + "call_id": "call_123", + "output": "Tool executed with: hello", + "type": "function_call_output", + } + ], + }, + ] + ) + assert ai_client_span2["data"]["gen_ai.request.model"] == "gpt-4" + assert ai_client_span2["data"]["gen_ai.request.temperature"] == 0.7 + assert ai_client_span2["data"]["gen_ai.request.top_p"] == 1.0 + assert ai_client_span2["data"]["gen_ai.response.text"] == safe_serialize( + ["Task completed using the tool"] + ) + assert ai_client_span2["data"]["gen_ai.system"] == "openai" + assert ai_client_span2["data"]["gen_ai.usage.input_tokens.cached"] == 0 + assert ai_client_span2["data"]["gen_ai.usage.input_tokens"] == 15 + assert ai_client_span2["data"]["gen_ai.usage.output_tokens.reasoning"] == 0 + assert ai_client_span2["data"]["gen_ai.usage.output_tokens"] == 10 + assert ai_client_span2["data"]["gen_ai.usage.total_tokens"] == 25 + + +@pytest.mark.asyncio +async def test_error_handling(sentry_init, capture_events, test_agent): + """ + Test error handling in agent execution. + """ + + with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): + with patch( + "agents.models.openai_responses.OpenAIResponsesModel.get_response" + ) as mock_get_response: + mock_get_response.side_effect = Exception("Model Error") + + sentry_init( + integrations=[OpenAIAgentsIntegration()], + traces_sample_rate=1.0, + ) + + events = capture_events() + + with pytest.raises(Exception, match="Model Error"): + await agents.Runner.run( + test_agent, "Test input", run_config=test_run_config + ) + + ( + error_event, + transaction, + ) = events + + assert error_event["exception"]["values"][0]["type"] == "Exception" + assert error_event["exception"]["values"][0]["value"] == "Model Error" + assert error_event["exception"]["values"][0]["mechanism"]["type"] == "openai_agents" + + spans = transaction["spans"] + (invoke_agent_span, ai_client_span) = spans + + assert transaction["transaction"] == "test_agent workflow" + assert transaction["contexts"]["trace"]["origin"] == "auto.ai.openai_agents" + + assert invoke_agent_span["description"] == "invoke_agent test_agent" + assert invoke_agent_span["origin"] == "auto.ai.openai_agents" + + assert ai_client_span["description"] == "chat gpt-4" + assert ai_client_span["origin"] == "auto.ai.openai_agents" + assert ai_client_span["tags"]["status"] == "internal_error" diff --git a/tox.ini b/tox.ini index f4aee13d02..5c993718d7 100644 --- a/tox.ini +++ b/tox.ini @@ -10,7 +10,7 @@ # The file (and all resulting CI YAMLs) then need to be regenerated via # "scripts/generate-test-files.sh". # -# Last generated: 2025-06-24T07:19:36.122984+00:00 +# Last generated: 2025-06-24T12:35:34.437673+00:00 [tox] requires = @@ -145,6 +145,8 @@ envlist = {py3.9,py3.11,py3.12}-cohere-v5.11.4 {py3.9,py3.11,py3.12}-cohere-v5.15.0 + {py3.9,py3.11,py3.12}-openai_agents-v0.0.19 + {py3.8,py3.10,py3.11}-huggingface_hub-v0.22.2 {py3.8,py3.11,py3.12}-huggingface_hub-v0.26.5 {py3.8,py3.12,py3.13}-huggingface_hub-v0.30.2 @@ -515,6 +517,9 @@ deps = cohere-v5.11.4: cohere==5.11.4 cohere-v5.15.0: cohere==5.15.0 + openai_agents-v0.0.19: openai-agents==0.0.19 + openai_agents: pytest-asyncio + huggingface_hub-v0.22.2: huggingface_hub==0.22.2 huggingface_hub-v0.26.5: huggingface_hub==0.26.5 huggingface_hub-v0.30.2: huggingface_hub==0.30.2 @@ -809,6 +814,7 @@ setenv = litestar: TESTPATH=tests/integrations/litestar loguru: TESTPATH=tests/integrations/loguru openai: TESTPATH=tests/integrations/openai + openai_agents: TESTPATH=tests/integrations/openai_agents openfeature: TESTPATH=tests/integrations/openfeature opentelemetry: TESTPATH=tests/integrations/opentelemetry potel: TESTPATH=tests/integrations/opentelemetry From 15f1348ffd2ea5f1a098df56d1864d7faa9117e1 Mon Sep 17 00:00:00 2001 From: getsentry-bot Date: Tue, 24 Jun 2025 13:55:38 +0000 Subject: [PATCH 13/14] release: 2.31.0 --- CHANGELOG.md | 17 +++++++++++++++++ docs/conf.py | 2 +- sentry_sdk/consts.py | 2 +- setup.py | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddeed9d687..80ef5dc6ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Changelog +## 2.31.0 + +### Various fixes & improvements + +- Support `openai-agents` (#4437) by @antonpirker +- ref(langchain): Greatly simplify `_wrap_configure` (#4479) by @szokeasaurusrex +- tests: Tox update (#4509) by @sentrivana +- Cursor generated rules (#4493) by @sl0thentr0py +- fix(ci): Remove tracerite pin (almost) (#4504) by @sentrivana +- fix(profiling): Ensure profiler thread exits when needed (#4497) by @Zylphrex +- fix(ci): Do not install newest tracerite (#4494) by @sentrivana +- tests: Regenerate tox (#4484) by @sentrivana +- fix(scope): Handle token reset `LookupError`s gracefully (#4481) by @sentrivana +- tests: Upper bound on fakeredis on old Python versions (#4482) by @sentrivana +- feat(logs): Add support for dict args (#4478) by @AbhiPrasad +- tests: Regenerate tox (#4457) by @sentrivana + ## 2.30.0 ### Various fixes & improvements diff --git a/docs/conf.py b/docs/conf.py index 4e12abf550..01b40ae828 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -31,7 +31,7 @@ copyright = "2019-{}, Sentry Team and Contributors".format(datetime.now().year) author = "Sentry Team and Contributors" -release = "2.30.0" +release = "2.31.0" version = ".".join(release.split(".")[:2]) # The short X.Y version. diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 53148a36df..7102eea0e7 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -1181,4 +1181,4 @@ def _get_default_options(): del _get_default_options -VERSION = "2.30.0" +VERSION = "2.31.0" diff --git a/setup.py b/setup.py index ecb5dfa994..0662be384e 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def get_file_text(file_name): setup( name="sentry-sdk", - version="2.30.0", + version="2.31.0", author="Sentry Team and Contributors", author_email="hello@sentry.io", url="https://github.com/getsentry/sentry-python", From 9792e4f4ac0a529a42726b01f8c78f5fd3e218a5 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 24 Jun 2025 16:18:34 +0200 Subject: [PATCH 14/14] Updated changelog --- CHANGELOG.md | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80ef5dc6ea..8bcd8ddc73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,18 +4,35 @@ ### Various fixes & improvements -- Support `openai-agents` (#4437) by @antonpirker -- ref(langchain): Greatly simplify `_wrap_configure` (#4479) by @szokeasaurusrex -- tests: Tox update (#4509) by @sentrivana -- Cursor generated rules (#4493) by @sl0thentr0py -- fix(ci): Remove tracerite pin (almost) (#4504) by @sentrivana -- fix(profiling): Ensure profiler thread exits when needed (#4497) by @Zylphrex -- fix(ci): Do not install newest tracerite (#4494) by @sentrivana -- tests: Regenerate tox (#4484) by @sentrivana -- fix(scope): Handle token reset `LookupError`s gracefully (#4481) by @sentrivana -- tests: Upper bound on fakeredis on old Python versions (#4482) by @sentrivana -- feat(logs): Add support for dict args (#4478) by @AbhiPrasad -- tests: Regenerate tox (#4457) by @sentrivana +- **New Integration (BETA):** Add support for `openai-agents` (#4437) by @antonpirker + + We can now instrument AI agents that are created with the [OpenAI Agents SDK](https://openai.github.io/openai-agents-python/) out of the box. + +```python +import sentry_sdk +from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration + +# Add the OpenAIAgentsIntegration to your sentry_sdk.init call: +sentry_sdk.init( + dsn="...", + integrations=[ + OpenAIAgentsIntegration(), + ] +) +``` + +For more information see the [OpenAI Agents integrations documentation](https://docs.sentry.io/platforms/python/integrations/openai-agents/). + +- Logs: Add support for `dict` arguments (#4478) by @AbhiPrasad +- Add Cursor generated rules (#4493) by @sl0thentr0py +- Greatly simplify Langchain integrations `_wrap_configure` (#4479) by @szokeasaurusrex +- Fix(ci): Remove tracerite pin (almost) (#4504) by @sentrivana +- Fix(profiling): Ensure profiler thread exits when needed (#4497) by @Zylphrex +- Fix(ci): Do not install newest `tracerite` (#4494) by @sentrivana +- Fix(scope): Handle token reset `LookupError`s gracefully (#4481) by @sentrivana +- Tests: Tox update (#4509) by @sentrivana +- Tests: Upper bound on fakeredis on old Python versions (#4482) by @sentrivana +- Tests: Regenerate tox (#4457) by @sentrivana ## 2.30.0 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy