Skip to content

Commit

Permalink
Merge branch 'master' into async-llm
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker authored Oct 17, 2024
2 parents b8439f5 + 891afee commit 67e0cd5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
TRANSACTION_SOURCE_ROUTE,
TRANSACTION_SOURCE_URL,
TRANSACTION_SOURCE_COMPONENT,
TRANSACTION_SOURCE_CUSTOM,
)
from sentry_sdk.utils import (
ContextVar,
Expand Down Expand Up @@ -274,6 +275,7 @@ def event_processor(self, event, hint, asgi_scope):
].get("source") in [
TRANSACTION_SOURCE_COMPONENT,
TRANSACTION_SOURCE_ROUTE,
TRANSACTION_SOURCE_CUSTOM,
]
if not already_set:
name, source = self._get_transaction_name_and_source(
Expand Down
18 changes: 14 additions & 4 deletions sentry_sdk/spotlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import urllib.error
import urllib3

from itertools import chain

from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand All @@ -13,11 +15,12 @@
from typing import Dict
from typing import Optional

from sentry_sdk.utils import logger, env_to_bool
from sentry_sdk.utils import logger, env_to_bool, capture_internal_exceptions
from sentry_sdk.envelope import Envelope


DEFAULT_SPOTLIGHT_URL = "http://localhost:8969/stream"
DJANGO_SPOTLIGHT_MIDDLEWARE_PATH = "sentry_sdk.spotlight.SpotlightMiddleware"


class SpotlightClient:
Expand Down Expand Up @@ -112,9 +115,16 @@ def setup_spotlight(options):
else:
return None

if settings is not None and env_to_bool(
os.environ.get("SENTRY_SPOTLIGHT_ON_ERROR", "1")
if (
settings is not None
and settings.DEBUG
and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_ON_ERROR", "1"))
):
settings.MIDDLEWARE.append("sentry_sdk.spotlight.SpotlightMiddleware")
with capture_internal_exceptions():
middleware = settings.MIDDLEWARE
if DJANGO_SPOTLIGHT_MIDDLEWARE_PATH not in middleware:
settings.MIDDLEWARE = type(middleware)(
chain(middleware, (DJANGO_SPOTLIGHT_MIDDLEWARE_PATH,))
)

return SpotlightClient(url)
42 changes: 42 additions & 0 deletions tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ async def app(scope, receive, send):
return app


@pytest.fixture
def asgi3_custom_transaction_app():

async def app(scope, receive, send):
sentry_sdk.get_current_scope().set_transaction_name("foobar", source="custom")
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
[b"content-type", b"text/plain"],
],
}
)

await send(
{
"type": "http.response.body",
"body": b"Hello, world!",
}
)

return app


def test_invalid_transaction_style(asgi3_app):
with pytest.raises(ValueError) as exp:
SentryAsgiMiddleware(asgi3_app, transaction_style="URL")
Expand Down Expand Up @@ -679,3 +704,20 @@ def dummy_traces_sampler(sampling_context):

async with TestClient(app) as client:
await client.get(request_url)


@pytest.mark.asyncio
async def test_custom_transaction_name(
sentry_init, asgi3_custom_transaction_app, capture_events
):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
app = SentryAsgiMiddleware(asgi3_custom_transaction_app)

async with TestClient(app) as client:
await client.get("/test")

(transaction_event,) = events
assert transaction_event["type"] == "transaction"
assert transaction_event["transaction"] == "foobar"
assert transaction_event["transaction_info"] == {"source": "custom"}
4 changes: 4 additions & 0 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ def test_ensures_spotlight_middleware_when_spotlight_is_enabled(sentry_init, set
Test that ensures if Spotlight is enabled, relevant SpotlightMiddleware
is added to middleware list in settings.
"""
settings.DEBUG = True
original_middleware = frozenset(settings.MIDDLEWARE)

sentry_init(integrations=[DjangoIntegration()], spotlight=True)
Expand All @@ -1263,6 +1264,7 @@ def test_ensures_no_spotlight_middleware_when_env_killswitch_is_false(
Test that ensures if Spotlight is enabled, but is set to a falsy value
the relevant SpotlightMiddleware is NOT added to middleware list in settings.
"""
settings.DEBUG = True
monkeypatch.setenv("SENTRY_SPOTLIGHT_ON_ERROR", "no")

original_middleware = frozenset(settings.MIDDLEWARE)
Expand All @@ -1281,6 +1283,8 @@ def test_ensures_no_spotlight_middleware_when_no_spotlight(
Test that ensures if Spotlight is not enabled
the relevant SpotlightMiddleware is NOT added to middleware list in settings.
"""
settings.DEBUG = True

# We should NOT have the middleware even if the env var is truthy if Spotlight is off
monkeypatch.setenv("SENTRY_SPOTLIGHT_ON_ERROR", "1")

Expand Down

0 comments on commit 67e0cd5

Please sign in to comment.