Skip to content

Commit

Permalink
Move more Sentry logging to configure_scope (#6343)
Browse files Browse the repository at this point in the history
* switch another instance of push scope to configure scope

* default to unknown if cannot read debug info from request for Sentry
  • Loading branch information
pauldambra authored Oct 9, 2021
1 parent 4acae13 commit e47ddf3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
27 changes: 21 additions & 6 deletions posthog/test/test_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
7 changes: 4 additions & 3 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", "")
Expand Down

0 comments on commit e47ddf3

Please sign in to comment.