def test_trace_client_error(self):
        with patch.object(self.client,
                          'execute_command',
                          side_effect=ValueError) as exc_command:
            exc_command.__name__ = 'execute_command'

            redis_opentracing.init_tracing(self.tracer,
                                           trace_all_classes=False)
            redis_opentracing.trace_client(self.client)

            call_exc = None
            try:
                self.client.get('my.key')
            except ValueError as exc:
                call_exc = exc

            self.assertEqual(exc_command.call_count, 1)
            self.assertTrue(True, exc_command.call_args == (('my.key',),))
            self.assertEqual(len(self.tracer.finished_spans()), 1)
            span = self.tracer.finished_spans()[0]
            self.assertEqual(span.operation_name, 'GET')
            self.assertEqual(span.tags, {
                'component': 'redis-py',
                'db.type': 'redis',
                'db.statement': 'GET my.key',
                'span.kind': 'client',
                'error': True,
            })
            self.assertEqual(len(span.logs), 1)
            self.assertEqual(span.logs[0].key_values.get('event', None),
                             'error')
            self.assertTrue(isinstance(
                span.logs[0].key_values.get('error.object', None), ValueError
            ))
Exemplo n.º 2
0
    def test_trace_client_error(self):
        with patch.object(self.client,
                          'execute_command',
                          side_effect=ValueError) as exc_command:
            exc_command.__name__ = 'execute_command'

            redis_opentracing.init_tracing(self.tracer,
                                           trace_all_classes=False,
                                           prefix='Test')
            redis_opentracing.trace_client(self.client)

            call_exc = None
            try:
                self.client.get('my.key')
            except ValueError as exc:
                call_exc = exc

            self.assertEqual(exc_command.call_count, 1)
            self.assertTrue(True, exc_command.call_args == (('my.key', ), ))
            self.assertEqual(len(self.tracer.spans), 1)
            self.assertEqual(self.tracer.spans[0].operation_name, 'Test/GET')
            self.assertEqual(self.tracer.spans[0].is_finished, True)
            self.assertEqual(
                self.tracer.spans[0].tags, {
                    'component': 'redis-py',
                    'db.type': 'redis',
                    'db.statement': 'GET my.key',
                    'span.kind': 'client',
                    'error': 'true',
                    'error.object': call_exc,
                })
def writeredis():
    # start tracing the redis client
    redis_opentracing.trace_client(rdb)
    r = requests.get("https://www.google.com/search?q=python")
    dict = {}
    # put the first 50 results into dict
    for key, value in r.headers.items()[:50]:
        print(key, ":", value)
        dict.update({key: value})
    rdb.mset(dict)
    return jsonify(dict)
Exemplo n.º 4
0
    def test_trace_client_start_span_cb(self):
        def start_span_cb(span):
            span.set_operation_name('Test')

        with patch.object(self.client, 'execute_command',
                          return_value='1') as exc_command:
            exc_command.__name__ = 'execute_command'

            redis_opentracing.init_tracing(self.tracer,
                                           trace_all_classes=False,
                                           start_span_cb=start_span_cb)
            redis_opentracing.trace_client(self.client)
            res = self.client.get('my.key')

            span = self.tracer.finished_spans()[0]
            self.assertEqual(span.operation_name, 'Test')
Exemplo n.º 5
0
    def test_trace_client_start_span_cb_exc(self):
        def start_span_cb(span):
            raise RuntimeError('This should not happen')

        with patch.object(self.client, 'execute_command',
                          return_value='1') as exc_command:
            exc_command.__name__ = 'execute_command'

            redis_opentracing.init_tracing(self.tracer,
                                           trace_all_classes=False,
                                           start_span_cb=start_span_cb)
            redis_opentracing.trace_client(self.client)
            res = self.client.get('my.key')

            span = self.tracer.finished_spans()[0]
            self.assertEqual(span.operation_name, 'GET')
            self.assertFalse(span.tags.get('error', False))
