Esempio n. 1
0
    def test_redis_cache_tracing_with_a_wrong_connection(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": 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 = self.pop_spans()
        # 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.metrics[net.TARGET_PORT] == 2230
        assert span.error == 1
Esempio n. 2
0
    def test_simple_cache_add(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"})

        cache.add(u"á_complex_number", 50)
        spans = writer.pop()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "add")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 0)

        expected_meta = {
            "flask_cache.key": u"á_complex_number",
            "flask_cache.backend": "simple",
        }

        eq_(span.meta, expected_meta)
    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
    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.metrics[net.TARGET_PORT] == 2230
Esempio n. 5
0
    def test_simple_cache_delete_many(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"})

        cache.delete_many("complex_operation", "another_complex_op")
        spans = writer.pop()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "delete_many")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 0)

        expected_meta = {
            "flask_cache.key": "['complex_operation', 'another_complex_op']",
            "flask_cache.backend": "simple",
        }

        eq_(span.meta, expected_meta)
Esempio n. 6
0
    def test_simple_cache_add(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"})

        cache.add(u"á_complex_number", 50)
        spans = writer.pop()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "add")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 0)

        expected_meta = {
            "flask_cache.key": u"á_complex_number",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(span.meta, expected_meta)
Esempio n. 7
0
    def test_simple_cache_set_many(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"})

        cache.set_many({
            'first_complex_op': 10,
            'second_complex_op': 20,
        })
        spans = writer.pop()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "set_many")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 0)

        expected_meta = {
            "flask_cache.key": "['first_complex_op', 'second_complex_op']",
            "flask_cache.backend": "simple",
        }

        eq_(span.meta["flask_cache.backend"], "simple")
        ok_("first_complex_op" in span.meta["flask_cache.key"])
        ok_("second_complex_op" in span.meta["flask_cache.key"])
    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.metrics[net.TARGET_PORT] == 2230
        assert span.error == 1
Esempio n. 9
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()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "get")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.meta[CACHE_BACKEND], "memcached")
        eq_(span.meta[net.TARGET_HOST], 'localhost')
        eq_(span.meta[net.TARGET_PORT], '2230')
Esempio n. 10
0
    def test_simple_cache_get_ot(self):
        """OpenTracing version of test_simple_cache_get."""
        ot_tracer = init_tracer("my_svc", self.tracer)

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

        with ot_tracer.start_active_span("ot_span"):
            cache.get(u"á_complex_operation")

        spans = self.get_spans()
        self.assertEqual(len(spans), 2)
        ot_span, dd_span = spans

        # confirm the parenting
        self.assertIsNone(ot_span.parent_id)
        self.assertEqual(dd_span.parent_id, ot_span.span_id)

        self.assertEqual(ot_span.resource, "ot_span")
        self.assertEqual(ot_span.service, "my_svc")

        self.assertEqual(dd_span.service, self.SERVICE)
        self.assertEqual(dd_span.resource, "get")
        self.assertEqual(dd_span.name, "flask_cache.cmd")
        self.assertEqual(dd_span.span_type, "cache")
        self.assertEqual(dd_span.error, 0)

        expected_meta = {
            "flask_cache.key": u"á_complex_operation",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(dd_span.meta, expected_meta)
Esempio n. 11
0
    def test_memcached_cache_tracing_with_a_wrong_connection(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": ["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 = self.pop_spans()
        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.metrics[net.TARGET_PORT] == 2230
Esempio n. 12
0
    def test_simple_cache_get_ot(self):
        """OpenTracing version of test_simple_cache_get."""
        ot_tracer = init_tracer('my_svc', self.tracer)

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

        with ot_tracer.start_active_span('ot_span'):
            cache.get(u'á_complex_operation')

        spans = self.get_spans()
        self.assertEqual(len(spans), 2)
        ot_span, dd_span = spans

        # confirm the parenting
        self.assertIsNone(ot_span.parent_id)
        self.assertEqual(dd_span.parent_id, ot_span.span_id)

        self.assertEqual(ot_span.resource, 'ot_span')
        self.assertEqual(ot_span.service, 'my_svc')

        self.assertEqual(dd_span.service, self.SERVICE)
        self.assertEqual(dd_span.resource, 'get')
        self.assertEqual(dd_span.name, 'flask_cache.cmd')
        self.assertEqual(dd_span.span_type, 'cache')
        self.assertEqual(dd_span.error, 0)

        expected_meta = {
            'flask_cache.key': u'á_complex_operation',
            'flask_cache.backend': 'simple',
        }

        assert_dict_issuperset(dd_span.meta, expected_meta)
Esempio n. 13
0
    def test_simple_cache_set_many(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"})

        cache.set_many({
            'first_complex_op': 10,
            'second_complex_op': 20,
        })
        spans = writer.pop()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "set_many")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 0)

        eq_(span.meta["flask_cache.backend"], "simple")
        ok_("first_complex_op" in span.meta["flask_cache.key"])
        ok_("second_complex_op" in span.meta["flask_cache.key"])
Esempio n. 14
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": 22230,
        }
        cache = Cache(app, config=config)

        # use a wrong redis connection
        with assert_raises(ConnectionError) as ex:
            cache.get(u"á_complex_operation")

        # ensure that the error is not caused by our tracer
        ok_("localhost:22230. Connection refused." in ex.exception.args[0])
        spans = writer.pop()
        # an error trace must be sent
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "get")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.meta[CACHE_BACKEND], "redis")
        eq_(span.meta[net.TARGET_HOST], 'localhost')
        eq_(span.meta[net.TARGET_PORT], '22230')
        eq_(span.error, 1)
