From 40021fd54138dcd6a47232f4216407231d658f1b Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Thu, 20 Dec 2018 15:26:58 +0100 Subject: [PATCH 1/2] fix: Capture WSGI variables early --- sentry_sdk/integrations/wsgi.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 0a25c4124a..ef9eaa1e8b 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -163,6 +163,13 @@ def close(self): def _make_wsgi_event_processor(environ): + 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 +178,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 From 372ede7027b430c4cd393d30ebab8f8aa6310c17 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Thu, 20 Dec 2018 21:39:14 +0100 Subject: [PATCH 2/2] doc: Explain reasoning behind change --- sentry_sdk/integrations/wsgi.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index ef9eaa1e8b..64b045e811 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -163,6 +163,18 @@ 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")