Skip to content

Commit 22df2b7

Browse files
author
Emanuele Palazzetti
committed
flake-cache: test the tracer with a wrong client connection
1 parent ff6a73b commit 22df2b7

File tree

3 files changed

+116
-42
lines changed

3 files changed

+116
-42
lines changed

tests/contrib/flask_cache/test.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
from nose.tools import eq_, ok_
55

6+
# project
7+
from ddtrace.ext import net
68
from ddtrace.tracer import Tracer
79
from ddtrace.contrib.flask_cache import get_traced_cache
10+
from ddtrace.contrib.flask_cache.tracers import TYPE, CACHE_BACKEND
811

12+
# 3rd party
913
from flask import Flask
1014

15+
# testing
1116
from ...test_tracer import DummyWriter
1217

1318

@@ -241,3 +246,45 @@ def test_simple_cache_set_many(self):
241246
eq_(span.meta["flask_cache.backend"], "simple")
242247
ok_("first_complex_op" in span.meta["flask_cache.key"])
243248
ok_("second_complex_op" in span.meta["flask_cache.key"])
249+
250+
def test_default_span_tags(self):
251+
# create the TracedCache instance for a Flask app
252+
tracer = Tracer()
253+
Cache = get_traced_cache(tracer, service=self.SERVICE)
254+
app = Flask(__name__)
255+
cache = Cache(app, config={"CACHE_TYPE": "simple"})
256+
# test tags and attributes
257+
with cache._TracedCache__trace("flask_cache.cmd") as span:
258+
eq_(span.service, cache._datadog_service)
259+
eq_(span.span_type, TYPE)
260+
eq_(span.meta[CACHE_BACKEND], "simple")
261+
ok_(net.TARGET_HOST not in span.meta)
262+
ok_(net.TARGET_PORT not in span.meta)
263+
264+
def test_default_span_tags_for_redis(self):
265+
# create the TracedCache instance for a Flask app
266+
tracer = Tracer()
267+
Cache = get_traced_cache(tracer, service=self.SERVICE)
268+
app = Flask(__name__)
269+
cache = Cache(app, config={"CACHE_TYPE": "redis"})
270+
# test tags and attributes
271+
with cache._TracedCache__trace("flask_cache.cmd") as span:
272+
eq_(span.service, cache._datadog_service)
273+
eq_(span.span_type, TYPE)
274+
eq_(span.meta[CACHE_BACKEND], "redis")
275+
eq_(span.meta[net.TARGET_HOST], 'localhost')
276+
eq_(span.meta[net.TARGET_PORT], '6379')
277+
278+
def test_default_span_tags_memcached(self):
279+
# create the TracedCache instance for a Flask app
280+
tracer = Tracer()
281+
Cache = get_traced_cache(tracer, service=self.SERVICE)
282+
app = Flask(__name__)
283+
cache = Cache(app, config={"CACHE_TYPE": "memcached"})
284+
# test tags and attributes
285+
with cache._TracedCache__trace("flask_cache.cmd") as span:
286+
eq_(span.service, cache._datadog_service)
287+
eq_(span.span_type, TYPE)
288+
eq_(span.meta[CACHE_BACKEND], "memcached")
289+
eq_(span.meta[net.TARGET_HOST], "127.0.0.1")
290+
eq_(span.meta[net.TARGET_PORT], "11211")

tests/contrib/flask_cache/test_utils.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,48 +59,6 @@ def test_extract_memcached_multiple_connection_metadata(self):
5959
}
6060
eq_(meta, expected_meta)
6161

62-
def test_default_span_tags(self):
63-
# create the TracedCache instance for a Flask app
64-
tracer = Tracer()
65-
Cache = get_traced_cache(tracer, service=self.SERVICE)
66-
app = Flask(__name__)
67-
cache = Cache(app, config={"CACHE_TYPE": "simple"})
68-
# test tags and attributes
69-
with cache._TracedCache__trace("flask_cache.cmd") as span:
70-
eq_(span.service, cache._datadog_service)
71-
eq_(span.span_type, TYPE)
72-
eq_(span.meta[CACHE_BACKEND], "simple")
73-
ok_(net.TARGET_HOST not in span.meta)
74-
ok_(net.TARGET_PORT not in span.meta)
75-
76-
def test_default_span_tags_for_redis(self):
77-
# create the TracedCache instance for a Flask app
78-
tracer = Tracer()
79-
Cache = get_traced_cache(tracer, service=self.SERVICE)
80-
app = Flask(__name__)
81-
cache = Cache(app, config={"CACHE_TYPE": "redis"})
82-
# test tags and attributes
83-
with cache._TracedCache__trace("flask_cache.cmd") as span:
84-
eq_(span.service, cache._datadog_service)
85-
eq_(span.span_type, TYPE)
86-
eq_(span.meta[CACHE_BACKEND], "redis")
87-
eq_(span.meta[net.TARGET_HOST], 'localhost')
88-
eq_(span.meta[net.TARGET_PORT], '6379')
89-
90-
def test_default_span_tags_memcached(self):
91-
# create the TracedCache instance for a Flask app
92-
tracer = Tracer()
93-
Cache = get_traced_cache(tracer, service=self.SERVICE)
94-
app = Flask(__name__)
95-
cache = Cache(app, config={"CACHE_TYPE": "memcached"})
96-
# test tags and attributes
97-
with cache._TracedCache__trace("flask_cache.cmd") as span:
98-
eq_(span.service, cache._datadog_service)
99-
eq_(span.span_type, TYPE)
100-
eq_(span.meta[CACHE_BACKEND], "memcached")
101-
eq_(span.meta[net.TARGET_HOST], "127.0.0.1")
102-
eq_(span.meta[net.TARGET_PORT], "11211")
103-
10462
def test_resource_from_cache_with_prefix(self):
10563
# create the TracedCache instance for a Flask app
10664
tracer = Tracer()

