From 2a3b5945c42a1c7fa6a9ea2a074b15777f0e5a49 Mon Sep 17 00:00:00 2001 From: Benjamin Wohlwend Date: Tue, 8 Jan 2019 17:15:54 +0100 Subject: [PATCH] log a debug message if accessing request.body fails --- elasticapm/contrib/django/client.py | 3 ++- tests/contrib/django/django_tests.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/elasticapm/contrib/django/client.py b/elasticapm/contrib/django/client.py index f705779e5..1e3778232 100644 --- a/elasticapm/contrib/django/client.py +++ b/elasticapm/contrib/django/client.py @@ -115,7 +115,8 @@ def get_data_from_request(self, request, capture_body=False): else: try: data = request.body - except Exception: + except Exception as e: + self.logger.debug("Can't capture request body: %s", compat.text_type(e)) data = "" result["body"] = data if (capture_body or not data) else "[REDACTED]" diff --git a/tests/contrib/django/django_tests.py b/tests/contrib/django/django_tests.py index 51f4cafc1..c92308393 100644 --- a/tests/contrib/django/django_tests.py +++ b/tests/contrib/django/django_tests.py @@ -550,6 +550,19 @@ def test_post_raw_data(django_elasticapm_client): assert request["body"] == "[REDACTED]" +def test_post_read_error_logging(django_elasticapm_client, caplog, rf): + request = rf.post("/test", data="{}", content_type="application/json") + + def read(): + raise IOError("foobar") + + request.read = read + with caplog.at_level(logging.DEBUG): + django_elasticapm_client.get_data_from_request(request, capture_body=True) + record = caplog.records[0] + assert record.message == "Can't capture request body: foobar" + + @pytest.mark.skipif(django.VERSION < (1, 9), reason="get-raw-uri-not-available") def test_disallowed_hosts_error_django_19(django_elasticapm_client): request = WSGIRequest(