Skip to content

Commit

Permalink
feat: add sentry integration
Browse files Browse the repository at this point in the history
  • Loading branch information
trowik committed Jan 17, 2023
1 parent 01d0b0c commit abe37f1
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 261 deletions.
6 changes: 6 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ If either `EMAIL_HOST_USER` or `EMAIL_HOST_PASSWORD` is empty, Django won't atte
## Email error handler
* `ENABLE_ADMIN_EMAIL_LOGGING`: enable Django to send email to admins on errors (default: `False`)
* `ADMINS`: list of people who will get code error notifications. Items in the list should follow this example: `Test Example <test@example.com>,Test2 <test2@example.com>`

## Sentry
* `SENTRY_DSN`: identifier (data source name) for where to send events to. If no value is provided, sentry won't be activated (default: ")
* `SENTRY_ENVIRONMENT`: which app environment sent an event to sentry (default: `development`)
* `SENTRY_TRACES_SAMPLE_RATE`: percentage chance a given transaction will be sent to Sentry (default: `1.0`)
* `SENTRY_SEND_DEFAULT_PII`: enable send PII data that associates users to errors (default: `True`)
14 changes: 14 additions & 0 deletions document_merge_service/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import pytest
import sentry_sdk
from django.core.cache import cache
from pytest_factoryboy import register
from rest_framework.test import APIClient
Expand All @@ -11,6 +12,7 @@
from document_merge_service.api.data import django_file

from .api.authentication import AnonymousUser
from .sentry import sentry_init

register(factories.TemplateFactory)

Expand Down Expand Up @@ -94,3 +96,15 @@ def dms_test_bin():
def loadtest_data():
base = Path(__file__).parent.absolute()
return Path(base, "api", "data", "loadtest")


@pytest.fixture
def sentry_mock(monkeypatch, mocker):
sentry_init("https://SomePublicKey@0.ingest.sentry.io/0", "test", 0.01, False)
sentry_client = sentry_sdk.Hub.current.client
transport = mocker.MagicMock()
monkeypatch.setattr(sentry_client, "transport", transport)
try:
yield transport
finally:
sentry_sdk.Hub.current.bind_client(None)
21 changes: 21 additions & 0 deletions document_merge_service/sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import logging

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import LoggingIntegration


def sentry_init(dsn, env, traces_sample_rate, send_default_pii):
sentry_sdk.init(
dsn=dsn,
environment=env,
send_default_pii=send_default_pii,
traces_sample_rate=traces_sample_rate,
integrations=[
DjangoIntegration(),
LoggingIntegration(level=logging.INFO, event_level=logging.ERROR),
# the `level` kwarg defaults to INFO and instructs sentry to include log messages of that level or higher in
# the message sent to sentry when triggered by an event of level specified in event_level kwarg as
# breadcrumbs.
],
)
16 changes: 16 additions & 0 deletions document_merge_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import environ

from .sentry import sentry_init

env = environ.Env()
django_root = environ.Path(__file__) - 2

Expand Down Expand Up @@ -242,3 +244,17 @@ def parse_admins(admins):
# Email error handler
if ENABLE_ADMIN_EMAIL_LOGGING: # pragma: no cover
LOGGING["loggers"]["django"]["handlers"].append("mail_admins") # type: ignore

# Sentry error tracking
SENTRY_DSN = env.str("SENTRY_DSN", default="")
SENTRY_ENVIRONMENT = env.str("SENTRY_ENVIRONMENT", default="development")
SENTRY_TRACES_SAMPLE_RATE = env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.01)
SENTRY_SEND_DEFAULT_PII = env.bool("SENTRY_SEND_DEFAULT_PII", default=False)

if SENTRY_DSN: # pragma: no cover
sentry_init(
SENTRY_DSN,
SENTRY_ENVIRONMENT,
SENTRY_TRACES_SAMPLE_RATE,
SENTRY_SEND_DEFAULT_PII,
)
12 changes: 12 additions & 0 deletions document_merge_service/tests/test_sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sentry_sdk


def test_sentry(sentry_mock):
assert len(sentry_mock.method_calls) == 0
sentry_sdk.capture_exception(Exception("test_sentry_exc"))
assert len(sentry_mock.method_calls) == 1
sentry_mock.record_lost_event.assert_not_called()
assert (
sentry_mock.method_calls[0].args[0]["exception"]["values"][0]["value"]
== "test_sentry_exc"
)
Loading

0 comments on commit abe37f1

Please sign in to comment.