diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 0a25c4124a..64b045e811 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -163,6 +163,25 @@ def close(self): def _make_wsgi_event_processor(environ): + # It's a bit unfortunate that we have to extract and parse the request data + # from the environ so eagerly, but there are a few good reasons for this. + # + # We might be in a situation where the scope/hub never gets torn down + # properly. In that case we will have an unnecessary strong reference to + # all objects in the environ (some of which may take a lot of memory) when + # we're really just interested in a few of them. + # + # Keeping the environment around for longer than the request lifecycle is + # also not necessarily something uWSGI can deal with: + # https://github.com/unbit/uwsgi/issues/1950 + + client_ip = get_client_ip(environ) + request_url = get_request_url(environ) + query_string = environ.get("QUERY_STRING") + method = environ.get("REQUEST_METHOD") + env = dict(_get_environ(environ)) + headers = _filter_headers(dict(_get_headers(environ))) + def event_processor(event, hint): with capture_internal_exceptions(): # if the code below fails halfway through we at least have some data @@ -171,22 +190,13 @@ def event_processor(event, hint): if _should_send_default_pii(): user_info = event.setdefault("user", {}) if "ip_address" not in user_info: - user_info["ip_address"] = get_client_ip(environ) - - if "url" not in request_info: - request_info["url"] = get_request_url(environ) - - if "query_string" not in request_info: - request_info["query_string"] = environ.get("QUERY_STRING") - - if "method" not in request_info: - request_info["method"] = environ.get("REQUEST_METHOD") - - if "env" not in request_info: - request_info["env"] = dict(_get_environ(environ)) + user_info.setdefault("ip_address", client_ip) - if "headers" not in request_info: - request_info["headers"] = _filter_headers(dict(_get_headers(environ))) + request_info.setdefault("url", request_url) + request_info.setdefault("query_string", query_string) + request_info.setdefault("method", method) + request_info.setdefault("env", env) + request_info.setdefault("headers", headers) return event