Skip to content

Commit 5bd54f6

Browse files
mergify[bot]Kyle-Verhoogmabdinur
authored
fix(flask-caching): support v2.0 (#3882) (#4050)
## Description flask-caching released version 2.0 which moves the cache client to a new library called [`cachelib`](https://github.com/pallets-eco/cachelib). Diff here: pallets-eco/flask-caching@v1.10.0...v2.0.0 This breaks our logic which collects tags for the redis client. ## Fix It was noticed that both `_write_client` and `_read_client` use the same connection information so we don't need to dynamically check for each. Also [`_read_clients`](https://github.com/pallets-eco/flask-caching/blob/ac13e01baa11d65c68dcba64f99ccaafd9b5419c/src/flask_caching/backends/rediscache.py#L62) was moved to [`_read_client`](https://github.com/pallets-eco/cachelib/blob/13c8bf8db156dd06154f5e08e23bdd9e73480a5b/src/cachelib/redis.py#L29) when the logic was moved to `cachelib`. (cherry picked from commit 94b0770) Co-authored-by: Kyle Verhoog <[email protected]> Co-authored-by: Munir Abdinur <[email protected]>
1 parent 987ef91 commit 5bd54f6

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

ddtrace/contrib/flask_cache/tracers.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import logging
6+
import typing
67

78
from ddtrace import config
89

@@ -14,6 +15,10 @@
1415
from .utils import _resource_from_cache_prefix
1516

1617

18+
if typing.TYPE_CHECKING:
19+
from ddtrace import Span
20+
21+
1722
log = logging.Logger(__name__)
1823

1924
DEFAULT_SERVICE = config.service or "flask-cache"
@@ -55,7 +60,8 @@ class TracedCache(cache_cls):
5560
_datadog_service = service
5661
_datadog_meta = meta
5762

58-
def __trace(self, cmd, write=False):
63+
def __trace(self, cmd):
64+
# type: (str, bool) -> Span
5965
"""
6066
Start a tracing with default attributes and tags
6167
"""
@@ -68,7 +74,7 @@ def __trace(self, cmd, write=False):
6874
# set analytics sample rate
6975
s.set_tag(ANALYTICS_SAMPLE_RATE_KEY, config.flask_cache.get_analytics_sample_rate())
7076
# add connection meta if there is one
71-
client = _extract_client(self.cache, write=write)
77+
client = _extract_client(self.cache)
7278
if client is not None:
7379
try:
7480
s.set_tags(_extract_conn_tags(client))
@@ -91,7 +97,7 @@ def set(self, *args, **kwargs):
9197
"""
9298
Track ``set`` operation
9399
"""
94-
with self.__trace("flask_cache.cmd", write=True) as span:
100+
with self.__trace("flask_cache.cmd") as span:
95101
span.resource = _resource_from_cache_prefix("SET", self.config)
96102
if len(args) > 0:
97103
span.set_tag(COMMAND_KEY, args[0])
@@ -101,7 +107,7 @@ def add(self, *args, **kwargs):
101107
"""
102108
Track ``add`` operation
103109
"""
104-
with self.__trace("flask_cache.cmd", write=True) as span:
110+
with self.__trace("flask_cache.cmd") as span:
105111
span.resource = _resource_from_cache_prefix("ADD", self.config)
106112
if len(args) > 0:
107113
span.set_tag(COMMAND_KEY, args[0])
@@ -111,7 +117,7 @@ def delete(self, *args, **kwargs):
111117
"""
112118
Track ``delete`` operation
113119
"""
114-
with self.__trace("flask_cache.cmd", write=True) as span:
120+
with self.__trace("flask_cache.cmd") as span:
115121
span.resource = _resource_from_cache_prefix("DELETE", self.config)
116122
if len(args) > 0:
117123
span.set_tag(COMMAND_KEY, args[0])
@@ -121,7 +127,7 @@ def delete_many(self, *args, **kwargs):
121127
"""
122128
Track ``delete_many`` operation
123129
"""
124-
with self.__trace("flask_cache.cmd", write=True) as span:
130+
with self.__trace("flask_cache.cmd") as span:
125131
span.resource = _resource_from_cache_prefix("DELETE_MANY", self.config)
126132
span.set_tag(COMMAND_KEY, list(args))
127133
return super(TracedCache, self).delete_many(*args, **kwargs)
@@ -130,7 +136,7 @@ def clear(self, *args, **kwargs):
130136
"""
131137
Track ``clear`` operation
132138
"""
133-
with self.__trace("flask_cache.cmd", write=True) as span:
139+
with self.__trace("flask_cache.cmd") as span:
134140
span.resource = _resource_from_cache_prefix("CLEAR", self.config)
135141
return super(TracedCache, self).clear(*args, **kwargs)
136142

@@ -147,7 +153,7 @@ def set_many(self, *args, **kwargs):
147153
"""
148154
Track ``set_many`` operation
149155
"""
150-
with self.__trace("flask_cache.cmd", write=True) as span:
156+
with self.__trace("flask_cache.cmd") as span:
151157
span.resource = _resource_from_cache_prefix("SET_MANY", self.config)
152158
if len(args) > 0:
153159
span.set_tag(COMMAND_KEY, list(args[0].keys()))

ddtrace/contrib/flask_cache/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ def _resource_from_cache_prefix(resource, cache):
1717
return name.lower()
1818

1919

20-
def _extract_client(cache, write=False):
20+
def _extract_client(cache):
2121
"""
2222
Get the client from the cache instance according to the current operation
2323
"""
2424
client = getattr(cache, "_client", None)
2525
if client is None:
2626
# flask-caching has _read_clients & _write_client for the redis backend
27-
client = getattr(cache, "_write_client" if write else "_read_clients", None)
27+
# These use the same connection so just try to get a reference to one of them.
28+
# flask-caching < 2.0.0 uses _read_clients so look for that one too.
29+
for attr in ("_write_client", "_read_client", "_read_clients"):
30+
client = getattr(cache, attr, None)
31+
if client is not None:
32+
break
2833
return client
2934

3035

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
flask_caching: fix redis tagging after the v2.0 release.

0 commit comments

Comments
 (0)