def clear(cls): """ Clear this thread's instance of the RequestConfiguration. """ tls = ThreadLocals.get_instance() if tls.has_item("bugsnag"): tls.del_item("bugsnag")
def test_session_tracker_sessions_are_unique(self): tracker = SessionTracker(self.config) tracker.auto_sessions = True locs = ThreadLocals.get_instance() tracker.start_session() session_one = locs.get_item('bugsnag-session').copy() tracker.start_session() session_two = locs.get_item('bugsnag-session').copy() self.assertNotEqual(session_one['id'], session_two['id'])
def __call__(self, notification): tls = ThreadLocals.get_instance() session = tls.get_item('bugsnag-session', {}).copy() if session: if notification.unhandled: session['events']['unhandled'] += 1 else: session['events']['handled'] += 1 notification.session = session self.bugsnag(notification)
def get_instance(cls): # type: () -> RequestConfiguration """ Get this thread's instance of the RequestConfiguration. """ tls = ThreadLocals.get_instance() instance = tls.get_item("bugsnag", None) if not instance: instance = RequestConfiguration() tls.set_item("bugsnag", instance) return instance
def test_thread_locals(self): key = "TEST_THREAD_LOCALS" val = {"Test": "Thread", "Locals": "Here"} locs = ThreadLocals.get_instance() self.assertFalse(locs.has_item(key)) locs.set_item(key, val) self.assertTrue(locs.has_item(key)) item = locs.get_item(key) self.assertEqual(item, val) locs.del_item(key) self.assertFalse(locs.has_item(key)) item = locs.get_item(key, "default") self.assertEqual(item, "default")
def test_session_tracker_stores_session_in_threadlocals(self): locs = ThreadLocals.get_instance() tracker = SessionTracker(self.config) tracker.auto_sessions = True tracker.start_session() session = locs.get_item('bugsnag-session') self.assertTrue('id' in session) self.assertTrue('startedAt' in session) self.assertTrue('events' in session) self.assertTrue('handled' in session['events']) self.assertTrue('unhandled' in session['events']) self.assertEqual(session['events']['handled'], 0) self.assertEqual(session['events']['unhandled'], 0)
def start_session(self): if not self.auto_sessions and self.config.auto_capture_sessions: self.auto_sessions = True self.__start_delivery() start_time = strftime('%Y-%m-%dT%H:%M:00', gmtime()) new_session = { 'id': uuid4().hex, 'startedAt': start_time, 'events': { 'handled': 0, 'unhandled': 0 } } tls = ThreadLocals.get_instance() tls.set_item("bugsnag-session", new_session) self.__queue_session(start_time)