Skip to content

Commit 81e5277

Browse files
authored
Add support for stats via Nebulex.Telemetry.StatsHandler (#29)
Add support for stats via `Nebulex.Telemetry.StatsHandler`
1 parent 79ebdbb commit 81e5277

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/nebulex_redis_adapter.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule NebulexRedisAdapter.StatsTest do
2+
use ExUnit.Case, async: true
3+
4+
defmodule Cache do
5+
use Nebulex.Cache,
6+
otp_app: :nebulex,
7+
adapter: NebulexRedisAdapter
8+
end
9+
10+
setup do
11+
{:ok, pid} = Cache.start_link(stats: true, conn_opts: [database: 1])
12+
13+
on_exit(fn ->
14+
:ok = Process.sleep(100)
15+
if Process.alive?(pid), do: Cache.stop(pid)
16+
end)
17+
18+
{:ok, cache: Cache, name: Cache}
19+
end
20+
21+
describe "c:NebulexRedisAdapter.stats/1" do
22+
test "returns valid %Stats{}" do
23+
size = Cache.count_all()
24+
refute Cache.get("stats")
25+
assert Cache.put("stats", "stats") == :ok
26+
assert Cache.get("stats") == "stats"
27+
assert Cache.put("stats", "stats") == :ok
28+
assert Cache.take("stats") == "stats"
29+
refute Cache.get("stats")
30+
assert Cache.put_new("stats", "stats")
31+
assert Cache.replace("stats", "stats stats")
32+
assert Cache.delete_all() == size + 1
33+
34+
assert stats = Cache.stats()
35+
assert stats.measurements.evictions == size + 2
36+
assert stats.measurements.hits == 2
37+
assert stats.measurements.misses == 2
38+
assert stats.measurements.writes == 3
39+
assert stats.measurements.updates == 1
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)