Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
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,
        }
Ejemplo n.º 11
0
 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
Ejemplo n.º 12
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,
        }
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
 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
Ejemplo n.º 15
0
 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
Ejemplo n.º 16
0
    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'], [])
Ejemplo n.º 17
0
    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"], [])
Ejemplo n.º 18
0
    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'])
Ejemplo n.º 19
0
    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"])
Ejemplo n.º 20
0
    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)
Ejemplo n.º 21
0
    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')
Ejemplo n.º 22
0
    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"], "********")
Ejemplo n.º 23
0
    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'], '****************')
Ejemplo n.º 24
0
    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')
Ejemplo n.º 25
0
    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'], '****************')
Ejemplo n.º 26
0
    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)
Ejemplo n.º 27
0
 def emit(self, record):
     capture("Message", message=record.msg, params=record.args)
Ejemplo n.º 28
0
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)
Ejemplo n.º 29
0
 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)
Ejemplo n.º 30
0
 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)
Ejemplo n.º 31
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