Skip to content

Commit d2618d0

Browse files
committed
Add a ready-to-use debug Telemetry handler
1 parent bbbe44c commit d2618d0

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

.iex.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ cacheinfo = fn() ->
5656
IO.puts "size: #{size}"
5757
:ets.i(:fun_with_flags_cache)
5858
end
59+
60+
# FunWithFlags.Telemetry.attach_debug_handler()

lib/fun_with_flags/telemetry.ex

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,39 @@ defmodule FunWithFlags.Telemetry do
136136

137137
:telemetry.execute(event_name, measurements, metadata)
138138
end
139+
140+
141+
@doc """
142+
Attach a debug handler to FunWithFlags telemetry events.
143+
144+
Attach a Telemetry handler that logs all events at the `:alert` level.
145+
It uses the `:alert` level rather than `:debug` or `:info` simply to make it
146+
more convenient to eyeball these logs and to print them while running the tests.
147+
"""
148+
@spec attach_debug_handler() :: :ok | {:error, :already_exists}
149+
def attach_debug_handler do
150+
events = [
151+
[:fun_with_flags, :persistence, :read],
152+
[:fun_with_flags, :persistence, :read_all_flags],
153+
[:fun_with_flags, :persistence, :read_all_flag_names],
154+
[:fun_with_flags, :persistence, :write],
155+
[:fun_with_flags, :persistence, :delete_flag],
156+
[:fun_with_flags, :persistence, :delete_gate],
157+
[:fun_with_flags, :persistence, :reload],
158+
[:fun_with_flags, :persistence, :error],
159+
]
160+
161+
:telemetry.attach_many("local-debug-handler", events, &__MODULE__.debug_event_handler/4, %{})
162+
end
163+
164+
@doc false
165+
def debug_event_handler([:fun_with_flags, :persistence, event], %{system_time: system_time}, metadata, _config) do
166+
dt = DateTime.from_unix!(system_time, :native) |> DateTime.to_iso8601()
167+
168+
Logger.alert(fn ->
169+
"FunWithFlags telemetry event: #{event}, system_time: #{dt}, metadata: #{inspect(metadata)}"
170+
end)
171+
172+
:ok
173+
end
139174
end

test/fun_with_flags/telemetry_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,16 @@ defmodule FunWithFlags.TelemetryTest do
7373
:telemetry.detach(ref)
7474
end
7575
end
76+
77+
describe "attach_debug_handler" do
78+
test "it attaches a debug handler to FunWithFlags telemetry events" do
79+
assert :telemetry_handler_table.list_by_prefix([:fun_with_flags]) == []
80+
81+
FWFTel.attach_debug_handler()
82+
83+
assert length(:telemetry_handler_table.list_by_prefix([:fun_with_flags])) == 8
84+
85+
:telemetry.detach("local-debug-handler")
86+
end
87+
end
7688
end

test/test_helper.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ if does_anything_need_redis do
4141
FunWithFlags.TestUtils.use_redis_test_db()
4242
end
4343

44+
# FunWithFlags.Telemetry.attach_debug_handler()
45+
4446
ExUnit.start()
4547

4648
if FunWithFlags.Config.persist_in_ecto? do

0 commit comments

Comments
 (0)