Skip to content

Commit

Permalink
Add Sentry support to the /_status endpoint
Browse files Browse the repository at this point in the history
Have the `/_status` endpoint send a test message to Sentry if called
with a `?sentry` query param.

This enables testing the Sentry configuration in different environments
(production, staging, etc). Otherwise you'd need to have some way to
make the app crash in order to test it.

`?sentry` does not affect the status endpoint's HTTP return status so it
won't affect Elastic Beanstalk health checking.
  • Loading branch information
seanh committed Jan 30, 2024
1 parent 325955f commit ac421b2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bouncer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from elasticsearch import exceptions
from pyramid import httpexceptions, i18n, view
from pyramid.httpexceptions import HTTPNoContent
from sentry_sdk import capture_message

from bouncer import util
from bouncer.embed_detector import url_embeds_client
Expand Down Expand Up @@ -240,6 +241,9 @@ def healthcheck(request):
if status not in ("yellow", "green"):
raise FailedHealthcheck("cluster status was {!r}".format(status))

if "sentry" in request.params:
capture_message("Test message from the healthcheck() view")

return {"status": "okay"}


Expand Down
19 changes: 18 additions & 1 deletion tests/unit/bouncer/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,14 @@ def h_pyramid_sentry(self, patch):


class TestHealthcheck(object):
def test_ok(self):
def test_ok(self, capture_message):
request = mock_request()
request.es.cluster.health.return_value = {"status": "green"}

result = views.healthcheck(request)

assert result == {"status": "okay"}
capture_message.assert_not_called()

def test_failed_es_request(self):
request = mock_request()
Expand All @@ -322,6 +323,17 @@ def test_wrong_cluster_status(self):

assert "cluster status" in str(e.value)

def test_sentry(self, capture_message):
request = mock_request()
request.params["sentry"] = ""
request.es.cluster.health.return_value = {"status": "green"}

views.healthcheck(request)

capture_message.assert_called_once_with(
"Test message from the healthcheck() view"
)


@pytest.fixture
def parse_document(request):
Expand Down Expand Up @@ -377,3 +389,8 @@ def url_embeds_client():
yield url_embeds_client

patcher.stop()


@pytest.fixture(autouse=True)
def capture_message(patch):
return patch("bouncer.views.capture_message")
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ setenv =
dev: HYPOTHESIS_URL = {env:HYPOTHESIS_URL:http://localhost:5000}
dev: VIA_BASE_URL = {env:VIA_BASE_URL:http://localhost:9083}
dev: WEB_CONCURRENCY = {env:WEB_CONCURRENCY:2}
dev: SENTRY_ENVIRONMENT = {env:SENTRY_ENVIRONMENT:dev}
passenv =
HOME
dev: CHROME_EXTENSION_ID
Expand Down

0 comments on commit ac421b2

Please sign in to comment.