Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The wrapped receive() did not return anything. #1698

Merged
merged 3 commits into from
Oct 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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