tests/contrib/flask_cache/test_wrapper_safety.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
from ddtrace.ext import net
88
from ddtrace.tracer import Tracer
99
from ddtrace.contrib.flask_cache import get_traced_cache
10+
from ddtrace.contrib.flask_cache.tracers import TYPE, CACHE_BACKEND
1011

1112
# 3rd party
1213
from flask import Flask
14+
from redis.exceptions import ConnectionError
1315

1416
# testing
1517
from ...test_tracer import DummyWriter
@@ -157,3 +159,70 @@ def test_cache_set_many_without_arguments(self):
157159
eq_(span.name, "flask_cache.cmd")
158160
eq_(span.span_type, "cache")
159161
eq_(span.error, 1)
162+
163+
def test_redis_cache_tracing_with_a_wrong_connection(self):
164+
# initialize the dummy writer
165+
writer = DummyWriter()
166+
tracer = Tracer()
167+
tracer.writer = writer
168+
169+
# create the TracedCache instance for a Flask app
170+
Cache = get_traced_cache(tracer, service=self.SERVICE)
171+
app = Flask(__name__)
172+
config = {
173+
"CACHE_TYPE": "redis",
174+
"CACHE_REDIS_PORT": 22230,
175+
}
176+
cache = Cache(app, config=config)
177+
178+
# use a wrong redis connection
179+
with assert_raises(ConnectionError) as ex:
180+
cache.get(u"á_complex_operation")
181+
182+
# ensure that the error is not caused by our tracer
183+
ok_("localhost:22230. Connection refused." in ex.exception.args[0])
184+
spans = writer.pop()
185+
# an error trace must be sent
186+
eq_(len(spans), 1)
187+
span = spans[0]
188+
eq_(span.service, self.SERVICE)
189+
eq_(span.resource, "get")
190+
eq_(span.name, "flask_cache.cmd")
191+
eq_(span.span_type, "cache")
192+
eq_(span.meta[CACHE_BACKEND], "redis")
193+
eq_(span.meta[net.TARGET_HOST], 'localhost')
194+
eq_(span.meta[net.TARGET_PORT], '22230')
195+
eq_(span.error, 1)
196+
197+
def test_memcached_cache_tracing_with_a_wrong_connection(self):
198+
# initialize the dummy writer
199+
writer = DummyWriter()
200+
tracer = Tracer()
201+
tracer.writer = writer
202+
203+
# create the TracedCache instance for a Flask app
204+
Cache = get_traced_cache(tracer, service=self.SERVICE)
205+
app = Flask(__name__)
206+
config = {
207+
"CACHE_TYPE": "memcached",
208+
"CACHE_MEMCACHED_SERVERS": ['localhost:22230'],
209+
}
210+
cache = Cache(app, config=config)
211+
212+
# use a wrong memcached connection
213+
# unfortunately, the library doesn't raise an error
214+
cache.get(u"á_complex_operation")
215+
216+
# ensure that the error is not caused by our tracer
217+
spans = writer.pop()
218+
# an error trace must be sent
219+
eq_(len(spans), 1)
220+
span = spans[0]
221+
eq_(span.service, self.SERVICE)
222+
eq_(span.resource, "get")
223+
eq_(span.name, "flask_cache.cmd")
224+
eq_(span.span_type, "cache")
225+
eq_(span.meta[CACHE_BACKEND], "memcached")
226+
eq_(span.meta[net.TARGET_HOST], 'localhost')
227+
eq_(span.meta[net.TARGET_PORT], '22230')
228+
eq_(span.error, 0)

0 commit comments

Comments
 (0)