def setUp(self):
        super(FlaskCacheTest, self).setUp()

        # create the TracedCache instance for a Flask app
        Cache = get_traced_cache(self.tracer, service=self.SERVICE)
        app = Flask(__name__)
        self.cache = Cache(app, config={'CACHE_TYPE': 'simple'})
Example #2
0
    def test_cache_add_without_arguments(self):
        # initialize the dummy writer
        writer = DummyWriter()
        tracer = Tracer()
        tracer.writer = writer

        # create the TracedCache instance for a Flask app
        Cache = get_traced_cache(tracer, service=self.SERVICE)
        app = Flask(__name__)
        cache = Cache(app, config={'CACHE_TYPE': 'simple'})

        # make a wrong call
        with pytest.raises(TypeError) as ex:
            cache.add()

        # ensure that the error is not caused by our tracer
        assert 'add()' in ex.value.args[0]
        assert 'argument' in ex.value.args[0]
        spans = writer.pop()
        # an error trace must be sent
        assert len(spans) == 1
        span = spans[0]
        assert span.service == self.SERVICE
        assert span.resource == 'add'
        assert span.name == 'flask_cache.cmd'
        assert span.span_type == 'cache'
        assert span.error == 1
Example #3
0
    def test_memcached_cache_tracing_with_a_wrong_connection(self):
        # initialize the dummy writer
        writer = DummyWriter()
        tracer = Tracer()
        tracer.writer = writer

        # create the TracedCache instance for a Flask app
        Cache = get_traced_cache(tracer, service=self.SERVICE)
        app = Flask(__name__)
        config = {
            'CACHE_TYPE': 'memcached',
            'CACHE_MEMCACHED_SERVERS': ['localhost:2230'],
        }
        cache = Cache(app, config=config)

        # use a wrong memcached connection
        try:
            cache.get(u'á_complex_operation')
        except Exception:
            pass

        # ensure that the error is not caused by our tracer
        spans = writer.pop()
        assert len(spans) == 1
        span = spans[0]
        assert span.service == self.SERVICE
        assert span.resource == 'get'
        assert span.name == 'flask_cache.cmd'
        assert span.span_type == 'cache'
        assert span.meta[CACHE_BACKEND] == 'memcached'
        assert span.meta[net.TARGET_HOST] == 'localhost'
        assert span.meta[net.TARGET_PORT] == '2230'
Example #4
0
    def test_redis_cache_tracing_with_a_wrong_connection(self):
        # initialize the dummy writer
        writer = DummyWriter()
        tracer = Tracer()
        tracer.writer = writer

        # create the TracedCache instance for a Flask app
        Cache = get_traced_cache(tracer, service=self.SERVICE)
        app = Flask(__name__)
        config = {
            'CACHE_TYPE': 'redis',
            'CACHE_REDIS_PORT': 2230,
            'CACHE_REDIS_HOST': '127.0.0.1'
        }
        cache = Cache(app, config=config)

        # use a wrong redis connection
        with pytest.raises(ConnectionError) as ex:
            cache.get(u'á_complex_operation')

        # ensure that the error is not caused by our tracer
        assert '127.0.0.1:2230. Connection refused.' in ex.value.args[0]
        spans = writer.pop()
        # an error trace must be sent
        assert len(spans) == 1
        span = spans[0]
        assert span.service == self.SERVICE
        assert span.resource == 'get'
        assert span.name == 'flask_cache.cmd'
        assert span.span_type == 'cache'
        assert span.meta[CACHE_BACKEND] == 'redis'
        assert span.meta[net.TARGET_HOST] == '127.0.0.1'
        assert span.meta[net.TARGET_PORT] == '2230'
        assert span.error == 1
Example #5
0
 def test_resource_from_cache_without_prefix(self):
     # create the TracedCache instance for a Flask app
     tracer = Tracer()
     Cache = get_traced_cache(tracer, service=self.SERVICE)
     app = Flask(__name__)
     traced_cache = Cache(app, config={'CACHE_TYPE': 'redis'})
     # expect only the resource name
     expected_resource = 'get'
     resource = _resource_from_cache_prefix('GET', traced_cache.config)
     assert resource == expected_resource
