Skip to content

Commit

Permalink
Pass SENTRY_ENVIRONMENT through to frontend
Browse files Browse the repository at this point in the history
Propagate the Sentry environment name through to the frontend code that
initializes the Sentry JS SDK.
  • Loading branch information
robertknight committed Jul 8, 2024
1 parent 34a358a commit d7110c5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions h/static/scripts/base/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import * as Sentry from '@sentry/browser';

export type SentryConfig = {
dsn: string;
environment: string;
release: string;
userid?: string;
};

export function init(config: SentryConfig) {
Sentry.init({
dsn: config.dsn,
environment: config.environment,
release: config.release,
});

Expand Down
2 changes: 2 additions & 0 deletions h/static/scripts/tests/base/sentry-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ describe('sentry', () => {
it('configures the Sentry client', () => {
sentry.init({
dsn: 'dsn',
environment: 'prod',
release: 'release',
userid: 'acct:foobar@hypothes.is',
});
assert.calledWith(
fakeSentry.init,
sinon.match({
dsn: 'dsn',
environment: 'prod',
release: 'release',
}),
);
Expand Down
1 change: 1 addition & 0 deletions h/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
34 changes: 20 additions & 14 deletions tests/unit/h/subscribers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"]
Expand All @@ -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):
Expand Down

0 comments on commit d7110c5

Please sign in to comment.