Skip to content

Commit 941da5d

Browse files
committed
[#6] Instrument the adapter with Telemetry events
1 parent 206dff6 commit 941da5d

File tree

4 files changed

+94
-61
lines changed

4 files changed

+94
-61
lines changed

lib/nebulex/adapters/cachex.ex

Lines changed: 78 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ defmodule Nebulex.Adapters.Cachex do
8787
8888
> See [Cachex.start_link/1][cachex_start_link] for more information.
8989
90+
## Telemetry events
91+
92+
This adapter emits the recommended Telemetry events.
93+
See the "Telemetry events" section in `Nebulex.Cache`
94+
for more information.
95+
9096
## Distributed caching topologies
9197
9298
In the same way we use the distributed adapters and the multilevel one to
@@ -146,6 +152,7 @@ defmodule Nebulex.Adapters.Cachex do
146152
# Inherit default transaction implementation
147153
use Nebulex.Adapter.Transaction
148154

155+
import Nebulex.Adapter
149156
import Nebulex.Helpers
150157

151158
alias Cachex.{Options, Query}
@@ -166,33 +173,33 @@ defmodule Nebulex.Adapters.Cachex do
166173
Cachex
167174
])
168175

169-
stats =
170-
if opts[:stats_counter] do
171-
true
172-
else
173-
Options.get(opts, :stats, &is_boolean/1, false)
174-
end
176+
adapter_meta = %{
177+
name: name,
178+
telemetry: Keyword.fetch!(opts, :telemetry),
179+
telemetry_prefix: Keyword.fetch!(opts, :telemetry_prefix),
180+
stats: Options.get(opts, :stats, &is_boolean/1, false)
181+
}
175182

176183
child_spec =
177184
opts
178185
|> Keyword.put(:name, name)
179-
|> Keyword.put(:stats, stats)
186+
|> Keyword.put(:stats, adapter_meta.stats)
180187
|> Cachex.child_spec()
181188

182-
{:ok, child_spec, %{name: name, stats: stats}}
189+
{:ok, child_spec, adapter_meta}
183190
end
184191

185192
## Nebulex.Adapter.Entry
186193

187194
@impl true
188-
def get(%{name: name}, key, _opts) do
189-
Cachex.get!(name, key)
195+
defspan get(adapter_meta, key, _opts) do
196+
Cachex.get!(adapter_meta.name, key)
190197
end
191198

