Browse Source

fix(monitors): Remove monitor context when appropriate

David Cramer 6 years ago
parent
commit
ddaa4a230e
1 changed files with 10 additions and 4 deletions
  1. 10 4
      src/sentry/utils/monitors.py

+ 10 - 4
src/sentry/utils/monitors.py

@@ -59,16 +59,19 @@ def suppress_errors(func):
 
 @suppress_errors
 def report_monitor_begin(task, **kwargs):
+    monitor_id = task.request.headers.get('X-Sentry-Monitor')
+    with configure_scope() as scope:
+        if monitor_id:
+            scope.set_context('monitor', {'id': monitor_id})
+        else:
+            scope.remove_context('monitor')
+
     if not SENTRY_DSN or not API_ROOT:
         return
 
-    monitor_id = task.request.headers.get('X-Sentry-Monitor')
     if not monitor_id:
         return
 
-    with configure_scope() as scope:
-        scope.set_context('monitor', {'id': monitor_id})
-
     session = SafeSession()
     req = session.post(u'{}/api/0/monitors/{}/checkins/'.format(API_ROOT, monitor_id), headers={
         'Authorization': u'DSN {}'.format(SENTRY_DSN)
@@ -82,6 +85,9 @@ def report_monitor_begin(task, **kwargs):
 
 @suppress_errors
 def report_monitor_complete(task, retval, **kwargs):
+    with configure_scope() as scope:
+        scope.remove_context('monitor')
+
     if not SENTRY_DSN or not API_ROOT:
         return