2020 from typing import Optional
2121
2222
23+ METHODS_TO_INSTRUMENT = [
24+ "set" ,
25+ "set_many" ,
26+ "get" ,
27+ "get_many" ,
28+ ]
29+
30+
2331def _get_span_description (method_name , args , kwargs ):
2432 # type: (str, tuple[Any], dict[str, Any]) -> str
2533 return _key_as_string (_get_safe_key (method_name , args , kwargs ))
@@ -34,58 +42,6 @@ def _set_address_and_port(span, address, port):
3442 span .set_data (SPANDATA .NETWORK_PEER_PORT , port )
3543
3644
37- def _patch_get_cache (cache , address , port ):
38- # type: (CacheHandler, Optional[str], Optional[int]) -> None
39- from sentry_sdk .integrations .django import DjangoIntegration
40-
41- original_method = cache .get
42-
43- @ensure_integration_enabled (DjangoIntegration , original_method )
44- def _instrument_call (cache , original_method , args , kwargs , address , port ):
45- # type: (CacheHandler, Callable[..., Any], tuple[Any, ...], dict[str, Any], Optional[str], Optional[int]) -> Any
46- op = OP .CACHE_GET
47- description = _get_span_description ("get" , args , kwargs )
48-
49- default_value = None
50- if len (args ) >= 2 :
51- default_value = args [1 ]
52- elif "default" in kwargs :
53- default_value = kwargs ["default" ]
54-
55- with sentry_sdk .start_span (
56- op = op ,
57- name = description ,
58- origin = DjangoIntegration .origin ,
59- ) as span :
60- value = original_method (* args , ** kwargs )
61-
62- with capture_internal_exceptions ():
63- _set_address_and_port (span , address , port )
64-
65- key = _get_safe_key ("get" , args , kwargs )
66- if key is not None :
67- span .set_data (SPANDATA .CACHE_KEY , key )
68-
69- item_size = None
70- if value != default_value :
71- item_size = len (str (value ))
72- span .set_data (SPANDATA .CACHE_HIT , True )
73- else :
74- span .set_data (SPANDATA .CACHE_HIT , False )
75-
76- if item_size is not None :
77- span .set_data (SPANDATA .CACHE_ITEM_SIZE , item_size )
78-
79- return value
80-
81- @functools .wraps (original_method )
82- def sentry_method (* args , ** kwargs ):
83- # type: (*Any, **Any) -> Any
84- return _instrument_call (cache , original_method , args , kwargs , address , port )
85-
86- setattr (cache , "get" , sentry_method )
87-
88-
8945def _patch_cache_method (cache , method_name , address , port ):
9046 # type: (CacheHandler, str, Optional[str], Optional[int]) -> None
9147 from sentry_sdk .integrations .django import DjangoIntegration
@@ -98,7 +54,8 @@ def _instrument_call(
9854 ):
9955 # type: (CacheHandler, str, Callable[..., Any], tuple[Any, ...], dict[str, Any], Optional[str], Optional[int]) -> Any
10056 is_set_operation = method_name .startswith ("set" )
101- is_get_operation = not is_set_operation
57+ is_get_method = method_name == "get"
58+ is_get_many_method = method_name == "get_many"
10259
10360 op = OP .CACHE_PUT if is_set_operation else OP .CACHE_GET
10461 description = _get_span_description (method_name , args , kwargs )
@@ -118,12 +75,24 @@ def _instrument_call(
11875 span .set_data (SPANDATA .CACHE_KEY , key )
11976
12077 item_size = None
121- if is_get_operation :
78+ if is_get_many_method :
12279 if value != {}:
12380 item_size = len (str (value ))
12481 span .set_data (SPANDATA .CACHE_HIT , True )
12582 else :
12683 span .set_data (SPANDATA .CACHE_HIT , False )
84+ elif is_get_method :
85+ default_value = None
86+ if len (args ) >= 2 :
87+ default_value = args [1 ]
88+ elif "default" in kwargs :
89+ default_value = kwargs ["default" ]
90+
91+ if value != default_value :
92+ item_size = len (str (value ))
93+ span .set_data (SPANDATA .CACHE_HIT , True )
94+ else :
95+ span .set_data (SPANDATA .CACHE_HIT , False )
12796 else : # TODO: We don't handle `get_or_set` which we should
12897 arg_count = len (args )
12998 if arg_count >= 2 :
@@ -151,11 +120,8 @@ def sentry_method(*args, **kwargs):
151120def _patch_cache (cache , address = None , port = None ):
152121 # type: (CacheHandler, Optional[str], Optional[int]) -> None
153122 if not hasattr (cache , "_sentry_patched" ):
154- _patch_cache_method (cache , "set" , address , port )
155- _patch_cache_method (cache , "set_many" , address , port )
156- # Separate patch to account for custom default values on cache misses.
157- _patch_get_cache (cache , address , port )
158- _patch_cache_method (cache , "get_many" , address , port )
123+ for method_name in METHODS_TO_INSTRUMENT :
124+ _patch_cache_method (cache , method_name , address , port )
159125 cache ._sentry_patched = True
160126
161127
0 commit comments