def emit(self, record): tags = (('level', logbook.get_level_name(record.level).lower()), ('logger', record.channel)) if record.exc_info: return capture('Exception', exc_info=record.exc_info, tags=tags) return capture('Message', message=record.message, tags=tags)
def main(): parser = OptionParser(version="%%prog %s" % VERSION) parser.add_option('--config', metavar='CONFIG') parser.add_option('--num', default=100) (options, args) = parser.parse_args() if options.config: app.config.from_pyfile(options.config) else: config_path = os.path.expanduser(os.path.join('~', '.sentry', 'sentry.conf.py')) if os.path.exists(config_path): app.config.from_pyfile(config_path) exceptions = [ValueError, SyntaxError, KeyError, IndexError, OSError] messages = [ 'Old Man, sorry. What knight live in that castle over there?', 'You fight with the strength of many men, Sir knight.', 'A witch! A witch! A witch! We\'ve got a witch! A witch!', 'Does wood sink in water?', 'The wise Sir Bedemir was the first to join King Arthur\'s knights, but other illustrious names were soon to follow', ] urls = [ 'http://example.com', 'http://example.com/foo/bar/', 'http://example.com/foo/bar/?baz=biz', ] sql_queries = ['SELECT * FROM table', 'INSERT INTO FOO (a, b, c) VALUES (1, 2, 3)', 'TRUNCATE TABLE baz'] sql_engines = ['psycopg2', 'mysqldb', 'oracle'] http_methods = Http.METHODS for n in xrange(options.num): x = random.randint(0, 2) if x == 0: event = 'Exception' kwargs = {} exc_class = exceptions[n % len(exceptions)] exc_value = messages[n % len(messages)] try: raise exc_class(exc_value) except: kwargs = {'exc_info': sys.exc_info()} elif x == 1: event = 'Message' kwargs = {'message': messages[n % len(messages)]} elif x == 2: event = 'Query' kwargs = {'query': sql_queries[n % len(sql_queries)], 'engine': sql_engines[n % len(sql_engines)]} if random.randint(0, 1) == 1: kwargs['data'] = { 'sentry.interfaces.Http': { 'url': urls[n % len(urls)], 'method': http_methods[n % len(http_methods)], } } capture(event, **kwargs) sys.exit(0)
def emit(self, record): # TODO: level should be a string tags = (('level', record.level), ('logger', record.channel)) if record.exc_info: return capture('Exception', exc_info=record.exc_info, tags=tags) return capture('Message', message=record.message, tags=tags)
def test_query_event(self): event_id = capture('Query', query='SELECT * FROM table', engine='psycopg2', time_spent=36) event = Event.objects.get(event_id) self.assertEquals(event.type, 'sentry.events.Query') self.assertEquals(event.time_spent, 36)
def test_query_event(self): event_id = capture("Query", query="SELECT * FROM table", engine="psycopg2", time_spent=36) event = Event.objects.get(event_id) self.assertEquals(event.type, "sentry.events.Query") self.assertEquals(event.time_spent, 36)
def test_exception_event_without_exc_info(self): try: raise ValueError('foo bar') except: pass # Exception pulls in sys.exc_info() # by default event_id = capture('Exception') event = Event.objects.get(event_id) self.assertEquals(event.type, 'sentry.events.Exception') self.assertEquals(event.time_spent, 0) data = event.data self.assertTrue('event' in data) event_data = data['event'] self.assertTrue('value' in event_data) self.assertEquals(event_data['value'], 'foo bar') self.assertTrue('type' in event_data) self.assertEquals(event_data['type'], 'ValueError') self.assertTrue('frames' in event_data) self.assertEquals(len(event_data['frames']), 1) frame = event_data['frames'][0] self.assertTrue('function' in frame) self.assertEquals(frame['function'], 'test_exception_event_without_exc_info') self.assertTrue('lineno' in frame) self.assertTrue(frame['lineno'] > 0) self.assertTrue('module' in frame) self.assertEquals(frame['module'], 'tests.test_events') self.assertTrue('id' in frame) self.assertTrue('filename' in frame)
def test_exception_event_without_exc_info(self): try: raise ValueError("foo bar") except: pass # Exception pulls in sys.exc_info() # by default event_id = capture("Exception") event = Event.objects.get(event_id) self.assertEquals(event.type, "sentry.events.Exception") self.assertEquals(event.time_spent, 0) data = event.data self.assertTrue("sentry.interfaces.Exception" in data) event_data = data["sentry.interfaces.Exception"] self.assertTrue("value" in event_data) self.assertEquals(event_data["value"], "foo bar") self.assertTrue("type" in event_data) self.assertEquals(event_data["type"], "ValueError") self.assertTrue("frames" in event_data) self.assertEquals(len(event_data["frames"]), 1) frame = event_data["frames"][0] self.assertTrue("function" in frame) self.assertEquals(frame["function"], "test_exception_event_without_exc_info") self.assertTrue("lineno" in frame) self.assertTrue(frame["lineno"] > 0) self.assertTrue("module" in frame) self.assertEquals(frame["module"], "tests.test_events") self.assertTrue("id" in frame) self.assertTrue("filename" in frame)
def test_message_event(self): event_id = capture('Message', message='foo') event = Event.objects.get(event_id) self.assertEquals(event.type, 'sentry.events.Message') self.assertEquals(event.time_spent, 0)
def sentry_exception_handler(request=None, **kwargs): event_id = capture('Exception') if request: # attach the sentry object to the request request.sentry = { 'id': event_id, }
def process_response(self, request, response): if response.status_code != 404: return response message_id = capture('Message', message='Http 404 at %s' % (request.build_absolute_uri()), tags=(('level', 'info'), ('logger', 'http404'))) request.sentry = { 'id': message_id, } return response
def emit(self, record): from sentry import capture # Avoid typical config issues by overriding loggers behavior if record.name == 'sentry.errors': print >> sys.stderr, "Recursive log message sent to SentryHandler" print >> sys.stderr, record.message return kwargs = dict( message=record.message, level=record.level, logger=record.channel, data=record.extra, ) if record.exc_info: return capture('sentry.events.Exception', exc_inf=record.exc_info, **kwargs) return capture('sentry.events.Exception', **kwargs)
def handle_exception(self, exc_info, environ): event_id = capture('Exception', exc_info=exc_info, http={ 'method': environ.get('REQUEST_METHOD'), 'url': get_current_url(environ, strip_querystring=True), 'querystring': environ.get('QUERY_STRING'), }, ) return event_id
def handle_exception(self, exc_info, environ): event_id = capture( "Exception", exc_info=exc_info, data={ "sentry.interfaces.Http": { "method": environ.get("REQUEST_METHOD"), "url": get_current_url(environ, strip_querystring=True), "querystring": environ.get("QUERY_STRING"), } }, ) return event_id
def test_message_event(self): event_id = capture('Message', message='foo') event = Event.objects.get(event_id) self.assertEquals(event.type, 'sentry.events.Message') self.assertEquals(event.time_spent, 0) self.assertTrue('sentry.interfaces.Message' in event.data) event_data = event.data['sentry.interfaces.Message'] self.assertTrue('message' in event_data) self.assertEquals(event_data['message'], 'foo') self.assertTrue('params' in event_data) self.assertEquals(event_data['params'], [])
def test_message_event(self): event_id = capture("Message", message="foo") event = Event.objects.get(event_id) self.assertEquals(event.type, "sentry.events.Message") self.assertEquals(event.time_spent, 0) self.assertTrue("sentry.interfaces.Message" in event.data) event_data = event.data["sentry.interfaces.Message"] self.assertTrue("message" in event_data) self.assertEquals(event_data["message"], "foo") self.assertTrue("params" in event_data) self.assertEquals(event_data["params"], [])
def test_tags(self): event_id = capture('Message', message='foo', tags=[('level', 'info')]) event = Event.objects.get(event_id) self.assertEquals(len(event.tags), 2, event.tags) tags = dict(event.tags) self.assertTrue('level' in tags) self.assertEquals(tags['level'], 'info') self.assertTrue('server' in tags) self.assertEquals(tags['server'], app.config['NAME'])
def test_tags(self): event_id = capture("Message", message="foo", tags=[("level", "info")]) event = Event.objects.get(event_id) self.assertEquals(len(event.tags), 2, event.tags) tags = dict(event.tags) self.assertTrue("level" in tags) self.assertEquals(tags["level"], "info") self.assertTrue("server" in tags) self.assertEquals(tags["server"], app.config["NAME"])
def test_exception_event_with_exc_info(self): try: raise ValueError('foo bar') except: exc_info = sys.exc_info() # We raise a second event to ensure we actually reference # the first event try: raise SyntaxError('baz') except: pass # Exception pulls in sys.exc_info() # by default event_id = capture('Exception', exc_info=exc_info) event = Event.objects.get(event_id) self.assertEquals(event.type, 'sentry.events.Exception') self.assertEquals(event.time_spent, 0) data = event.data self.assertTrue('sentry.interfaces.Exception' in data) event_data = data['sentry.interfaces.Exception'] self.assertTrue('value' in event_data) self.assertEquals(event_data['value'], 'foo bar') self.assertTrue('type' in event_data) self.assertEquals(event_data['type'], 'ValueError') self.assertTrue('sentry.interfaces.Stacktrace' in data) event_data = data['sentry.interfaces.Stacktrace'] self.assertTrue('frames' in event_data) self.assertEquals(len(event_data['frames']), 1) frame = event_data['frames'][0] self.assertTrue('function' in frame) self.assertEquals(frame['function'], 'test_exception_event_with_exc_info') self.assertTrue('lineno' in frame) self.assertTrue(frame['lineno'] > 0) self.assertTrue('module' in frame) self.assertEquals(frame['module'], 'tests.test_events') self.assertTrue('id' in frame) self.assertTrue('filename' in frame)
def test_create(self): event_id = capture('Message', message='hello world', data={ 'sentry.interfaces.Http': { 'url': 'http://example.com/foo/?bar=baz', 'method': 'GET', } }) event = Event.objects.get(event_id) self.assertTrue('sentry.interfaces.Http' in event.data) result = event.data['sentry.interfaces.Http'] self.assertTrue('url' in result, result) self.assertEquals(result['url'], 'http://example.com/foo/') self.assertTrue('method' in result, result) self.assertEquals(result['method'], 'GET') self.assertTrue('data' in result, result) self.assertEquals(result['data'], {}) self.assertTrue('querystring' in result, result) self.assertEquals(result['querystring'], 'bar=baz')
def test_context_processors(self): try: username = "******" password = "******" raise ValueError("foo bar") except: exc_info = sys.exc_info() # We raise a second event to ensure we actually reference # the first event try: raise SyntaxError("baz") except: pass # Exception pulls in sys.exc_info() # by default event_id = capture("Exception") event = Event.objects.get(event_id) event_data = event.data["sentry.interfaces.Exception"] frame = event_data["frames"][0] self.assertEquals(frame["vars"]["password"], "********")
def test_context_processors(self): try: username = '******' password = '******' raise ValueError('foo bar') except: exc_info = sys.exc_info() # We raise a second event to ensure we actually reference # the first event try: raise SyntaxError('baz') except: pass # Exception pulls in sys.exc_info() # by default event_id = capture('Exception') event = Event.objects.get(event_id) event_data = event.data['sentry.interfaces.Exception'] frame = event_data['frames'][0] self.assertEquals(frame['vars']['password'], '****************')
def test_context_processors(self): try: username = '******' password = '******' raise ValueError('foo bar') except: exc_info = sys.exc_info() # We raise a second event to ensure we actually reference # the first event try: raise SyntaxError('baz') except: pass # Exception pulls in sys.exc_info() # by default event_id = capture('Exception') event = Event.objects.get(event_id) self.assertTrue('sentry.interfaces.Stacktrace' in event.data) event_data = event.data['sentry.interfaces.Stacktrace'] frame = event_data['frames'][0] self.assertEquals(frame['vars']['password'], '****************')
def emit(self, record): tags = (('level', record.levelname.lower()), ('logger', record.name)) if record.exc_info: return capture('Exception', exc_info=record.exc_info, tags=tags) return capture('Message', message=record.msg, tags=tags)
def emit(self, record): capture("Message", message=record.msg, params=record.args)
def main(): parser = OptionParser(version="%%prog %s" % VERSION) parser.add_option('--config', metavar='CONFIG') parser.add_option('--num', default=100) (options, args) = parser.parse_args() if options.config: app.config.from_pyfile(options.config) else: config_path = os.path.expanduser( os.path.join('~', '.sentry', 'sentry.conf.py')) if os.path.exists(config_path): app.config.from_pyfile(config_path) exceptions = [ValueError, SyntaxError, KeyError, IndexError, OSError] messages = [ 'Old Man, sorry. What knight live in that castle over there?', 'You fight with the strength of many men, Sir knight.', 'A witch! A witch! A witch! We\'ve got a witch! A witch!', 'Does wood sink in water?', 'The wise Sir Bedemir was the first to join King Arthur\'s knights, but other illustrious names were soon to follow', ] urls = [ 'http://example.com', 'http://example.com/foo/bar/', 'http://example.com/foo/bar/?baz=biz', ] sql_queries = [ 'SELECT * FROM table', 'INSERT INTO FOO (a, b, c) VALUES (1, 2, 3)', 'TRUNCATE TABLE baz' ] sql_engines = ['psycopg2', 'mysqldb', 'oracle'] http_methods = Http.METHODS for n in xrange(options.num): x = random.randint(0, 2) if x == 0: event = 'Exception' kwargs = {} exc_class = exceptions[n % len(exceptions)] exc_value = messages[n % len(messages)] try: raise exc_class(exc_value) except: kwargs = {'exc_info': sys.exc_info()} elif x == 1: event = 'Message' kwargs = {'message': messages[n % len(messages)]} elif x == 2: event = 'Query' kwargs = { 'query': sql_queries[n % len(sql_queries)], 'engine': sql_engines[n % len(sql_engines)] } if random.randint(0, 1) == 1: kwargs['data'] = { 'sentry.interfaces.Http': { 'url': urls[n % len(urls)], 'method': http_methods[n % len(http_methods)], } } capture(event, **kwargs) sys.exit(0)
def handle_exception(self, exc_info, environ): from sentry import capture event_id = capture('sentry.events.Exception', exc_info=exc_info) return event_id