Example #6
0
 def test_resource_from_cache_with_prefix(self):
     # create the TracedCache instance for a Flask app
     tracer = Tracer()
     Cache = get_traced_cache(tracer, service=self.SERVICE)
     app = Flask(__name__)
     config = {
         'CACHE_TYPE': 'redis',
         'CACHE_REDIS_PORT': REDIS_CONFIG['port'],
         'CACHE_KEY_PREFIX': 'users',
     }
     traced_cache = Cache(app, config=config)
     # expect a resource with a prefix
     expected_resource = 'get users'
     resource = _resource_from_cache_prefix('GET', traced_cache.cache)
     assert resource == expected_resource
 def test_default_span_tags_memcached(self):
     # create the TracedCache instance for a Flask app
     Cache = get_traced_cache(self.tracer, service=self.SERVICE)
     app = Flask(__name__)
     config = {
         'CACHE_TYPE': 'memcached',
         'CACHE_MEMCACHED_SERVERS': ['127.0.0.1:{}'.format(self.TEST_MEMCACHED_PORT)],
     }
     cache = Cache(app, config=config)
     # test tags and attributes
     with cache._TracedCache__trace('flask_cache.cmd') as span:
         self.assertEqual(span.service, self.SERVICE)
         self.assertEqual(span.span_type, TYPE)
         self.assertEqual(span.meta[CACHE_BACKEND], 'memcached')
         self.assertEqual(span.meta[net.TARGET_HOST], '127.0.0.1')
         self.assertEqual(span.meta[net.TARGET_PORT], self.TEST_MEMCACHED_PORT)
 def test_default_span_tags_for_redis(self):
     # create the TracedCache instance for a Flask app
     Cache = get_traced_cache(self.tracer, service=self.SERVICE)
     app = Flask(__name__)
     config = {
         'CACHE_TYPE': 'redis',
         'CACHE_REDIS_PORT': self.TEST_REDIS_PORT,
     }
     cache = Cache(app, config=config)
     # test tags and attributes
     with cache._TracedCache__trace('flask_cache.cmd') as span:
         self.assertEqual(span.service, self.SERVICE)
         self.assertEqual(span.span_type, TYPE)
         self.assertEqual(span.meta[CACHE_BACKEND], 'redis')
         self.assertEqual(span.meta[net.TARGET_HOST], 'localhost')
         self.assertEqual(span.meta[net.TARGET_PORT], self.TEST_REDIS_PORT)
Example #9
0
 def test_extract_redis_connection_metadata(self):
     # create the TracedCache instance for a Flask app
     tracer = Tracer()
     Cache = get_traced_cache(tracer, service=self.SERVICE)
     app = Flask(__name__)
     config = {
         'CACHE_TYPE': 'redis',
         'CACHE_REDIS_PORT': REDIS_CONFIG['port'],
     }
     traced_cache = Cache(app, config=config)
     # extract client data
     meta = _extract_conn_tags(traced_cache.cache._client)
     expected_meta = {
         'out.host': 'localhost',
         'out.port': REDIS_CONFIG['port'],
         'out.redis_db': 0
     }
     assert meta == expected_meta
Example #10
0
 def test_extract_memcached_connection_metadata(self):
     # create the TracedCache instance for a Flask app
     tracer = Tracer()
     Cache = get_traced_cache(tracer, service=self.SERVICE)
     app = Flask(__name__)
     config = {
         'CACHE_TYPE':
         'memcached',
         'CACHE_MEMCACHED_SERVERS':
         ['127.0.0.1:{}'.format(MEMCACHED_CONFIG['port'])],
     }
     traced_cache = Cache(app, config=config)
     # extract client data
     meta = _extract_conn_tags(traced_cache.cache._client)
     expected_meta = {
         'out.host': '127.0.0.1',
         'out.port': MEMCACHED_CONFIG['port']
     }
     assert meta == expected_meta