diff --git a/sentry_sdk/_types.py b/sentry_sdk/_types.py index baf5f6a2fd..e071ff07ff 100644 --- a/sentry_sdk/_types.py +++ b/sentry_sdk/_types.py @@ -13,6 +13,10 @@ SENSITIVE_DATA_SUBSTITUTE = "[Filtered]" BLOB_DATA_SUBSTITUTE = "[Blob substitute]" +OVER_SIZE_LIMIT_SUBSTITUTE = ( + "[Value removed due to size of field exceeding configured maximum size.]" +) +UNPARSABLE_RAW_DATA_SUBSTITUTE = "[Value removed due to being unparsable.]" class AnnotatedValue: @@ -47,6 +51,8 @@ def __len__(self: "AnnotatedValue") -> int: @classmethod def removed_because_raw_data(cls) -> "AnnotatedValue": """The value was removed because it could not be parsed. This is done for request body values that are not json nor a form.""" + # This is the legacy approach - we want to transition over to `substituted_because_raw_data` after we completely transition + # to span-first return AnnotatedValue( value="", metadata={ @@ -59,12 +65,29 @@ def removed_because_raw_data(cls) -> "AnnotatedValue": }, ) + @classmethod + def substituted_because_raw_data(cls) -> "AnnotatedValue": + """The value was replaced because it could not be parsed. This is done for request body values that are not json nor a form.""" + return AnnotatedValue( + value=UNPARSABLE_RAW_DATA_SUBSTITUTE, + metadata={ + "rem": [ # Remark + [ + "!raw", # Unparsable raw data + "s", # The fields original value was substituted + ] + ] + }, + ) + @classmethod def removed_because_over_size_limit(cls, value: "Any" = "") -> "AnnotatedValue": """ The actual value was removed because the size of the field exceeded the configured maximum size, for example specified with the max_request_body_size sdk option. """ + # This is the legacy approach - we want to transition over to `substituted_because_over_size_limit` after we completely transition + # to span-first return AnnotatedValue( value=value, metadata={ @@ -77,6 +100,26 @@ def removed_because_over_size_limit(cls, value: "Any" = "") -> "AnnotatedValue": }, ) + @classmethod + def substituted_because_over_size_limit( + cls, value: "Any" = OVER_SIZE_LIMIT_SUBSTITUTE + ) -> "AnnotatedValue": + """ + The actual value was replaced because the size of the field exceeded the configured maximum size, + for example specified with the max_request_body_size sdk option. + """ + return AnnotatedValue( + value=value, + metadata={ + "rem": [ # Remark + [ + "!config", # Because of configured maximum size + "s", # The fields original value was substituted + ] + ] + }, + ) + @classmethod def substituted_because_contains_sensitive_data(cls) -> "AnnotatedValue": """The actual value was removed because it contained sensitive information.""" diff --git a/sentry_sdk/integrations/_asgi_common.py b/sentry_sdk/integrations/_asgi_common.py index 525ca4b5b5..82edc6ce99 100644 --- a/sentry_sdk/integrations/_asgi_common.py +++ b/sentry_sdk/integrations/_asgi_common.py @@ -93,7 +93,9 @@ def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]": if ty in ("http", "websocket"): request_data["method"] = asgi_scope.get("method") - request_data["headers"] = headers = _filter_headers(_get_headers(asgi_scope)) + request_data["headers"] = headers = _filter_headers( + _get_headers(asgi_scope), + ) request_data["query_string"] = _get_query(asgi_scope) request_data["url"] = _get_url( diff --git a/sentry_sdk/integrations/_wsgi_common.py b/sentry_sdk/integrations/_wsgi_common.py index bcac1eb2d4..4208955620 100644 --- a/sentry_sdk/integrations/_wsgi_common.py +++ b/sentry_sdk/integrations/_wsgi_common.py @@ -218,11 +218,11 @@ def _filter_headers( if should_send_default_pii(): return headers - substitute: "Union[AnnotatedValue, str]" - if use_annotated_value: - substitute = AnnotatedValue.removed_because_over_size_limit() - else: - substitute = SENSITIVE_DATA_SUBSTITUTE + substitute: "Union[AnnotatedValue, str]" = ( + SENSITIVE_DATA_SUBSTITUTE + if not use_annotated_value + else AnnotatedValue.removed_because_over_size_limit() + ) return { k: (v if k.upper().replace("-", "_") not in SENSITIVE_HEADERS else substitute)