Skip to content

Commit

Permalink
Merge pull request #25 from 4teamwork/deif/HG-3984-make-sentry-init-c…
Browse files Browse the repository at this point in the history
…onfigurable

Make sentry init more configurable
  • Loading branch information
deiferni authored Oct 31, 2023
2 parents e160f05 + 072f19a commit ab30742
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
1 change: 1 addition & 0 deletions changes/HG-3984.other
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide additional default sentry arguments and allow overriding them more easily.
61 changes: 47 additions & 14 deletions django_utils/settings/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,58 @@
class SentryMixin:
SENTRY_DSN = values.Value(environ_required=True, environ_prefix="")

SENTRY_ENVIRONMENT = values.Value("Production", environ_prefix="")

# A path to an alternative CA bundle file in PEM-format.
SENTRY_CACERTS = values.Value(None, environ_prefix="")

SENTRY_TAGS = values.DictValue(default={}, environ_prefix="")
SENTRY_ENVIRONMENT = values.Value("Production", environ_prefix="")
SENTRY_SERVER_NAME = values.Value(None, environ_prefix="")
SENTRY_IN_APP_INLCUDE = values.ListValue(default=[], environ_prefix="")
SENTRY_IN_APP_EXCLUDE = values.ListValue(default=[], environ_prefix="")

@classmethod
def post_setup(cls):
super().post_setup()
if cls.SENTRY_DSN is not None:
sentry_sdk.init(
dsn=cls.SENTRY_DSN,
integrations=[DjangoIntegration()],
send_default_pii=True,
environment=cls.SENTRY_ENVIRONMENT,
ca_certs=cls.SENTRY_CACERTS,
)

if cls.SENTRY_TAGS:
for tagname, tagvalue in cls.SENTRY_TAGS.items():
sentry_sdk.set_tag(tagname, tagvalue)
cls.init_sentry_sdk()
cls.configure_sentry_tags()

@classmethod
def init_sentry_sdk(cls):
if cls.SENTRY_DSN is None:
return

sentry_sdk.init(**cls.make_sentry_init_kwargs())

@classmethod
def make_sentry_init_kwargs(cls):
return dict(
dsn=cls.SENTRY_DSN,
integrations=[DjangoIntegration()],
send_default_pii=True,
environment=cls.get_sentry_environment(),
ca_certs=cls.SENTRY_CACERTS,
release=cls.get_sentry_release_version(),
server_name=cls.SENTRY_SERVER_NAME,
in_app_include=cls.SENTRY_IN_APP_INLCUDE,
in_app_exclude=cls.SENTRY_IN_APP_EXCLUDE,
)

@classmethod
def get_sentry_environment(cls):
return cls.SENTRY_ENVIRONMENT

@classmethod
def get_sentry_release_version(cls):
return None

@classmethod
def get_sentry_tags(cls):
return cls.SENTRY_TAGS

@classmethod
def configure_sentry_tags(cls):
if cls.SENTRY_DSN is None:
return

for tagname, tagvalue in cls.get_sentry_tags().items():
sentry_sdk.set_tag(tagname, tagvalue)

0 comments on commit ab30742

Please sign in to comment.