Skip to content

Commit ca2c0ee

Browse files
Support redis.asyncio (#744)
* Support `redis.asyncio` * Fix `flake8` issues Co-authored-by: Timothy Pansino <11214426+TimPansino@users.noreply.github.com>
1 parent 01ceca7 commit ca2c0ee

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

newrelic/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,14 @@ def _process_module_builtin_defaults():
26652665
"aioredis.connection", "newrelic.hooks.datastore_aioredis", "instrument_aioredis_connection"
26662666
)
26672667

2668+
_process_module_definition("redis.asyncio.client", "newrelic.hooks.datastore_aioredis", "instrument_aioredis_client")
2669+
2670+
_process_module_definition("redis.asyncio.commands", "newrelic.hooks.datastore_aioredis", "instrument_aioredis_client")
2671+
2672+
_process_module_definition(
2673+
"redis.asyncio.connection", "newrelic.hooks.datastore_aioredis", "instrument_aioredis_connection"
2674+
)
2675+
26682676
_process_module_definition(
26692677
"elasticsearch.client",
26702678
"newrelic.hooks.datastore_elasticsearch",

newrelic/hooks/datastore_aioredis.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from newrelic.api.datastore_trace import DatastoreTrace, DatastoreTraceWrapper
15+
from newrelic.api.datastore_trace import DatastoreTrace
1616
from newrelic.api.time_trace import current_trace
1717
from newrelic.api.transaction import current_transaction
18-
from newrelic.common.object_wrapper import wrap_function_wrapper, function_wrapper, FunctionWrapper
18+
from newrelic.common.object_wrapper import wrap_function_wrapper, function_wrapper
1919
from newrelic.hooks.datastore_redis import (
2020
_redis_client_methods,
2121
_redis_multipart_commands,
2222
_redis_operation_re,
2323
)
2424

25-
from newrelic.common.async_wrapper import async_wrapper
2625

27-
import aioredis
28-
29-
try:
30-
AIOREDIS_VERSION = lambda: tuple(int(x) for x in getattr(aioredis, "__version__").split("."))
31-
except Exception:
32-
AIOREDIS_VERSION = lambda: (0, 0, 0)
26+
def get_aioredis_version():
27+
try:
28+
import aioredis as aioredis_legacy
29+
except ModuleNotFoundError:
30+
return None
31+
try:
32+
return tuple(int(x) for x in getattr(aioredis_legacy, "__version__").split("."))
33+
except Exception:
34+
return 0, 0, 0
3335

3436

3537
def _conn_attrs_to_dict(connection):
@@ -68,7 +70,8 @@ def _nr_wrapper_AioRedis_method_(wrapped, instance, args, kwargs):
6870
# Check for transaction and return early if found.
6971
# Method will return synchronously without executing,
7072
# it will be added to the command stack and run later.
71-
if AIOREDIS_VERSION() < (2,):
73+
aioredis_version = get_aioredis_version()
74+
if aioredis_version and aioredis_version < (2,):
7275
# AioRedis v1 uses a RedisBuffer instead of a real connection for queueing up pipeline commands
7376
from aioredis.commands.transaction import _RedisBuffer
7477
if isinstance(instance._pool_or_conn, _RedisBuffer):
@@ -77,7 +80,10 @@ def _nr_wrapper_AioRedis_method_(wrapped, instance, args, kwargs):
7780
return wrapped(*args, **kwargs)
7881
else:
7982
# AioRedis v2 uses a Pipeline object for a client and internally queues up pipeline commands
80-
from aioredis.client import Pipeline
83+
if aioredis_version:
84+
from aioredis.client import Pipeline
85+
else:
86+
from redis.asyncio.client import Pipeline
8187
if isinstance(instance, Pipeline):
8288
return wrapped(*args, **kwargs)
8389

0 commit comments

Comments
 (0)