@@ -21,18 +21,20 @@ defmodule Nebulex.Caching.Decorators.Runtime do
2121 context = Context . get ( )
2222 cache = eval_cache ( cache , context )
2323 key = eval_key ( key , context )
24+ opts = add_telemetry_metadata ( opts , context )
2425
2526 do_eval_cacheable ( cache , key , references , opts , match , on_error , block_fun )
2627 end
2728
2829 @ doc false
29- @ spec eval_cache_evict ( any ( ) , any ( ) , boolean ( ) , boolean ( ) , atom ( ) , fun ( ) ) :: any ( )
30- def eval_cache_evict ( cache , key , before? , all_entries? , on_error , block_fun ) do
30+ @ spec eval_cache_evict ( any ( ) , any ( ) , keyword ( ) , boolean ( ) , boolean ( ) , atom ( ) , fun ( ) ) :: any ( )
31+ def eval_cache_evict ( cache , key , opts , before? , all_entries? , on_error , block_fun ) do
3132 context = Context . get ( )
3233 cache = eval_cache ( cache , context )
3334 key = eval_key ( key , context )
35+ opts = add_telemetry_metadata ( opts , context )
3436
35- do_eval_cache_evict ( cache , key , before? , all_entries? , on_error , block_fun )
37+ do_eval_cache_evict ( cache , key , opts , before? , all_entries? , on_error , block_fun )
3638 end
3739
3840 @ doc false
@@ -41,6 +43,7 @@ defmodule Nebulex.Caching.Decorators.Runtime do
4143 context = Context . get ( )
4244 cache = eval_cache ( cache , context )
4345 key = eval_key ( key , context )
46+ opts = add_telemetry_metadata ( opts , context )
4447
4548 do_eval_cache_put ( cache , key , value , opts , on_error , match )
4649 end
@@ -145,7 +148,7 @@ defmodule Nebulex.Caching.Decorators.Runtime do
145148 fn value ->
146149 with false <- do_eval_cache_put ( ref_cache , ref_key , value , opts , on_error , match ) do
147150 # The match returned `false`, remove the parent's key reference
148- _ignore = do_apply ( cache , :delete , [ key ] )
151+ _ignore = do_apply ( cache , :delete , [ key , Keyword . take ( opts , [ :telemetry_metadata ] ) ] )
149152
150153 false
151154 end
@@ -154,7 +157,7 @@ defmodule Nebulex.Caching.Decorators.Runtime do
154157 case eval_function ( match , value ) do
155158 false ->
156159 # Remove the parent's key reference
157- _ignore = do_apply ( cache , :delete , [ key ] )
160+ _ignore = do_apply ( cache , :delete , [ key , Keyword . take ( opts , [ :telemetry_metadata ] ) ] )
158161
159162 block_fun . ( )
160163
@@ -227,48 +230,48 @@ defmodule Nebulex.Caching.Decorators.Runtime do
227230 raise reason
228231 end
229232
230- defp do_eval_cache_evict ( cache , key , true , all_entries? , on_error , block_fun ) do
231- _ignore = do_evict ( all_entries? , cache , key , on_error )
233+ defp do_eval_cache_evict ( cache , key , opts , true , all_entries? , on_error , block_fun ) do
234+ _ignore = do_evict ( all_entries? , cache , key , opts , on_error )
232235
233236 block_fun . ( )
234237 end
235238
236- defp do_eval_cache_evict ( cache , key , false , all_entries? , on_error , block_fun ) do
239+ defp do_eval_cache_evict ( cache , key , opts , false , all_entries? , on_error , block_fun ) do
237240 result = block_fun . ( )
238241
239- _ignore = do_evict ( all_entries? , cache , key , on_error )
242+ _ignore = do_evict ( all_entries? , cache , key , opts , on_error )
240243
241244 result
242245 end
243246
244- defp do_evict ( false , cache , { :in , keys } , on_error ) do
247+ defp do_evict ( false , cache , { :in , keys } , opts , on_error ) do
245248 keys
246249 |> group_by_cache ( cache )
247250 |> Enum . each ( fn
248251 { cache , [ key ] } ->
249- run_cmd ( cache , :delete , [ key , [ ] ] , on_error )
252+ run_cmd ( cache , :delete , [ key , opts ] , on_error )
250253
251254 { cache , keys } ->
252- run_cmd ( cache , :delete_all , [ [ in: keys ] ] , on_error )
255+ run_cmd ( cache , :delete_all , [ [ in: keys ] , opts ] , on_error )
253256 end )
254257 end
255258
256- defp do_evict ( false , cache , { :query , _ } = q , on_error ) do
257- run_cmd ( cache , :delete_all , [ [ q ] ] , on_error )
259+ defp do_evict ( false , cache , { :query , _ } = q , opts , on_error ) do
260+ run_cmd ( cache , :delete_all , [ [ q ] , opts ] , on_error )
258261 end
259262
260- defp do_evict ( false , cache , { :query , q , k } , on_error ) do
261- _ignore = run_cmd ( cache , :delete_all , [ [ { : query, q } ] ] , on_error )
263+ defp do_evict ( false , cache , { :query , q , k } , opts , on_error ) do
264+ _ignore = run_cmd ( cache , :delete_all , [ [ query: q ] , opts ] , on_error )
262265
263- do_evict ( false , cache , k , on_error )
266+ do_evict ( false , cache , k , opts , on_error )
264267 end
265268
266- defp do_evict ( false , cache , key , on_error ) do
267- run_cmd ( cache , :delete , [ key , [ ] ] , on_error )
269+ defp do_evict ( false , cache , key , opts , on_error ) do
270+ run_cmd ( cache , :delete , [ key , opts ] , on_error )
268271 end
269272
270- defp do_evict ( true , cache , _key , on_error ) do
271- run_cmd ( cache , :delete_all , [ ] , on_error )
273+ defp do_evict ( true , cache , _key , opts , on_error ) do
274+ run_cmd ( cache , :delete_all , [ [ query: nil ] , opts ] , on_error )
272275 end
273276
274277 defp do_eval_cache_put (
@@ -354,6 +357,25 @@ defmodule Nebulex.Caching.Decorators.Runtime do
354357 )
355358 end
356359
360+ defp add_telemetry_metadata ( opts , % Context {
361+ decorator: decorator ,
362+ module: m ,
363+ function_name: f ,
364+ arity: a
365+ } ) do
366+ ctx = % {
367+ decorator: decorator ,
368+ module: m ,
369+ function_name: f ,
370+ arity: a
371+ }
372+
373+ Keyword . update ( opts , :telemetry_metadata , % { decorator_context: ctx } , fn
374+ meta when is_map ( meta ) -> Map . put ( meta , :decorator_context , ctx )
375+ meta -> meta
376+ end )
377+ end
378+
357379 @ compile inline: [ raise_invalid_cache: 1 ]
358380 @ spec raise_invalid_cache ( any ( ) ) :: no_return ( )
359381 defp raise_invalid_cache ( cache ) do
0 commit comments