192199
@impl true
193-
def get_all(%{name: name}, keys, _opts) do
200+
defspan get_all(adapter_meta, keys, _opts) do
194201
Enum.reduce(keys, %{}, fn key, acc ->
195-
if value = Cachex.get!(name, key) do
202+
if value = Cachex.get!(adapter_meta.name, key) do
196203
Map.put(acc, key, value)
197204
else
198205
acc
@@ -201,15 +208,19 @@ defmodule Nebulex.Adapters.Cachex do
201208
end
202209

203210
@impl true
204-
def put(%{name: name}, key, value, ttl, :put, _opts) do
211+
defspan put(adapter_meta, key, value, ttl, on_write, _opts) do
212+
do_put(adapter_meta.name, key, value, ttl, on_write)
213+
end
214+
215+
defp do_put(name, key, value, ttl, :put) do
205216
Cachex.put!(name, key, value, ttl: to_ttl(ttl))
206217
end
207218

208-
def put(%{name: name}, key, value, ttl, :replace, _opts) do
219+
defp do_put(name, key, value, ttl, :replace) do
209220
Cachex.update!(name, key, value, ttl: to_ttl(ttl))
210221
end
211222

212-
def put(%{name: name}, key, value, ttl, :put_new, _opts) do
223+
defp do_put(name, key, value, ttl, :put_new) do
213224
# FIXME: This is a workaround since Cachex does not support a direct action
214225
# for put_new. Fix it if a better solution comes up.
215226
if Cachex.get!(name, key) do
@@ -220,15 +231,19 @@ defmodule Nebulex.Adapters.Cachex do
220231
end
221232

222233
@impl true
223-
def put_all(adapter_meta, entries, ttl, on_write, opts) when is_map(entries) do
224-
put_all(adapter_meta, :maps.to_list(entries), ttl, on_write, opts)
234+
defspan put_all(adapter_meta, entries, ttl, on_write, _opts) do
235+
do_put_all(adapter_meta.name, entries, ttl, on_write)
236+
end
237+
238+
defp do_put_all(name, entries, ttl, on_write) when is_map(entries) do
239+
do_put_all(name, :maps.to_list(entries), ttl, on_write)
225240
end
226241

227-
def put_all(%{name: name}, entries, ttl, :put, _opts) when is_list(entries) do
242+
defp do_put_all(name, entries, ttl, :put) when is_list(entries) do
228243
Cachex.put_many!(name, entries, ttl: to_ttl(ttl))
229244
end
230245

231-
def put_all(%{name: name}, entries, ttl, :put_new, _opts) when is_list(entries) do
246+
defp do_put_all(name, entries, ttl, :put_new) when is_list(entries) do
232247
{keys, _} = Enum.unzip(entries)
233248

234249
# FIXME: This is a workaround since Cachex does not support a direct action
@@ -243,31 +258,31 @@ defmodule Nebulex.Adapters.Cachex do
243258
end
244259

245260
@impl true
246-
def delete(%{name: name}, key, _opts) do
247-
true = Cachex.del!(name, key)
261+
defspan delete(adapter_meta, key, _opts) do
262+
true = Cachex.del!(adapter_meta.name, key)
248263
:ok
249264
end
250265

251266
@impl true
252-
def take(%{name: name}, key, _opts) do
253-
Cachex.take!(name, key)
267+
defspan take(adapter_meta, key, _opts) do
268+
Cachex.take!(adapter_meta.name, key)
254269
end
255270

256271
@impl true
257-
def has_key?(%{name: name}, key) do
258-
{:ok, bool} = Cachex.exists?(name, key)
272+
defspan has_key?(adapter_meta, key) do
273+
{:ok, bool} = Cachex.exists?(adapter_meta.name, key)
259274
bool
260275
end
261276

262277
@impl true
263-
def ttl(%{name: name}, key) do
278+
defspan ttl(adapter_meta, key) do
264279
cond do
265280
# Key does exist and has a TTL associated with it
266-
ttl = Cachex.ttl!(name, key) ->
281+
ttl = Cachex.ttl!(adapter_meta.name, key) ->
267282
ttl
268283

269284
# Key does exist and hasn't a TTL associated with it
270-
Cachex.get!(name, key) ->
285+
Cachex.get!(adapter_meta.name, key) ->
271286
:infinity
272287

273288
# Key does not exist
@@ -277,21 +292,25 @@ defmodule Nebulex.Adapters.Cachex do
277292
end
278293

279294
@impl true
280-
def expire(%{name: name}, key, ttl) do
281-
Cachex.expire!(name, key, to_ttl(ttl))
295+
defspan expire(adapter_meta, key, ttl) do
296+
Cachex.expire!(adapter_meta.name, key, to_ttl(ttl))
282297
end
283298

284299
@impl true
285-
def touch(%{name: name}, key) do
286-
Cachex.touch!(name, key)
300+
defspan touch(adapter_meta, key) do
301+
Cachex.touch!(adapter_meta.name, key)
287302
end
288303

289304
@impl true
290-
def update_counter(%{name: name}, key, amount, :infinity, default, _opts) do
305+
defspan update_counter(adapter_meta, key, amount, ttl, default, _opts) do
306+
do_update_counter(adapter_meta.name, key, amount, ttl, default)
307+
end
308+
309+
defp do_update_counter(name, key, amount, :infinity, default) do
291310
Cachex.incr!(name, key, amount, initial: default)
292311
end
293312

294-
def update_counter(%{name: name}, key, incr, ttl, default, _opts) do
313+
defp do_update_counter(name, key, incr, ttl, default) do
295314
# FIXME: This is a workaround since Cachex does not support `:ttl` here.
296315
# Fix it if a better solution comes up.
297316
Cachex.transaction!(name, [key], fn worker ->
@@ -304,34 +323,42 @@ defmodule Nebulex.Adapters.Cachex do
304323
## Nebulex.Adapter.Queryable
305324

306325
@impl true
307-
def execute(%{name: name}, :count_all, nil, _opts) do
326+
defspan execute(adapter_meta, operation, query, _opts) do
327+
do_execute(adapter_meta.name, operation, query)
328+
end
329+
330+
defp do_execute(name, :count_all, nil) do
308331
Cachex.size!(name)
309332
end
310333

311-
def execute(%{name: name}, :delete_all, nil, _opts) do
334+
defp do_execute(name, :delete_all, nil) do
312335
Cachex.clear!(name)
313336
end
314337

315-
def execute(%{name: name}, :delete_all, :expired, _opts) do
338+
defp do_execute(name, :delete_all, :expired) do
316339
Cachex.purge!(name)
317340
end
318341

319-
def execute(adapter_meta, :all, query, opts) do
320-
adapter_meta
321-
|> stream(query, opts)
342+
defp do_execute(name, :all, query) do
343+
name
344+
|> do_stream(query, [])
322345
|> Enum.to_list()
323346
end
324347

325-
def execute(_adapter_meta, operation, query, _opts) do
348+
defp do_execute(_name, operation, query) do
326349
raise Nebulex.QueryError, message: "unsupported #{operation}", query: query
327350
end
328351

329352
@impl true
330-
def stream(adapter_meta, nil, opts) do
331-
stream(adapter_meta, Query.create(true, :key), opts)
353+
defspan stream(adapter_meta, query, opts) do
354+
do_stream(adapter_meta.name, query, opts)
355+
end
356+
357+
defp do_stream(name, nil, opts) do
358+
do_stream(name, Query.create(true, :key), opts)
332359
end
333360

334-
def stream(%{name: name}, query, opts) do
361+
defp do_stream(name, query, opts) do
335362
query = maybe_return_entry(query, opts[:return])
336363
Cachex.stream!(name, query, batch_size: opts[:page_size] || 20)
337364
rescue
@@ -360,16 +387,16 @@ defmodule Nebulex.Adapters.Cachex do
360387
## Nebulex.Adapter.Persistence
361388

362389
@impl true
363-
def dump(%{name: name}, path, opts) do
364-
case Cachex.dump(name, path, opts) do
390+
defspan dump(adapter_meta, path, opts) do
391+
case Cachex.dump(adapter_meta.name, path, opts) do
365392
{:ok, true} -> :ok
366393
{:error, _} = error -> error
367394
end
368395
end
369396

370397
@impl true
371-
def load(%{name: name}, path, opts) do
372-
case Cachex.load(name, path, opts) do
398+
defspan load(adapter_meta, path, opts) do
399+
case Cachex.load(adapter_meta.name, path, opts) do
373400
{:ok, true} -> :ok
374401
{:error, _} = error -> error
375402
end
@@ -378,10 +405,10 @@ defmodule Nebulex.Adapters.Cachex do
378405
## Nebulex.Adapter.Stats
379406

380407
@impl true
381-
def stats(%{name: name, stats: stats}) do
382-
if stats do
408+
defspan stats(adapter_meta) do
409+
if adapter_meta.stats do
383410
{meta, stats} =
384-
name
411+
adapter_meta.name
385412
|> Cachex.stats!()
386413
|> Map.pop(:meta, %{})
387414

mix.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ defmodule NebulexAdaptersCachex.MixProject do
22
use Mix.Project
33

44
@source_url "https://github.com/cabol/nebulex_adapters_cachex"
5-
@version "2.0.0"
6-
@nbx_vsn "2.0.0"
5+
@version "2.1.0"
6+
@nbx_vsn "2.1.0"
77

88
def project do
99
[
@@ -51,6 +51,7 @@ defmodule NebulexAdaptersCachex.MixProject do
5151
[
5252
nebulex_dep(),
5353
{:cachex, "~> 3.3"},
54+
{:telemetry, "~> 0.4", optional: true},
5455

5556
# Test & Code Analysis
5657
{:excoveralls, "~> 0.13", only: :test},
@@ -80,7 +81,7 @@ defmodule NebulexAdaptersCachex.MixProject do
8081
[
8182
"nbx.setup": [
8283
"cmd rm -rf nebulex",
83-
"cmd git clone --depth 1 --branch v2.0.0 https://github.com/cabol/nebulex"
84+
"cmd git clone --depth 1 --branch v#{@nbx_vsn} https://github.com/cabol/nebulex"
8485
],
8586
check: [
8687
"compile --warnings-as-errors",

0 commit comments

Comments
 (0)