|
| 1 | +import time |
| 2 | + |
| 3 | +# 3rd party |
| 4 | +from nose.tools import eq_, ok_ |
| 5 | +from django.core.cache import caches |
| 6 | + |
| 7 | +# testing |
| 8 | +from .utils import DjangoTraceTestCase |
| 9 | + |
| 10 | + |
| 11 | +class DjangoCacheRedisTest(DjangoTraceTestCase): |
| 12 | + """ |
| 13 | + Ensures that the cache system is properly traced in |
| 14 | + different cache backend |
| 15 | + """ |
| 16 | + def test_cache_redis_get(self): |
| 17 | + # get the redis cache |
| 18 | + cache = caches['redis'] |
| 19 | + |
| 20 | + # (trace) the cache miss |
| 21 | + start = time.time() |
| 22 | + hit = cache.get('missing_key') |
| 23 | + end = time.time() |
| 24 | + |
| 25 | + # tests |
| 26 | + spans = self.tracer.writer.pop() |
| 27 | + eq_(len(spans), 1) |
| 28 | + |
| 29 | + span = spans[0] |
| 30 | + eq_(span.service, 'django') |
| 31 | + eq_(span.resource, 'get') |
| 32 | + eq_(span.name, 'django.cache') |
| 33 | + eq_(span.span_type, 'cache') |
| 34 | + eq_(span.error, 0) |
| 35 | + |
| 36 | + expected_meta = { |
| 37 | + 'django.cache.backend': 'django_redis.cache.RedisCache', |
| 38 | + 'django.cache.key': 'missing_key', |
| 39 | + } |
| 40 | + |
| 41 | + eq_(span.meta, expected_meta) |
| 42 | + assert start < span.start < span.start + span.duration < end |
| 43 | + |
| 44 | + def test_cache_redis_get_many(self): |
| 45 | + # get the redis cache |
| 46 | + cache = caches['redis'] |
| 47 | + |
| 48 | + # (trace) the cache miss |
| 49 | + start = time.time() |
| 50 | + hit = cache.get_many(['missing_key', 'another_key']) |
| 51 | + end = time.time() |
| 52 | + |
| 53 | + # tests |
| 54 | + spans = self.tracer.writer.pop() |
| 55 | + eq_(len(spans), 1) |
| 56 | + |
| 57 | + span = spans[0] |
| 58 | + eq_(span.service, 'django') |
| 59 | + eq_(span.resource, 'get_many') |
| 60 | + eq_(span.name, 'django.cache') |
| 61 | + eq_(span.span_type, 'cache') |
| 62 | + eq_(span.error, 0) |
| 63 | + |
| 64 | + expected_meta = { |
| 65 | + 'django.cache.backend': 'django_redis.cache.RedisCache', |
| 66 | + 'django.cache.key': str(['missing_key', 'another_key']), |
| 67 | + } |
| 68 | + |
| 69 | + eq_(span.meta, expected_meta) |
| 70 | + assert start < span.start < span.start + span.duration < end |
| 71 | + |
| 72 | + def test_cache_pylibmc_get(self): |
| 73 | + # get the redis cache |
| 74 | + cache = caches['pylibmc'] |
| 75 | + |
| 76 | + # (trace) the cache miss |
| 77 | + start = time.time() |
| 78 | + hit = cache.get('missing_key') |
| 79 | + end = time.time() |
| 80 | + |
| 81 | + # tests |
| 82 | + spans = self.tracer.writer.pop() |
| 83 | + eq_(len(spans), 1) |
| 84 | + |
| 85 | + span = spans[0] |
| 86 | + eq_(span.service, 'django') |
| 87 | + eq_(span.resource, 'get') |
| 88 | + eq_(span.name, 'django.cache') |
| 89 | + eq_(span.span_type, 'cache') |
| 90 | + eq_(span.error, 0) |
| 91 | + |
| 92 | + expected_meta = { |
| 93 | + 'django.cache.backend': 'django.core.cache.backends.memcached.PyLibMCCache', |
| 94 | + 'django.cache.key': 'missing_key', |
| 95 | + } |
| 96 | + |
| 97 | + eq_(span.meta, expected_meta) |
| 98 | + assert start < span.start < span.start + span.duration < end |
| 99 | + |
| 100 | + def test_cache_pylibmc_get_many(self): |
| 101 | + # get the redis cache |
| 102 | + cache = caches['pylibmc'] |
| 103 | + |
| 104 | + # (trace) the cache miss |
| 105 | + start = time.time() |
| 106 | + hit = cache.get_many(['missing_key', 'another_key']) |
| 107 | + end = time.time() |
| 108 | + |
| 109 | + # tests |
| 110 | + spans = self.tracer.writer.pop() |
| 111 | + eq_(len(spans), 1) |
| 112 | + |
| 113 | + span = spans[0] |
| 114 | + eq_(span.service, 'django') |
| 115 | + eq_(span.resource, 'get_many') |
| 116 | + eq_(span.name, 'django.cache') |
| 117 | + eq_(span.span_type, 'cache') |
| 118 | + eq_(span.error, 0) |
| 119 | + |
| 120 | + expected_meta = { |
| 121 | + 'django.cache.backend': 'django.core.cache.backends.memcached.PyLibMCCache', |
| 122 | + 'django.cache.key': str(['missing_key', 'another_key']), |
| 123 | + } |
| 124 | + |
| 125 | + eq_(span.meta, expected_meta) |
| 126 | + assert start < span.start < span.start + span.duration < end |
| 127 | + |
| 128 | + def test_cache_memcached_get(self): |
| 129 | + # get the redis cache |
| 130 | + cache = caches['python_memcached'] |
| 131 | + |
| 132 | + # (trace) the cache miss |
| 133 | + start = time.time() |
| 134 | + hit = cache.get('missing_key') |
| 135 | + end = time.time() |
| 136 | + |
| 137 | + # tests |
| 138 | + spans = self.tracer.writer.pop() |
| 139 | + eq_(len(spans), 1) |
| 140 | + |
| 141 | + span = spans[0] |
| 142 | + eq_(span.service, 'django') |
| 143 | + eq_(span.resource, 'get') |
| 144 | + eq_(span.name, 'django.cache') |
| 145 | + eq_(span.span_type, 'cache') |
| 146 | + eq_(span.error, 0) |
| 147 | + |
| 148 | + expected_meta = { |
| 149 | + 'django.cache.backend': 'django.core.cache.backends.memcached.MemcachedCache', |
| 150 | + 'django.cache.key': 'missing_key', |
| 151 | + } |
| 152 | + |
| 153 | + eq_(span.meta, expected_meta) |
| 154 | + assert start < span.start < span.start + span.duration < end |
| 155 | + |
| 156 | + def test_cache_memcached_get_many(self): |
| 157 | + # get the redis cache |
| 158 | + cache = caches['python_memcached'] |
| 159 | + |
| 160 | + # (trace) the cache miss |
| 161 | + start = time.time() |
| 162 | + hit = cache.get_many(['missing_key', 'another_key']) |
| 163 | + end = time.time() |
| 164 | + |
| 165 | + # tests |
| 166 | + spans = self.tracer.writer.pop() |
| 167 | + eq_(len(spans), 1) |
| 168 | + |
| 169 | + span = spans[0] |
| 170 | + eq_(span.service, 'django') |
| 171 | + eq_(span.resource, 'get_many') |
| 172 | + eq_(span.name, 'django.cache') |
| 173 | + eq_(span.span_type, 'cache') |
| 174 | + eq_(span.error, 0) |
| 175 | + |
| 176 | + expected_meta = { |
| 177 | + 'django.cache.backend': 'django.core.cache.backends.memcached.MemcachedCache', |
| 178 | + 'django.cache.key': str(['missing_key', 'another_key']), |
| 179 | + } |
| 180 | + |
| 181 | + eq_(span.meta, expected_meta) |
| 182 | + assert start < span.start < span.start + span.duration < end |
| 183 | + |
| 184 | + def test_cache_django_pylibmc_get(self): |
| 185 | + # get the redis cache |
| 186 | + cache = caches['django_pylibmc'] |
| 187 | + |
| 188 | + # (trace) the cache miss |
| 189 | + start = time.time() |
| 190 | + hit = cache.get('missing_key') |
| 191 | + end = time.time() |
| 192 | + |
| 193 | + # tests |
| 194 | + spans = self.tracer.writer.pop() |
| 195 | + eq_(len(spans), 1) |
| 196 | + |
| 197 | + span = spans[0] |
| 198 | + eq_(span.service, 'django') |
| 199 | + eq_(span.resource, 'get') |
| 200 | + eq_(span.name, 'django.cache') |
| 201 | + eq_(span.span_type, 'cache') |
| 202 | + eq_(span.error, 0) |
| 203 | + |
| 204 | + expected_meta = { |
| 205 | + 'django.cache.backend': 'django_pylibmc.memcached.PyLibMCCache', |
| 206 | + 'django.cache.key': 'missing_key', |
| 207 | + } |
| 208 | + |
| 209 | + eq_(span.meta, expected_meta) |
| 210 | + assert start < span.start < span.start + span.duration < end |
| 211 | + |
| 212 | + def test_cache_django_pylibmc_get_many(self): |
| 213 | + # get the redis cache |
| 214 | + cache = caches['django_pylibmc'] |
| 215 | + |
| 216 | + # (trace) the cache miss |
| 217 | + start = time.time() |
| 218 | + hit = cache.get_many(['missing_key', 'another_key']) |
| 219 | + end = time.time() |
| 220 | + |
| 221 | + # tests |
| 222 | + spans = self.tracer.writer.pop() |
| 223 | + eq_(len(spans), 1) |
| 224 | + |
| 225 | + span = spans[0] |
| 226 | + eq_(span.service, 'django') |
| 227 | + eq_(span.resource, 'get_many') |
| 228 | + eq_(span.name, 'django.cache') |
| 229 | + eq_(span.span_type, 'cache') |
| 230 | + eq_(span.error, 0) |
| 231 | + |
| 232 | + expected_meta = { |
| 233 | + 'django.cache.backend': 'django_pylibmc.memcached.PyLibMCCache', |
| 234 | + 'django.cache.key': str(['missing_key', 'another_key']), |
| 235 | + } |
| 236 | + |
| 237 | + eq_(span.meta, expected_meta) |
| 238 | + assert start < span.start < span.start + span.duration < end |
0 commit comments