@@ -300,7 +300,12 @@ defmodule NebulexRedisAdapter do
300300 import Nebulex.Helpers
301301 import NebulexRedisAdapter.Encoder
302302
303+ use Nebulex.Adapter.Stats
304+
303305 alias Nebulex.Adapter
306+ alias Nebulex.Adapter.Stats
307+ alias Nebulex.Telemetry
308+ alias Nebulex.Telemetry.StatsHandler
304309 alias NebulexRedisAdapter . { ClientCluster , Command , Connection , RedisCluster }
305310
306311 ## Nebulex.Adapter
@@ -361,6 +366,9 @@ defmodule NebulexRedisAdapter do
361366 { node_name , Keyword . get ( node_opts , :pool_size , System . schedulers_online ( ) ) }
362367 end
363368
369+ # init stats
370+ stats_counter = Stats . init ( opts )
371+
364372 child_spec =
365373 Nebulex.Adapters.Supervisor . child_spec (
366374 name: normalize_module_name ( [ name , Supervisor ] ) ,
@@ -374,14 +382,29 @@ defmodule NebulexRedisAdapter do
374382 keyslot: keyslot ,
375383 nodes: nodes ,
376384 pool_size: pool_size ,
385+ stats_counter: stats_counter ,
386+ started_at: DateTime . utc_now ( ) ,
377387 default_dt: Keyword . get ( opts , :default_data_type , :object ) ,
378388 telemetry: Keyword . fetch! ( opts , :telemetry ) ,
379389 telemetry_prefix: Keyword . fetch! ( opts , :telemetry_prefix )
380390 }
381391
392+ _ = maybe_attach_stats_handler ( meta )
393+
382394 { :ok , child_spec , meta }
383395 end
384396
397+ defp maybe_attach_stats_handler ( % { stats_counter: nil } ) , do: :ok
398+
399+ defp maybe_attach_stats_handler ( % { stats_counter: stats_counter } = meta ) do
400+ Telemetry . attach_many (
401+ stats_counter ,
402+ [ meta . telemetry_prefix ++ [ :command , :stop ] ] ,
403+ & StatsHandler . handle_event / 4 ,
404+ stats_counter
405+ )
406+ end
407+
385408 defp do_init ( :standalone , name , pool_size , opts ) do
386409 { :ok , children } = Connection . init ( name , pool_size , opts )
387410 { children , ClientCluster.Keyslot }
@@ -622,6 +645,13 @@ defmodule NebulexRedisAdapter do
622645 )
623646 end
624647
648+ @ impl Nebulex.Adapter.Stats
649+ def stats ( % { started_at: started_at } = adapter_meta ) do
650+ if stats = super ( adapter_meta ) do
651+ % { stats | metadata: Map . put ( stats . metadata , :started_at , started_at ) }
652+ end
653+ end
654+
625655 ## Private Functions
626656
627657 defp with_pipeline ( adapter_meta , key , pipeline ) do
0 commit comments