Skip to content

Commit

Permalink
The wrapped receive() did not return anything. (#1698)
Browse files Browse the repository at this point in the history
We wrapped the receive() callback of all ASGI middleware to create spans when they where executed.
The receive() callback is used to receive message from the server.

But we forgot to return the value that the original receive() callback returns. So basically swallowing the return of the server.

Refs #1696
  • Loading branch information
antonpirker committed Oct 21, 2022
1 parent cfab78d commit fdb7512
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sentry_sdk/integrations/starlette.py
Expand Up @@ -106,7 +106,7 @@ async def _sentry_receive(*args, **kwargs):
description=receive.__qualname__,
) as span:
span.set_tag("starlette.middleware_name", middleware_name)
await receive(*args, **kwargs)
return await receive(*args, **kwargs)

receive_patched = receive.__name__ == "_sentry_receive"
new_receive = _sentry_receive if not receive_patched else receive
Expand All @@ -119,15 +119,15 @@ async def _sentry_send(*args, **kwargs):
op=OP.MIDDLEWARE_STARLETTE_SEND, description=send.__qualname__
) as span:
span.set_tag("starlette.middleware_name", middleware_name)
await send(*args, **kwargs)
return await send(*args, **kwargs)

send_patched = send.__name__ == "_sentry_send"
new_send = _sentry_send if not send_patched else send

await old_call(app, scope, new_receive, new_send, **kwargs)
return await old_call(app, scope, new_receive, new_send, **kwargs)

else:
await old_call(app, scope, receive, send, **kwargs)
return await old_call(app, scope, receive, send, **kwargs)

not_yet_patched = old_call.__name__ not in [
"_create_span_call",
Expand Down
34 changes: 34 additions & 0 deletions tests/integrations/starlette/test_starlette.py
Expand Up @@ -174,6 +174,21 @@ async def do_stuff(message):
await self.app(scope, receive, do_stuff)


class SampleReceiveSendMiddleware:
def __init__(self, app):
self.app = app

async def __call__(self, scope, receive, send):
message = await receive()
assert message
assert message["type"] == "http.request"

send_output = await send({"type": "something-unimportant"})
assert send_output is None

await self.app(scope, receive, send)


@pytest.mark.asyncio
async def test_starlettrequestextractor_content_length(sentry_init):
with mock.patch(
Expand Down Expand Up @@ -644,6 +659,25 @@ def test_middleware_callback_spans(sentry_init, capture_events):
idx += 1


@pytest.mark.asyncio
async def test_middleware_receive_send(sentry_init, capture_events):
sentry_init(
traces_sample_rate=1.0,
integrations=[StarletteIntegration()],
)
starlette_app = starlette_app_factory(
middleware=[Middleware(SampleReceiveSendMiddleware)]
)

client = TestClient(starlette_app, raise_server_exceptions=False)
try:
# NOTE: the assert statements checking
# for correct behaviour are in `SampleReceiveSendMiddleware`!
client.get("/message", auth=("Gabriela", "hello123"))
except Exception:
pass


def test_last_event_id(sentry_init, capture_events):
sentry_init(
integrations=[StarletteIntegration()],
Expand Down

0 comments on commit fdb7512

Please sign in to comment.