|
31 | 31 | from starlette.middleware.authentication import AuthenticationMiddleware |
32 | 32 | from starlette.testclient import TestClient |
33 | 33 |
|
| 34 | +STARLETTE_VERSION = tuple([int(x) for x in starlette.__version__.split(".")]) |
| 35 | + |
34 | 36 | PICTURE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "photo.jpg") |
35 | 37 |
|
36 | 38 | BODY_JSON = {"some": "json", "for": "testing", "nested": {"numbers": 123}} |
@@ -152,6 +154,26 @@ async def __anext__(self): |
152 | 154 | raise StopAsyncIteration |
153 | 155 |
|
154 | 156 |
|
| 157 | +class SampleMiddleware: |
| 158 | + def __init__(self, app): |
| 159 | + self.app = app |
| 160 | + |
| 161 | + async def __call__(self, scope, receive, send): |
| 162 | + # only handle http requests |
| 163 | + if scope["type"] != "http": |
| 164 | + await self.app(scope, receive, send) |
| 165 | + return |
| 166 | + |
| 167 | + async def do_stuff(message): |
| 168 | + if message["type"] == "http.response.start": |
| 169 | + # do something here. |
| 170 | + pass |
| 171 | + |
| 172 | + await send(message) |
| 173 | + |
| 174 | + await self.app(scope, receive, do_stuff) |
| 175 | + |
| 176 | + |
155 | 177 | @pytest.mark.asyncio |
156 | 178 | async def test_starlettrequestextractor_content_length(sentry_init): |
157 | 179 | with mock.patch( |
@@ -546,6 +568,82 @@ def test_middleware_spans(sentry_init, capture_events): |
546 | 568 | idx += 1 |
547 | 569 |
|
548 | 570 |
|
| 571 | +def test_middleware_callback_spans(sentry_init, capture_events): |
| 572 | + sentry_init( |
| 573 | + traces_sample_rate=1.0, |
| 574 | + integrations=[StarletteIntegration()], |
| 575 | + ) |
| 576 | + starlette_app = starlette_app_factory(middleware=[Middleware(SampleMiddleware)]) |
| 577 | + events = capture_events() |
| 578 | + |
| 579 | + client = TestClient(starlette_app, raise_server_exceptions=False) |
| 580 | + try: |
| 581 | + client.get("/message", auth=("Gabriela", "hello123")) |
| 582 | + except Exception: |
| 583 | + pass |
| 584 | + |
| 585 | + (_, transaction_event) = events |
| 586 | + |
| 587 | + expected = [ |
| 588 | + { |
| 589 | + "op": "middleware.starlette", |
| 590 | + "description": "ServerErrorMiddleware", |
| 591 | + "tags": {"starlette.middleware_name": "ServerErrorMiddleware"}, |
| 592 | + }, |
| 593 | + { |
| 594 | + "op": "middleware.starlette", |
| 595 | + "description": "SampleMiddleware", |
| 596 | + "tags": {"starlette.middleware_name": "SampleMiddleware"}, |
| 597 | + }, |
| 598 | + { |
| 599 | + "op": "middleware.starlette", |
| 600 | + "description": "ExceptionMiddleware", |
| 601 | + "tags": {"starlette.middleware_name": "ExceptionMiddleware"}, |
| 602 | + }, |
| 603 | + { |
| 604 | + "op": "middleware.starlette.send", |
| 605 | + "description": "SampleMiddleware.__call__.<locals>.do_stuff", |
| 606 | + "tags": {"starlette.middleware_name": "ExceptionMiddleware"}, |
| 607 | + }, |
| 608 | + { |
| 609 | + "op": "middleware.starlette.send", |
| 610 | + "description": "ServerErrorMiddleware.__call__.<locals>._send", |
| 611 | + "tags": {"starlette.middleware_name": "SampleMiddleware"}, |
| 612 | + }, |
| 613 | + { |
| 614 | + "op": "middleware.starlette.send", |
| 615 | + "description": "_ASGIAdapter.send.<locals>.send" |
| 616 | + if STARLETTE_VERSION < (0, 21) |
| 617 | + else "_TestClientTransport.handle_request.<locals>.send", |
| 618 | + "tags": {"starlette.middleware_name": "ServerErrorMiddleware"}, |
| 619 | + }, |
| 620 | + { |
| 621 | + "op": "middleware.starlette.send", |
| 622 | + "description": "SampleMiddleware.__call__.<locals>.do_stuff", |
| 623 | + "tags": {"starlette.middleware_name": "ExceptionMiddleware"}, |
| 624 | + }, |
| 625 | + { |
| 626 | + "op": "middleware.starlette.send", |
| 627 | + "description": "ServerErrorMiddleware.__call__.<locals>._send", |
| 628 | + "tags": {"starlette.middleware_name": "SampleMiddleware"}, |
| 629 | + }, |
| 630 | + { |
| 631 | + "op": "middleware.starlette.send", |
| 632 | + "description": "_ASGIAdapter.send.<locals>.send" |
| 633 | + if STARLETTE_VERSION < (0, 21) |
| 634 | + else "_TestClientTransport.handle_request.<locals>.send", |
| 635 | + "tags": {"starlette.middleware_name": "ServerErrorMiddleware"}, |
| 636 | + }, |
| 637 | + ] |
| 638 | + |
| 639 | + idx = 0 |
| 640 | + for span in transaction_event["spans"]: |
| 641 | + assert span["op"] == expected[idx]["op"] |
| 642 | + assert span["description"] == expected[idx]["description"] |
| 643 | + assert span["tags"] == expected[idx]["tags"] |
| 644 | + idx += 1 |
| 645 | + |
| 646 | + |
549 | 647 | def test_last_event_id(sentry_init, capture_events): |
550 | 648 | sentry_init( |
551 | 649 | integrations=[StarletteIntegration()], |
|
0 commit comments