Thanks to visit codestin.com
Credit goes to github.com

Skip to content

✨ Include route in scope to allow middleware and other tools to extract its information#4603

Merged
tiangolo merged 2 commits into
masterfrom
scope-include-route
Feb 21, 2022
Merged

✨ Include route in scope to allow middleware and other tools to extract its information#4603
tiangolo merged 2 commits into
masterfrom
scope-include-route

Conversation

@tiangolo

@tiangolo tiangolo commented Feb 21, 2022

Copy link
Copy Markdown
Member

This is useful, in particular, for external tools handling errors, logs, observability, etc. like Sentry.

There's a counterpart PR in Sentry: getsentry/sentry-python#1349


If you need this now, you can update Sentry's middleware like this:

from typing import TYPE_CHECKING, Any, Optional

from fastapi import FastAPI
from fastapi.routing import APIRoute
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

if TYPE_CHECKING:
    from sentry_sdk._types import Event, Hint

app = FastAPI()


class CustomSentryAsgiMiddleware(SentryAsgiMiddleware):
    def event_processor(
        self, event: "Event", hint: "Hint", asgi_scope: Any
    ) -> "Optional[Event]":
        result_event = super().event_processor(event, hint, asgi_scope)
        route: Optional[APIRoute] = asgi_scope.get("route")
        if route and result_event:
            result_event["transaction"] = route.path
        return result_event


app.add_middleware(CustomSentryAsgiMiddleware)

@codecov

codecov Bot commented Feb 21, 2022

Copy link
Copy Markdown

Codecov Report

Merging #4603 (c5d8244) into master (1ce16c2) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #4603   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          525       526    +1     
  Lines        13281     13324   +43     
=========================================
+ Hits         13281     13324   +43     
Impacted Files Coverage Δ
fastapi/routing.py 100.00% <100.00%> (ø)
tests/test_route_scope.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1ce16c2...c5d8244. Read the comment docs.

@github-actions

Copy link
Copy Markdown
Contributor

@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit c5d8244 at: https://6213b4d698b17b2fc529aaeb--fastapi.netlify.app

@tiangolo tiangolo merged commit f5d7df3 into master Feb 21, 2022
@tiangolo tiangolo deleted the scope-include-route branch February 21, 2022 15:51
JeanArhancet pushed a commit to JeanArhancet/fastapi that referenced this pull request Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant