From e47ddf360736e8ef6d64aa801271d1e8d0b76130 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Sat, 9 Oct 2021 13:29:29 +0100 Subject: [PATCH] Move more Sentry logging to configure_scope (#6343) * switch another instance of push scope to configure scope * default to unknown if cannot read debug info from request for Sentry --- posthog/test/test_utils.py | 27 +++++++++++++++++++++------ posthog/utils.py | 7 ++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/posthog/test/test_utils.py b/posthog/test/test_utils.py index 41a76fa9c67a6..381750b83e94e 100644 --- a/posthog/test/test_utils.py +++ b/posthog/test/test_utils.py @@ -1,4 +1,4 @@ -from unittest.mock import Mock, patch +from unittest.mock import call, patch from django.test import TestCase from django.test.client import RequestFactory @@ -87,21 +87,36 @@ def test_prefer_pageview(self): class TestLoadDataFromRequest(TestCase): - @patch("posthog.utils.push_scope") - def test_pushes_request_origin_into_sentry_scope(self, push_scope): + @patch("posthog.utils.configure_scope") + def test_pushes_request_origin_into_sentry_scope(self, patched_scope): origin = "potato.io" + referer = "https://" + origin - mock_set_tag = mock_sentry_context_for_tagging(push_scope) + mock_set_tag = mock_sentry_context_for_tagging(patched_scope) rf = RequestFactory() post_request = rf.post("/s/", "content", "text/plain") post_request.META["REMOTE_HOST"] = origin + post_request.META["HTTP_REFERER"] = referer with self.assertRaises(RequestParsingError) as ctx: load_data_from_request(post_request) - push_scope.assert_called_once() - mock_set_tag.assert_called_once_with("origin", origin) + patched_scope.assert_called_once() + mock_set_tag.assert_has_calls([call("origin", origin), call("referer", referer)]) + + @patch("posthog.utils.configure_scope") + def test_pushes_request_origin_into_sentry_scope_even_when_not_available(self, patched_scope): + mock_set_tag = mock_sentry_context_for_tagging(patched_scope) + + rf = RequestFactory() + post_request = rf.post("/s/", "content", "text/plain") + + with self.assertRaises(RequestParsingError): + load_data_from_request(post_request) + + patched_scope.assert_called_once() + mock_set_tag.assert_has_calls([call("origin", "unknown"), call("referer", "unknown")]) def test_fails_to_JSON_parse_the_literal_string_undefined_when_not_compressed(self): """ diff --git a/posthog/utils.py b/posthog/utils.py index 8244463d44520..6fbdebd87424e 100644 --- a/posthog/utils.py +++ b/posthog/utils.py @@ -37,7 +37,7 @@ from django.template.loader import get_template from django.utils import timezone from rest_framework.request import Request -from sentry_sdk import push_scope +from sentry_sdk import configure_scope from posthog.constants import AnalyticsDBMS, AvailableFeature from posthog.exceptions import RequestParsingError @@ -390,9 +390,10 @@ def load_data_from_request(request): return None # add the data in sentry's scope in case there's an exception - with push_scope() as scope: + with configure_scope() as scope: scope.set_context("data", data) - scope.set_tag("origin", request.META.get("REMOTE_HOST")) + scope.set_tag("origin", request.META.get("REMOTE_HOST", "unknown")) + scope.set_tag("referer", request.META.get("HTTP_REFERER", "unknown")) compression = ( request.GET.get("compression") or request.POST.get("compression") or request.headers.get("content-encoding", "")