Exemplo n.º 6
0
    def test_trace_client_pubsub(self):
        redis_opentracing.init_tracing(self.tracer, trace_all_classes=False)
        redis_opentracing.trace_client(self.client)

        pubsub = self.client.pubsub()
        pubsub.subscribe('test')

        # Subscribing can cause more than a SUBSCRIBE call.
        self.assertTrue(len(self.tracer.finished_spans()) >= 1)
        span = self.tracer.finished_spans()[0]
        self.assertEqual(span.operation_name, 'SUBSCRIBE')
        self.assertEqual(
            span.tags, {
                'component': 'redis-py',
                'db.type': 'redis',
                'db.statement': 'SUBSCRIBE test',
                'span.kind': 'client',
            })
Exemplo n.º 7
0
    def test_trace_client_pipeline(self):
        redis_opentracing.init_tracing(self.tracer, trace_all_classes=False)
        redis_opentracing.trace_client(self.client)

        pipe = self.client.pipeline()
        pipe.rpush('my:keys', 1, 3)
        pipe.rpush('my:keys', 5, 7)
        pipe.execute()
        self.assertEqual(len(self.tracer.finished_spans()), 1)
        span = self.tracer.finished_spans()[0]
        self.assertEqual(span.operation_name, 'MULTI')
        self.assertEqual(
            span.tags, {
                'component': 'redis-py',
                'db.type': 'redis',
                'db.statement': 'RPUSH my:keys 1 3;RPUSH my:keys 5 7',
                'span.kind': 'client',
            })
Exemplo n.º 8
0
    def test_trace_client_error(self):
        with patch.object(self.client,
                          'execute_command',
                          side_effect=ValueError('error msg')) as exc_command:
            exc_command.__name__ = 'execute_command'

            redis_opentracing.init_tracing(self.tracer,
                                           trace_all_classes=False)
            redis_opentracing.trace_client(self.client)

            call_exc = None
            try:
                self.client.get('my.key')
            except ValueError as exc:
                call_exc = exc

            self.assertEqual(exc_command.call_count, 1)
            self.assertTrue(True, exc_command.call_args == (('my.key', ), ))
            self.assertEqual(len(self.tracer.finished_spans()), 1)
            span = self.tracer.finished_spans()[0]
            self.assertEqual(span.operation_name, 'GET')
            tags = {
                'component': 'redis-py',
                'db.type': 'redis',
                'db.statement': 'GET my.key',
                'span.kind': 'client',
                'error': True,
            }

            for k, v in tags.items():
                assert k in span.tags
                assert span.tags[k] == v

            self.assertEqual(span.tags['error'], True)
            self.assertEqual(span.tags['sfx.error.message'], 'error msg')
            self.assertEqual(span.tags['sfx.error.kind'], 'ValueError')
            self.assertEqual(span.tags['sfx.error.object'],
                             '<class \'ValueError\'>')
            assert len(span.tags['sfx.error.stack']) > 50
Exemplo n.º 9
0
    def test_trace_client(self):
        with patch.object(self.client, 'execute_command',
                          return_value='1') as exc_command:
            exc_command.__name__ = 'execute_command'

            redis_opentracing.init_tracing(self.tracer,
                                           trace_all_classes=False)
            redis_opentracing.trace_client(self.client)
            res = self.client.get('my.key')

            self.assertEqual(res, '1')
            self.assertEqual(exc_command.call_count, 1)
            self.assertTrue(True, exc_command.call_args == (('my.key', ), ))
            self.assertEqual(len(self.tracer.finished_spans()), 1)
            span = self.tracer.finished_spans()[0]
            self.assertEqual(span.operation_name, 'GET')
            self.assertEqual(
                span.tags, {
                    'component': 'redis-py',
                    'db.type': 'redis',
                    'db.statement': 'GET my.key',
                    'span.kind': 'client',
                })
Exemplo n.º 10
0
import datetime
import redis

import redis_opentracing

# Your OpenTracing-compatible tracer here.
tracer = None

if __name__ == '__main__':
    client = redis.StrictRedis()
    redis_opentracing.init_tracing(tracer,
                                   trace_all_classes=False,
                                   prefix='Explicit')

    # Not traced.
    client.set('last_access', datetime.datetime.now())

    # Everthing from this point on client gets traced.
    redis_opentracing.trace_client(client)

    # Traced.
    client.set('last_update', datetime.datetime.now())

    # Traced as a MULTI command with
    # LPUSH fruits lemon watermelon
    pipe = client.pipeline()
    pipe.lpush('fruits', 'lemon', 'watermelon')
    print(pipe.execute())