Skip to content

Commit 66292da

Browse files
fix(redis): remove circular import from redis integrations [backport 2.1] (#7799)
Backport 6de31a4 from #7794 to 2.1. This change fixes #7783 by removing the imports of `contrib.redis` code from `contrib.yaaredis` and `contrib.aredis`. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. Co-authored-by: Emmett Butler <[email protected]>
1 parent 6886640 commit 66292da

File tree

6 files changed

+23
-20
lines changed

6 files changed

+23
-20
lines changed

ddtrace/contrib/aioredis/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from ...internal.utils.formats import CMD_MAX_LEN
2424
from ...internal.utils.formats import stringify_cache_args
2525
from .. import trace_utils
26-
from ..redis.asyncio_patch import _run_redis_command_async
2726
from ..trace_utils_redis import ROW_RETURNING_COMMANDS
27+
from ..trace_utils_redis import _run_redis_command_async
2828
from ..trace_utils_redis import _trace_redis_cmd
2929
from ..trace_utils_redis import _trace_redis_execute_pipeline
3030
from ..trace_utils_redis import determine_row_count

ddtrace/contrib/aredis/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ...internal.utils.formats import stringify_cache_args
1111
from ...internal.utils.wrappers import unwrap
1212
from ...pin import Pin
13-
from ..redis.asyncio_patch import _run_redis_command_async
13+
from ..trace_utils_redis import _run_redis_command_async
1414
from ..trace_utils_redis import _trace_redis_cmd
1515
from ..trace_utils_redis import _trace_redis_execute_pipeline
1616

ddtrace/contrib/redis/asyncio_patch.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from ddtrace import config
22

3-
from ...ext import db
43
from ...internal.utils.formats import stringify_cache_args
54
from ...pin import Pin
6-
from ..trace_utils_redis import ROW_RETURNING_COMMANDS
5+
from ..trace_utils_redis import _run_redis_command_async
76
from ..trace_utils_redis import _trace_redis_cmd
87
from ..trace_utils_redis import _trace_redis_execute_async_cluster_pipeline
98
from ..trace_utils_redis import _trace_redis_execute_pipeline
10-
from ..trace_utils_redis import determine_row_count
119

1210

1311
#
@@ -33,20 +31,6 @@ async def traced_async_execute_pipeline(func, instance, args, kwargs):
3331
return await func(*args, **kwargs)
3432

3533

36-
async def _run_redis_command_async(span, func, args, kwargs):
37-
parsed_command = stringify_cache_args(args)
38-
redis_command = parsed_command.split(" ")[0]
39-
try:
40-
result = await func(*args, **kwargs)
41-
if redis_command in ROW_RETURNING_COMMANDS:
42-
determine_row_count(redis_command=redis_command, span=span, result=result)
43-
return result
44-
except Exception:
45-
if redis_command in ROW_RETURNING_COMMANDS:
46-
span.set_metric(db.ROWCOUNT, 0)
47-
raise
48-
49-
5034
async def traced_async_execute_cluster_pipeline(func, instance, args, kwargs):
5135
pin = Pin.get_from(instance)
5236
if not pin or not pin.enabled():

ddtrace/contrib/trace_utils_redis.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,17 @@ def _trace_redis_execute_async_cluster_pipeline(pin, config_integration, resourc
160160
span.set_tag(ANALYTICS_SAMPLE_RATE_KEY, config_integration.get_analytics_sample_rate())
161161
# yield the span in case the caller wants to build on span
162162
yield span
163+
164+
165+
async def _run_redis_command_async(span, func, args, kwargs):
166+
parsed_command = stringify_cache_args(args)
167+
redis_command = parsed_command.split(" ")[0]
168+
try:
169+
result = await func(*args, **kwargs)
170+
if redis_command in ROW_RETURNING_COMMANDS:
171+
determine_row_count(redis_command=redis_command, span=span, result=result)
172+
return result
173+
except Exception:
174+
if redis_command in ROW_RETURNING_COMMANDS:
175+
span.set_metric(db.ROWCOUNT, 0)
176+
raise

ddtrace/contrib/yaaredis/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ...internal.utils.formats import stringify_cache_args
1111
from ...internal.utils.wrappers import unwrap
1212
from ...pin import Pin
13-
from ..redis.asyncio_patch import _run_redis_command_async
13+
from ..trace_utils_redis import _run_redis_command_async
1414
from ..trace_utils_redis import _trace_redis_cmd
1515
from ..trace_utils_redis import _trace_redis_execute_pipeline
1616

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
redis: This fix resolves an issue where the yaaredis and aredis integrations imported
5+
code from the redis integration, causing a circular import error.

0 commit comments

Comments
 (0)