Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move more Sentry logging to configure_scope #6343

Merged
merged 2 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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