Esempio n. 15
0
    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"})
Esempio n. 16
0
    def test_simple_cache_set(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"})

        cache.set(u"á_complex_operation", u"with_á_value\nin two lines")
        spans = writer.pop()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "set")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 0)

        expected_meta = {
            "flask_cache.key": u"á_complex_operation",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(span.meta, expected_meta)
Esempio n. 17
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:22230'],
        }
        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()
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "get")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.meta[CACHE_BACKEND], "memcached")
        eq_(span.meta[net.TARGET_HOST], 'localhost')
        eq_(span.meta[net.TARGET_PORT], '22230')
Esempio n. 18
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 assert_raises(TypeError) as ex:
            cache.add()

        # ensure that the error is not caused by our tracer
        ok_("add()" in ex.exception.args[0])
        ok_("argument" in ex.exception.args[0])
        spans = writer.pop()
        # an error trace must be sent
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "add")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 1)
Esempio n. 19
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 assert_raises(TypeError) as ex:
            cache.add()

        # ensure that the error is not caused by our tracer
        ok_("add()" in ex.exception.args[0])
        ok_("argument" in ex.exception.args[0])
        spans = writer.pop()
        # an error trace must be sent
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "add")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.error, 1)
Esempio n. 20
0
    def setUp(self):
        super(TestFlaskCacheSettings, self).setUp()

        # create the TracedCache instance for a Flask app
        Cache = get_traced_cache(self.tracer)
        app = Flask(__name__)
        self.cache = Cache(app, config={"CACHE_TYPE": "simple"})
Esempio n. 21
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 assert_raises(ConnectionError) as ex:
            cache.get(u"á_complex_operation")

        print(ex.exception)
        # ensure that the error is not caused by our tracer
        ok_("127.0.0.1:2230. Connection refused." in ex.exception.args[0])
        spans = writer.pop()
        # an error trace must be sent
        eq_(len(spans), 1)
        span = spans[0]
        eq_(span.service, self.SERVICE)
        eq_(span.resource, "get")
        eq_(span.name, "flask_cache.cmd")
        eq_(span.span_type, "cache")
        eq_(span.meta[CACHE_BACKEND], "redis")
        eq_(span.meta[net.TARGET_HOST], '127.0.0.1')
        eq_(span.meta[net.TARGET_PORT], '2230')
        eq_(span.error, 1)
Esempio n. 22
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
Esempio n. 23
0
 def test_default_span_tags(self):
     # create the TracedCache instance for a Flask app
     tracer = Tracer()
     Cache = get_traced_cache(tracer, service=self.SERVICE)
     app = Flask(__name__)
     cache = Cache(app, config={"CACHE_TYPE": "simple"})
     # test tags and attributes
     with cache._TracedCache__trace("flask_cache.cmd") as span:
         eq_(span.service, cache._datadog_service)
         eq_(span.span_type, TYPE)
         eq_(span.meta[CACHE_BACKEND], "simple")
         ok_(net.TARGET_HOST not in span.meta)
         ok_(net.TARGET_PORT not in span.meta)
Esempio n. 24
0
 def test_default_span_tags(self):
     # create the TracedCache instance for a Flask app
     tracer = Tracer()
     Cache = get_traced_cache(tracer, service=self.SERVICE)
     app = Flask(__name__)
     cache = Cache(app, config={"CACHE_TYPE": "simple"})
     # test tags and attributes
     with cache._TracedCache__trace("flask_cache.cmd") as span:
         eq_(span.service, cache._datadog_service)
         eq_(span.span_type, TYPE)
         eq_(span.meta[CACHE_BACKEND], "simple")
         ok_(net.TARGET_HOST not in span.meta)
         ok_(net.TARGET_PORT not in span.meta)
Esempio n. 25
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__)
     config = {
         "CACHE_REDIS_PORT": REDIS_CONFIG['port'],
         "CACHE_TYPE": "redis",
     }
     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)
     eq_(resource, expected_resource)
