/
utils.py
62 lines (54 loc) · 2.13 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# project
from ...ext import net
from ..pylibmc.addrs import parse_addresses
from ..redis.util import _extract_conn_tags as extract_redis_tags
def _resource_from_cache_prefix(resource, cache):
"""
Combine the resource name with the cache prefix (if any)
"""
if getattr(cache, "key_prefix", None):
name = "{} {}".format(resource, cache.key_prefix)
else:
name = resource
# enforce lowercase to make the output nicer to read
return name.lower()
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
# 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
def _extract_conn_tags(client):
"""
For the given client extracts connection tags
"""
tags = {}
if hasattr(client, "servers"):
# Memcached backend supports an address pool
if isinstance(client.servers, list) and len(client.servers) > 0:
# use the first address of the pool as a host because
# the code doesn't expose more information
contact_point = client.servers[0].address
tags[net.TARGET_HOST] = contact_point[0]
tags[net.TARGET_PORT] = contact_point[1]
elif hasattr(client, "connection_pool"):
# Redis main connection
redis_tags = extract_redis_tags(client.connection_pool.connection_kwargs)
tags.update(**redis_tags)
elif hasattr(client, "addresses"):
# pylibmc
# FIXME[matt] should we memoize this?
addrs = parse_addresses(client.addresses)
if addrs:
_, host, port, _ = addrs[0]
tags[net.TARGET_PORT] = port
tags[net.TARGET_HOST] = host
return tags