Skip to content

Commit

Permalink
tags request origins in Sentry when loading request data from the body (
Browse files Browse the repository at this point in the history
#6287)

* tags request origins in Sentry when loading request data from the body

relates to #4816
  • Loading branch information
pauldambra authored Oct 7, 2021
1 parent e67bd56 commit 275139c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
13 changes: 13 additions & 0 deletions posthog/api/test/mock_sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from unittest.mock import Mock


def mock_sentry_context_for_tagging(patched_push_scope):
mock_scope = Mock()
mock_set_tag = Mock()
mock_scope.set_context = Mock()
mock_scope.set_tag = mock_set_tag
mock_context_manager = Mock()
mock_context_manager.__enter__ = Mock(return_value=mock_scope)
mock_context_manager.__exit__ = Mock(return_value=None)
patched_push_scope.return_value = mock_context_manager
return mock_set_tag
17 changes: 3 additions & 14 deletions posthog/api/test/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from freezegun import freeze_time
from rest_framework import status

from posthog.api.test.mock_sentry import mock_sentry_context_for_tagging
from posthog.constants import ENVIRONMENT_TEST
from posthog.models import PersonalAPIKey
from posthog.models.feature_flag import FeatureFlag
Expand Down Expand Up @@ -89,7 +90,7 @@ def test_capture_event(self, patch_process_event_with_plugins):
@patch("posthog.api.capture.push_scope")
@patch("posthog.api.capture.celery_app.send_task", MagicMock())
def test_capture_event_adds_library_to_sentry(self, patch_push_scope):
mock_set_tag = self.mock_sentry_context(patch_push_scope)
mock_set_tag = mock_sentry_context_for_tagging(patch_push_scope)

data = {
"event": "$autocapture",
Expand All @@ -114,7 +115,7 @@ def test_capture_event_adds_library_to_sentry(self, patch_push_scope):
@patch("posthog.api.capture.push_scope")
@patch("posthog.api.capture.celery_app.send_task", MagicMock())
def test_capture_event_adds_unknown_to_sentry_when_no_properties_sent(self, patch_push_scope):
mock_set_tag = self.mock_sentry_context(patch_push_scope)
mock_set_tag = mock_sentry_context_for_tagging(patch_push_scope)

data = {
"event": "$autocapture",
Expand All @@ -134,18 +135,6 @@ def test_capture_event_adds_unknown_to_sentry_when_no_properties_sent(self, patc

mock_set_tag.assert_has_calls([call("library", "unknown"), call("library.version", "unknown")])

@staticmethod
def mock_sentry_context(push_scope):
mock_scope = Mock()
mock_set_tag = Mock()
mock_scope.set_context = Mock()
mock_scope.set_tag = mock_set_tag
mock_context_manager = Mock()
mock_context_manager.__enter__ = Mock(return_value=mock_scope)
mock_context_manager.__exit__ = Mock(return_value=None)
push_scope.return_value = mock_context_manager
return mock_set_tag

@patch("posthog.models.team.TEAM_CACHE", {})
@patch("posthog.api.capture.celery_app.send_task")
def test_test_api_key(self, patch_process_event_with_plugins):
Expand Down
19 changes: 19 additions & 0 deletions posthog/test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from unittest.mock import Mock, patch

from django.test import TestCase
from django.test.client import RequestFactory
from freezegun import freeze_time

from posthog.api.test.mock_sentry import mock_sentry_context_for_tagging
from posthog.exceptions import RequestParsingError
from posthog.models import EventDefinition
from posthog.test.base import BaseTest
Expand Down Expand Up @@ -84,6 +87,22 @@ def test_prefer_pageview(self):


class TestLoadDataFromRequest(TestCase):
@patch("posthog.utils.push_scope")
def test_pushes_request_origin_into_sentry_scope(self, push_scope):
origin = "potato.io"

mock_set_tag = mock_sentry_context_for_tagging(push_scope)

rf = RequestFactory()
post_request = rf.post("/s/", "content", "text/plain")
post_request.META["REMOTE_HOST"] = origin

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)

def test_fails_to_JSON_parse_the_literal_string_undefined_when_not_compressed(self):
"""
load_data_from_request assumes that any data
Expand Down
1 change: 1 addition & 0 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def load_data_from_request(request):
# add the data in sentry's scope in case there's an exception
with push_scope() as scope:
scope.set_context("data", data)
scope.set_tag("origin", request.META.get("REMOTE_HOST"))

compression = (
request.GET.get("compression") or request.POST.get("compression") or request.headers.get("content-encoding", "")
Expand Down

0 comments on commit 275139c

Please sign in to comment.