From 15a19e7e6d3b432c71d858c0cb96462686fb6039 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Mon, 8 Jul 2024 10:53:04 +0100 Subject: [PATCH] Pass `SENTRY_ENVIRONMENT` through to frontend Propagate the Sentry environment name through to the frontend code that initializes the Sentry JS SDK. --- h/static/scripts/base/sentry.ts | 2 ++ h/static/scripts/tests/base/sentry-test.js | 2 ++ h/subscribers.py | 1 + tests/unit/h/subscribers_test.py | 34 +++++++++++++--------- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/h/static/scripts/base/sentry.ts b/h/static/scripts/base/sentry.ts index 0ceeba9f89e..9947bf9d194 100644 --- a/h/static/scripts/base/sentry.ts +++ b/h/static/scripts/base/sentry.ts @@ -9,6 +9,7 @@ import * as Sentry from '@sentry/browser'; export type SentryConfig = { dsn: string; + environment: string; release: string; userid?: string; }; @@ -16,6 +17,7 @@ export type SentryConfig = { export function init(config: SentryConfig) { Sentry.init({ dsn: config.dsn, + environment: config.environment, release: config.release, }); diff --git a/h/static/scripts/tests/base/sentry-test.js b/h/static/scripts/tests/base/sentry-test.js index ebfebe733dc..ff11bedb789 100644 --- a/h/static/scripts/tests/base/sentry-test.js +++ b/h/static/scripts/tests/base/sentry-test.js @@ -23,6 +23,7 @@ describe('sentry', () => { it('configures the Sentry client', () => { sentry.init({ dsn: 'dsn', + environment: 'prod', release: 'release', userid: 'acct:foobar@hypothes.is', }); @@ -30,6 +31,7 @@ describe('sentry', () => { fakeSentry.init, sinon.match({ dsn: 'dsn', + environment: 'prod', release: 'release', }), ); diff --git a/h/subscribers.py b/h/subscribers.py index e972fc3e411..fd43edb9e9c 100644 --- a/h/subscribers.py +++ b/h/subscribers.py @@ -27,6 +27,7 @@ def add_renderer_globals(event): if "h.sentry_dsn_frontend" in request.registry.settings: event["frontend_settings"]["sentry"] = { "dsn": request.registry.settings["h.sentry_dsn_frontend"], + "environment": request.registry.settings["h.sentry_environment"], "release": __version__, "userid": request.authenticated_userid, } diff --git a/tests/unit/h/subscribers_test.py b/tests/unit/h/subscribers_test.py index 5b5fdb9795a..a32aaeee41d 100644 --- a/tests/unit/h/subscribers_test.py +++ b/tests/unit/h/subscribers_test.py @@ -4,7 +4,7 @@ from kombu.exceptions import OperationalError from transaction import TransactionManager -from h import subscribers +from h import __version__, subscribers from h.events import AnnotationEvent from h.exceptions import RealtimeMessageQueueError @@ -30,26 +30,26 @@ def test_adds_analytics_tracking_id(self, event, pyramid_request): def test_adds_frontend_settings(self, event): subscribers.add_renderer_globals(event) - assert event["frontend_settings"] == {} - def test_adds_frontend_settings_sentry(self, event, pyramid_request): - settings = pyramid_request.registry.settings - settings["h.sentry_dsn_frontend"] = "https://sentry.io/flibble" - + @pytest.mark.usefixtures("sentry_config") + def test_adds_frontend_settings_sentry(self, event): subscribers.add_renderer_globals(event) - result = event["frontend_settings"]["sentry"] - assert result["dsn"] == "https://sentry.io/flibble" - assert result["release"] - assert result["userid"] is None + assert event["frontend_settings"]["sentry"] == { + "dsn": "https://sentry.io/flibble", + "environment": "prod", + "release": __version__, + "userid": None, + } - def test_adds_frontend_settings_sentry_user( - self, event, pyramid_config, pyramid_request + @pytest.mark.usefixtures("sentry_config") + def test_adds_frontend_settings_sentry_userid( + self, + event, + pyramid_config, ): pyramid_config.testing_securitypolicy("acct:safet.baljić@example.com") - settings = pyramid_request.registry.settings - settings["h.sentry_dsn_frontend"] = "https://sentry.io/flibble" subscribers.add_renderer_globals(event) result = event["frontend_settings"]["sentry"]["userid"] @@ -64,6 +64,12 @@ def event(self, pyramid_request): def routes(self, pyramid_config): pyramid_config.add_route("index", "/idx") + @pytest.fixture + def sentry_config(self, pyramid_request): + settings = pyramid_request.registry.settings + settings["h.sentry_dsn_frontend"] = "https://sentry.io/flibble" + settings["h.sentry_environment"] = "prod" + class TestPublishAnnotationEvent: def test_it_publishes_the_realtime_event(self, event):