Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
101 changes: 31 additions & 70 deletions sentry_sdk/integrations/_asgi_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import sentry_sdk
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import has_data_collection_enabled

if TYPE_CHECKING:
from typing import Any, Dict, Optional, Union
Expand Down Expand Up @@ -123,19 +121,14 @@ def _get_request_data(

request_data["headers"] = _filter_headers(headers)

if has_data_collection_enabled(client_options):
qs = _get_query(asgi_scope)
if qs:
filtered_query_string = (
_apply_data_collection_filtering_to_query_string(
query_string=qs,
behaviour=client_options["data_collection"]["url_query_params"],
)
)
if filtered_query_string:
request_data["query_string"] = filtered_query_string
else:
request_data["query_string"] = _get_query(asgi_scope)
qs = _get_query(asgi_scope)
if qs:
filtered_query_string = _apply_data_collection_filtering_to_query_string(
query_string=qs,
behaviour=client_options["data_collection"]["url_query_params"],
)
if filtered_query_string:
request_data["query_string"] = filtered_query_string

request_data["url"] = _get_url(
asgi_scope,
Expand All @@ -146,10 +139,7 @@ def _get_request_data(

client = asgi_scope.get("client")
if client:
if has_data_collection_enabled(client_options):
if client_options["data_collection"]["user_info"]:
request_data["env"] = {"REMOTE_ADDR": _get_ip(asgi_scope)}
elif should_send_default_pii():
if client_options["data_collection"]["user_info"]:
request_data["env"] = {"REMOTE_ADDR": _get_ip(asgi_scope)}

return request_data
Expand All @@ -176,65 +166,36 @@ def _get_request_attributes(
for header, value in filtered_headers.items():
attributes[f"http.request.header.{header.lower()}"] = value

if has_data_collection_enabled(client_options):
filtered_query_string = None
query = _get_query(asgi_scope)

if query:
filtered_query_string = (
_apply_data_collection_filtering_to_query_string(
query_string=query,
behaviour=client_options["data_collection"]["url_query_params"],
)
)
if filtered_query_string:
attributes["http.query"] = filtered_query_string

path = _get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path)
attributes["url.path"] = path

url_without_query_string = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
headers.get("host"),
path=path,
)
filtered_query_string = None
query = _get_query(asgi_scope)

attributes["url.full"] = (
f"{url_without_query_string}?{filtered_query_string}"
if filtered_query_string is not None
else url_without_query_string
if query:
filtered_query_string = _apply_data_collection_filtering_to_query_string(
query_string=query,
behaviour=client_options["data_collection"]["url_query_params"],
)
if filtered_query_string:
attributes["http.query"] = filtered_query_string

elif should_send_default_pii():
query = _get_query(asgi_scope)
if query:
attributes["http.query"] = query
path = _get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path)
attributes["url.path"] = path

path = _get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path)
attributes["url.path"] = path
url_without_query_string = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
headers.get("host"),
path=path,
)

url_without_query_string = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
headers.get("host"),
path=path,
)
query_string = _get_query(asgi_scope)
attributes["url.full"] = (
f"{url_without_query_string}?{query_string}"
if query_string is not None
else url_without_query_string
)
attributes["url.full"] = (
f"{url_without_query_string}?{filtered_query_string}"
if filtered_query_string is not None
else url_without_query_string
)

asgi_scope_client = asgi_scope.get("client")
if asgi_scope_client:
if has_data_collection_enabled(client_options):
if client_options["data_collection"]["user_info"]:
ip = _get_ip(asgi_scope)
attributes["client.address"] = ip
elif should_send_default_pii():
if client_options["data_collection"]["user_info"]:
ip = _get_ip(asgi_scope)
attributes["client.address"] = ip

return attributes
10 changes: 2 additions & 8 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from sentry_sdk.integrations._wsgi_common import (
DEFAULT_HTTP_METHODS_TO_CAPTURE,
)
from sentry_sdk.scope import Scope, should_send_default_pii
from sentry_sdk.scope import Scope
from sentry_sdk.sessions import track_session
from sentry_sdk.traces import (
SOURCE_FOR_STYLE,
Expand All @@ -37,7 +37,6 @@
_get_installed_modules,
capture_internal_exceptions,
event_from_exception,
has_data_collection_enabled,
logger,
qualname_from_function,
reraise,
Expand Down Expand Up @@ -223,12 +222,7 @@ async def _run_app(

if scope.get("client"):
client_options = sentry_sdk.get_client().options
if has_data_collection_enabled(client_options):
if client_options["data_collection"]["user_info"]:
sentry_scope.set_attribute(
SPANDATA.USER_IP_ADDRESS, _get_ip(scope)
)
elif should_send_default_pii():
if client_options["data_collection"]["user_info"]:
sentry_scope.set_attribute(
SPANDATA.USER_IP_ADDRESS, _get_ip(scope)
)
Expand Down
Loading
Loading