Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions sentry_sdk/integrations/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,29 @@ async def sentry_handle_request(self, request, *args, **kwargs):

old_router_get = Router.get

def sentry_router_get(self, request):
# type: (Any, Request) -> Any
rv = old_router_get(self, request)
def sentry_router_get(self, *args):
# type: (Any, Union[Any, Request]) -> Any
rv = old_router_get(self, *args)
hub = Hub.current
if hub.get_integration(SanicIntegration) is not None:
with capture_internal_exceptions():
with hub.configure_scope() as scope:
scope.transaction = rv[0].__name__
if version >= (21, 3):
# Sanic versions above and including 21.3 append the app name to the
# route name, and so we need to remove it from Route name so the
# transaction name is consistent across all versions
sanic_app_name = self.ctx.app.name
sanic_route = rv[0].name

if sanic_route.startswith("%s." % sanic_app_name):
# We add a 1 to the len of the sanic_app_name because there is a dot
# that joins app name and the route name
# Format: app_name.route_name
sanic_route = sanic_route[len(sanic_app_name) + 1 :]

scope.transaction = sanic_route
else:
scope.transaction = rv[0].__name__
return rv

Router.get = sentry_router_get
Expand Down
53 changes: 47 additions & 6 deletions tests/integrations/sanic/test_sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
from sentry_sdk.integrations.sanic import SanicIntegration

from sanic import Sanic, request, response, __version__ as SANIC_VERSION_RAW
from sanic.response import HTTPResponse
from sanic.exceptions import abort

SANIC_VERSION = tuple(map(int, SANIC_VERSION_RAW.split(".")))


@pytest.fixture
def app():
app = Sanic(__name__)
if SANIC_VERSION >= (20, 12):
# Build (20.12.0) adds a feature where the instance is stored in an internal class
# registry for later retrieval, and so add register=False to disable that
app = Sanic(__name__, register=False)
else:
app = Sanic(__name__)

@app.route("/message")
def hi(request):
Expand Down Expand Up @@ -166,11 +172,46 @@ async def task(i):
if SANIC_VERSION >= (19,):
kwargs["app"] = app

await app.handle_request(
request.Request(**kwargs),
write_callback=responses.append,
stream_callback=responses.append,
)
if SANIC_VERSION >= (21, 3):
try:
app.router.reset()
app.router.finalize()
except AttributeError:
...

class MockAsyncStreamer:
def __init__(self, request_body):
self.request_body = request_body
self.iter = iter(self.request_body)
self.response = b"success"

def respond(self, response):
responses.append(response)
patched_response = HTTPResponse()
patched_response.send = lambda end_stream: asyncio.sleep(0.001)
return patched_response

def __aiter__(self):
return self

async def __anext__(self):
try:
return next(self.iter)
except StopIteration:
raise StopAsyncIteration

patched_request = request.Request(**kwargs)
patched_request.stream = MockAsyncStreamer([b"hello", b"foo"])

await app.handle_request(
patched_request,
)
else:
await app.handle_request(
request.Request(**kwargs),
write_callback=responses.append,
stream_callback=responses.append,
)

(r,) = responses
assert r.status == 200
Expand Down
5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ envlist =

{py3.5,py3.6,py3.7}-sanic-{0.8,18}
{py3.6,py3.7}-sanic-19
{py3.6,py3.7,py3.8}-sanic-20
{py3.7,py3.8,py3.9}-sanic-21

# TODO: Add py3.9
{pypy,py2.7}-celery-3
Expand Down Expand Up @@ -139,6 +141,9 @@ deps =
sanic-0.8: sanic>=0.8,<0.9
sanic-18: sanic>=18.0,<19.0
sanic-19: sanic>=19.0,<20.0
sanic-20: sanic>=20.0,<21.0
sanic-21: sanic>=21.0,<22.0
{py3.7,py3.8,py3.9}-sanic-21: sanic_testing
{py3.5,py3.6}-sanic: aiocontextvars==0.2.1
sanic: aiohttp
py3.5-sanic: ujson<4
Expand Down