diff options
author | Michael Manfre | 2022-12-04 20:08:23 -0500 |
---|---|---|
committer | GitHub | 2022-12-04 18:08:23 -0700 |
commit | 3f8045f4128d14558636b1c8a9445c29e8e1583d (patch) | |
tree | e36e7b4a928f9b8497283934d60e512ec02a818b /core | |
parent | 258d992deb06a1ffd82ad33004223cf518ce3aa3 (diff) | |
download | takahe-3f8045f4128d14558636b1c8a9445c29e8e1583d.tar.gz takahe-3f8045f4128d14558636b1c8a9445c29e8e1583d.tar.bz2 takahe-3f8045f4128d14558636b1c8a9445c29e8e1583d.zip |
[WIP] Sentry improvements (#108)
Stator clears scope during the main loop to behave more like
transactions. Transaction names are set.
Sentry tags:
* 'takahe.version'
* 'takahe.app' values 'web' or 'stator'
Added settings:
* TAKAHE_SENTRY_SAMPLE_RATE
* TAKAHE_SENTRY_TRACES_SAMPLE_RATE
Diffstat (limited to 'core')
-rw-r--r-- | core/middleware.py | 19 | ||||
-rw-r--r-- | core/sentry.py | 49 |
2 files changed, 68 insertions, 0 deletions
diff --git a/core/middleware.py b/core/middleware.py index fca5dd8..08c28fa 100644 --- a/core/middleware.py +++ b/core/middleware.py @@ -1,3 +1,6 @@ +from django.core.exceptions import MiddlewareNotUsed + +from core import sentry from core.models import Config @@ -13,3 +16,19 @@ class ConfigLoadingMiddleware: Config.system = Config.load_system() response = self.get_response(request) return response + + +class SentryTaggingMiddleware: + """ + Sets Sentry tags at the start of the request if Sentry is configured. + """ + + def __init__(self, get_response): + if not sentry.SENTRY_ENABLED: + raise MiddlewareNotUsed() + self.get_response = get_response + + def __call__(self, request): + sentry.set_takahe_app("web") + response = self.get_response(request) + return response diff --git a/core/sentry.py b/core/sentry.py new file mode 100644 index 0000000..9a7b100 --- /dev/null +++ b/core/sentry.py @@ -0,0 +1,49 @@ +from contextlib import contextmanager + +from django.conf import settings + +SENTRY_ENABLED = False +try: + if settings.SETUP.SENTRY_DSN: + import sentry_sdk + + SENTRY_ENABLED = True +except ImportError: + pass + + +def noop(*args, **kwargs): + pass + + +@contextmanager +def noop_context(*args, **kwargs): + yield + + +if SENTRY_ENABLED: + configure_scope = sentry_sdk.configure_scope + push_scope = sentry_sdk.push_scope + set_context = sentry_sdk.set_context + set_tag = sentry_sdk.set_tag + start_transaction = sentry_sdk.start_transaction +else: + configure_scope = noop_context + push_scope = noop_context + set_context = noop + set_tag = noop + start_transaction = noop_context + + +def set_takahe_app(name: str): + set_tag("takahe.app", name) + + +def scope_clear(scope): + if scope: + scope.clear() + + +def set_transaction_name(scope, name: str): + if scope: + scope.set_transaction_name(name) |