Esempio n. 26
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__)
     config = {
         "CACHE_REDIS_PORT": REDIS_CONFIG['port'],
         "CACHE_TYPE": "redis",
     }
     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)
     eq_(resource, expected_resource)
Esempio n. 27
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}
     eq_(meta, expected_meta)
Esempio n. 28
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']}
     eq_(meta, expected_meta)
Esempio n. 29
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
Esempio n. 30
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
Esempio n. 31
0
 def test_resource_from_cache_with_empty_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": "",
     }
     traced_cache = Cache(app, config=config)
     # expect a resource with a prefix
     expected_resource = "get"
     resource = _resource_from_cache_prefix("GET", traced_cache.cache)
     assert resource == expected_resource
Esempio n. 32
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)
     eq_(resource, expected_resource)
Esempio n. 33
0
 def test_resource_from_cache_with_empty_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': '',
     }
     traced_cache = Cache(app, config=config)
     # expect a resource with a prefix
     expected_resource = 'get'
     resource = _resource_from_cache_prefix('GET', traced_cache.cache)
     assert resource == expected_resource
Esempio n. 34
0
 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)
Esempio n. 35
0
 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, 'cache')
         self.assertEqual(span.meta[CACHE_BACKEND], 'memcached')
         self.assertEqual(span.meta[net.TARGET_HOST], '127.0.0.1')
         self.assertEqual(span.metrics[net.TARGET_PORT], self.TEST_MEMCACHED_PORT)
Esempio n. 36
0
 def test_default_span_tags_memcached(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(self.TEST_MEMCACHED_PORT)],
     }
     cache = Cache(app, config=config)
     # test tags and attributes
     with cache._TracedCache__trace("flask_cache.cmd") as span:
         eq_(span.service, cache._datadog_service)
         eq_(span.span_type, TYPE)
         eq_(span.meta[CACHE_BACKEND], "memcached")
         eq_(span.meta[net.TARGET_HOST], "127.0.0.1")
         eq_(span.meta[net.TARGET_PORT], self.TEST_MEMCACHED_PORT)
Esempio n. 37
0
 def test_default_span_tags_memcached(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(self.TEST_MEMCACHED_PORT)],
     }
     cache = Cache(app, config=config)
     # test tags and attributes
     with cache._TracedCache__trace("flask_cache.cmd") as span:
         eq_(span.service, cache._datadog_service)
         eq_(span.span_type, TYPE)
         eq_(span.meta[CACHE_BACKEND], "memcached")
         eq_(span.meta[net.TARGET_HOST], "127.0.0.1")
         eq_(span.meta[net.TARGET_PORT], self.TEST_MEMCACHED_PORT)
Esempio n. 38
0
 def test_default_span_tags_for_redis(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": self.TEST_REDIS_PORT,
     }
     cache = Cache(app, config=config)
     # test tags and attributes
     with cache._TracedCache__trace("flask_cache.cmd") as span:
         eq_(span.service, cache._datadog_service)
         eq_(span.span_type, TYPE)
         eq_(span.meta[CACHE_BACKEND], "redis")
         eq_(span.meta[net.TARGET_HOST], 'localhost')
         eq_(span.meta[net.TARGET_PORT], self.TEST_REDIS_PORT)
Esempio n. 39
0
 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, "cache")
         self.assertEqual(span.get_tag(CACHE_BACKEND), "memcached")
         self.assertEqual(span.get_tag(net.TARGET_HOST), "127.0.0.1")
         self.assertEqual(span.get_metric(net.TARGET_PORT),
                          self.TEST_MEMCACHED_PORT)
Esempio n. 40
0
 def test_extract_memcached_multiple_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"]),
             "localhost:{}".format(MEMCACHED_CONFIG["port"]),
         ],
     }
     traced_cache = Cache(app, config=config)
     # extract client data
     meta = _extract_conn_tags(_extract_client(traced_cache.cache))
     expected_meta = {
         "out.host": "127.0.0.1",
         "out.port": MEMCACHED_CONFIG["port"],
     }
     assert meta == expected_meta
Esempio n. 41
0
    def test_cache_delete_without_arguments(self):
        # create the TracedCache instance for a Flask app
        Cache = get_traced_cache(self.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.delete()

        # ensure that the error is not caused by our tracer
        assert "delete()" in ex.value.args[0]
        assert "argument" in ex.value.args[0]
        spans = self.pop_spans()
        # an error trace must be sent
        assert len(spans) == 1
        span = spans[0]
        assert span.service == self.SERVICE
        assert span.resource == "delete"
        assert span.name == "flask_cache.cmd"
        assert span.span_type == "cache"
        assert span.error == 1