Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(flask-caching): support v2.0 (backport #3882) #3891

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions ddtrace/contrib/flask_cache/tracers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import typing

from ddtrace import config

Expand All @@ -14,6 +15,10 @@
from .utils import _resource_from_cache_prefix


if typing.TYPE_CHECKING:
from ddtrace import Span


log = logging.Logger(__name__)

DEFAULT_SERVICE = config.service or "flask-cache"
Expand Down Expand Up @@ -55,7 +60,8 @@ class TracedCache(cache_cls):
_datadog_service = service
_datadog_meta = meta

def __trace(self, cmd, write=False):
def __trace(self, cmd):
# type: (str, bool) -> Span
"""
Start a tracing with default attributes and tags
"""
Expand All @@ -68,7 +74,7 @@ def __trace(self, cmd, write=False):
# set analytics sample rate
s.set_tag(ANALYTICS_SAMPLE_RATE_KEY, config.flask_cache.get_analytics_sample_rate())
# add connection meta if there is one
client = _extract_client(self.cache, write=write)
client = _extract_client(self.cache)
if client is not None:
try:
s.set_tags(_extract_conn_tags(client))
Expand All @@ -91,7 +97,7 @@ def set(self, *args, **kwargs):
"""
Track ``set`` operation
"""
with self.__trace("flask_cache.cmd", write=True) as span:
with self.__trace("flask_cache.cmd") as span:
span.resource = _resource_from_cache_prefix("SET", self.config)
if len(args) > 0:
span.set_tag(COMMAND_KEY, args[0])
Expand All @@ -101,7 +107,7 @@ def add(self, *args, **kwargs):
"""
Track ``add`` operation
"""
with self.__trace("flask_cache.cmd", write=True) as span:
with self.__trace("flask_cache.cmd") as span:
span.resource = _resource_from_cache_prefix("ADD", self.config)
if len(args) > 0:
span.set_tag(COMMAND_KEY, args[0])
Expand All @@ -111,7 +117,7 @@ def delete(self, *args, **kwargs):
"""
Track ``delete`` operation
"""
with self.__trace("flask_cache.cmd", write=True) as span:
with self.__trace("flask_cache.cmd") as span:
span.resource = _resource_from_cache_prefix("DELETE", self.config)
if len(args) > 0:
span.set_tag(COMMAND_KEY, args[0])
Expand All @@ -121,7 +127,7 @@ def delete_many(self, *args, **kwargs):
"""
Track ``delete_many`` operation
"""
with self.__trace("flask_cache.cmd", write=True) as span:
with self.__trace("flask_cache.cmd") as span:
span.resource = _resource_from_cache_prefix("DELETE_MANY", self.config)
span.set_tag(COMMAND_KEY, list(args))
return super(TracedCache, self).delete_many(*args, **kwargs)
Expand All @@ -130,7 +136,7 @@ def clear(self, *args, **kwargs):
"""
Track ``clear`` operation
"""
with self.__trace("flask_cache.cmd", write=True) as span:
with self.__trace("flask_cache.cmd") as span:
span.resource = _resource_from_cache_prefix("CLEAR", self.config)
return super(TracedCache, self).clear(*args, **kwargs)

Expand All @@ -147,7 +153,7 @@ def set_many(self, *args, **kwargs):
"""
Track ``set_many`` operation
"""
with self.__trace("flask_cache.cmd", write=True) as span:
with self.__trace("flask_cache.cmd") as span:
span.resource = _resource_from_cache_prefix("SET_MANY", self.config)
if len(args) > 0:
span.set_tag(COMMAND_KEY, list(args[0].keys()))
Expand Down
9 changes: 7 additions & 2 deletions ddtrace/contrib/flask_cache/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ def _resource_from_cache_prefix(resource, cache):
return name.lower()


def _extract_client(cache, write=False):
def _extract_client(cache):
"""
Get the client from the cache instance according to the current operation
"""
client = getattr(cache, "_client", None)
if client is None:
# flask-caching has _read_clients & _write_client for the redis backend
client = getattr(cache, "_write_client" if write else "_read_clients", None)
# These use the same connection so just try to get a reference to one of them.
# flask-caching < 2.0.0 uses _read_clients so look for that one too.
for attr in ("_write_client", "_read_client", "_read_clients"):
client = getattr(cache, attr, None)
if client is not None:
break
return client


Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/flask_caching-dc23b94171041b4f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
flask_caching: fix redis tagging after the v2.0 release.