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

ref(fastapi): Use new scopes API in FastAPI integration #2836

Merged
merged 23 commits into from
Mar 19, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
190923c
Created async and sync decorators
szokeasaurusrex Oct 18, 2023
c8aeadb
Added use of each sentry decorator
szokeasaurusrex Oct 18, 2023
c18a902
Fix circular import
szokeasaurusrex Oct 18, 2023
0fbea43
Merge branch 'sentry-sdk-2.0' into szokeasaurusrex/sentry_patched_dec…
szokeasaurusrex Mar 15, 2024
9f5c279
Revert changes to starlette.py
szokeasaurusrex Mar 15, 2024
45b90ab
Rename method
szokeasaurusrex Mar 15, 2024
69ebafb
Use actual generics, move async implementation to utils
szokeasaurusrex Mar 15, 2024
46cd0e2
Refactor parameters
szokeasaurusrex Mar 15, 2024
7a8196a
Undo changes to _types.py
szokeasaurusrex Mar 15, 2024
d9016db
Use client instead of Hub
szokeasaurusrex Mar 15, 2024
75934d1
Add doc string
szokeasaurusrex Mar 15, 2024
66726d0
Move type comments
szokeasaurusrex Mar 15, 2024
4e48ce3
Merge branch 'sentry-sdk-2.0' into szokeasaurusrex/sentry_patched_dec…
szokeasaurusrex Mar 18, 2024
8e5516d
Fix `_sentry_get_request_handler`
szokeasaurusrex Mar 18, 2024
e8c921c
Fix mypy
szokeasaurusrex Mar 18, 2024
20688fd
Fix circular import
szokeasaurusrex Mar 18, 2024
3c38580
Merge branch 'szokeasaurusrex/sentry_patched_decorator' into szokeasa…
szokeasaurusrex Mar 18, 2024
d017e09
FastAPI without Hub usage
szokeasaurusrex Mar 18, 2024
d93ff92
Added unit tests for decorators
szokeasaurusrex Mar 18, 2024
4b90191
Merge branch 'sentry-sdk-2.0' into szokeasaurusrex/sentry_patched_dec…
szokeasaurusrex Mar 18, 2024
4065c8a
Merge branch 'szokeasaurusrex/sentry_patched_decorator' into szokeasa…
szokeasaurusrex Mar 18, 2024
d26cf4a
Merge remote-tracking branch 'origin/sentry-sdk-2.0' into szokeasauru…
szokeasaurusrex Mar 19, 2024
aecfd0a
Undo changes to gql.py
szokeasaurusrex Mar 19, 2024
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
79 changes: 41 additions & 38 deletions sentry_sdk/integrations/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
from copy import deepcopy
from functools import wraps

import sentry_sdk
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.scope import Scope
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE
from sentry_sdk.utils import transaction_from_function, logger
from sentry_sdk.utils import (
transaction_from_function,
logger,
ensure_integration_enabled_async,
)

if TYPE_CHECKING:
from typing import Any, Callable, Dict
Expand Down Expand Up @@ -84,54 +88,53 @@
@wraps(old_call)
def _sentry_call(*args, **kwargs):
# type: (*Any, **Any) -> Any
hub = Hub.current
with hub.configure_scope() as sentry_scope:
if sentry_scope.profile is not None:
sentry_scope.profile.update_active_thread_id()
return old_call(*args, **kwargs)
sentry_scope = Scope.get_isolation_scope()

Check warning on line 91 in sentry_sdk/integrations/fastapi.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/fastapi.py#L91

Added line #L91 was not covered by tests
if sentry_scope.profile is not None:
sentry_scope.profile.update_active_thread_id()
return old_call(*args, **kwargs)

Check warning on line 94 in sentry_sdk/integrations/fastapi.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/fastapi.py#L93-L94

Added lines #L93 - L94 were not covered by tests

dependant.call = _sentry_call

old_app = old_get_request_handler(*args, **kwargs)

@ensure_integration_enabled_async(FastApiIntegration, old_app)
async def _sentry_app(*args, **kwargs):
# type: (*Any, **Any) -> Any
hub = Hub.current
integration = hub.get_integration(FastApiIntegration)
if integration is None:
return await old_app(*args, **kwargs)

integration = sentry_sdk.get_client().get_integration(FastApiIntegration)
request = args[0]

_set_transaction_name_and_source(
Scope.get_current_scope(), integration.transaction_style, request
)
with hub.configure_scope() as sentry_scope:
extractor = StarletteRequestExtractor(request)
info = await extractor.extract_request_info()

def _make_request_event_processor(req, integration):
# type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event]
def event_processor(event, hint):
# type: (Event, Dict[str, Any]) -> Event

# Extract information from request
request_info = event.get("request", {})
if info:
if "cookies" in info and _should_send_default_pii():
request_info["cookies"] = info["cookies"]
if "data" in info:
request_info["data"] = info["data"]
event["request"] = deepcopy(request_info)

return event

return event_processor

sentry_scope._name = FastApiIntegration.identifier
sentry_scope.add_event_processor(
_make_request_event_processor(request, integration)
)
sentry_scope = Scope.get_isolation_scope()
extractor = StarletteRequestExtractor(request)
info = await extractor.extract_request_info()

def _make_request_event_processor(req, integration):
# type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event]
def event_processor(event, hint):
# type: (Event, Dict[str, Any]) -> Event

# Extract information from request
request_info = event.get("request", {})
if info:
if (
"cookies" in info
and sentry_sdk.get_client().should_send_default_pii()
):
request_info["cookies"] = info["cookies"]
if "data" in info:
request_info["data"] = info["data"]
event["request"] = deepcopy(request_info)

return event

return event_processor

sentry_scope._name = FastApiIntegration.identifier
sentry_scope.add_event_processor(
_make_request_event_processor(request, integration)
)

return await old_app(*args, **kwargs)

Expand Down
Loading