Skip to content

Commit 5cae6a3

Browse files
fix(dogpile.cache): parse argument from both args and kwargs (#2734) (#2739)
* Add regression test case The issue is easily reproduced when only providing kwargs to get_or_create. * fix(dogpile.cache): use get_argument_value for extracting args * Add words Co-authored-by: Nicolas Vivet <[email protected]> (cherry picked from commit bcbe3a3) Co-authored-by: Kyle Verhoog <[email protected]>
1 parent 463270c commit 5cae6a3

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

ddtrace/contrib/dogpile_cache/region.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
from ...constants import SPAN_MEASURED_KEY
66
from ...pin import Pin
7+
from ...utils import get_argument_value
78

89

910
def _wrap_get_create(func, instance, args, kwargs):
1011
pin = Pin.get_from(dogpile.cache)
1112
if not pin or not pin.enabled():
1213
return func(*args, **kwargs)
1314

14-
key = args[0]
15+
key = get_argument_value(args, kwargs, 0, "key")
1516
with pin.tracer.trace("dogpile.cache", resource="get_or_create", span_type=SpanTypes.CACHE) as span:
1617
span.set_tag(SPAN_MEASURED_KEY)
1718
span.set_tag("key", key)
@@ -25,7 +26,7 @@ def _wrap_get_create_multi(func, instance, args, kwargs):
2526
if not pin or not pin.enabled():
2627
return func(*args, **kwargs)
2728

28-
keys = args[0]
29+
keys = get_argument_value(args, kwargs, 0, "keys")
2930
with pin.tracer.trace("dogpile.cache", resource="get_or_create_multi", span_type="cache") as span:
3031
span.set_tag(SPAN_MEASURED_KEY)
3132
span.set_tag("keys", keys)

docs/spelling_wordlist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ agentless
1515
analytics
1616
api
1717
app
18+
args
1819
asgi
1920
asyncio
2021
attrs
@@ -43,6 +44,7 @@ ddtrace
4344
deprecations
4445
django
4546
dogpile
47+
dogpile.cache
4648
dogstatsd
4749
elasticsearch
4850
elasticsearch1
@@ -70,6 +72,7 @@ js
7072
kombu
7173
kubernetes
7274
kwarg
75+
kwargs
7376
lifecycle
7477
lookups
7578
mako
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
dogpile.cache: handle both kwargs and args in the wrapper functions (using
5+
only kwargs would result in an IndexError.

tests/contrib/dogpile_cache/test_tracing.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def region(tracer):
2626
patch()
2727
# Setup a simple dogpile cache region for testing.
2828
# The backend is trivial so we can use memory to simplify test setup.
29-
test_region = dogpile.cache.make_region(name="TestRegion")
29+
test_region = dogpile.cache.make_region(name="TestRegion", key_mangler=lambda x: x)
3030
test_region.configure("dogpile.cache.memory")
3131
Pin.override(dogpile.cache, tracer=tracer)
3232
return test_region
@@ -205,3 +205,12 @@ def test_calls_inner_functions_correctly(self, region, mocker):
205205
spy_multi_cache.reset_mock()
206206
assert [6, 10] == multi_cache(3, 5)
207207
assert spy_single_cache.call_count == 0
208+
209+
210+
def test_get_or_create_kwarg_only(region):
211+
"""
212+
When get_or_create is called with only kwargs
213+
The arguments should be handled correctly
214+
"""
215+
assert region.get_or_create(key="key", creator=lambda: 3) == 3
216+
assert region.get_or_create_multi(keys="keys", creator=lambda *args: [1, 2])

0 commit comments

Comments
 (0)