Skip to content

Commit

Permalink
Merge pull request #9 from contested-space/fix/detach-handlers-on-ter…
Browse files Browse the repository at this point in the history
…mination

Fix/detach handlers on termination
  • Loading branch information
rkallos authored Jan 18, 2024
2 parents a52a6a1 + 7d19b13 commit f67e72b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/peep.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ defmodule Peep do

@impl true
def init(options) do
Process.flag(:trap_exit, true)
tid = Storage.new(options.name)

metrics = options.metrics
Expand Down Expand Up @@ -146,6 +147,11 @@ defmodule Peep do
{:noreply, %State{state | statsd_state: new_statsd_state}}
end

@impl true
def terminate(:shutdown, %{handler_ids: handler_ids}) do
EventHandler.detach(handler_ids)
end

defp set_statsd_timer(interval) do
Process.send_after(self(), :statsd_flush, interval)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/peep/event_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ defmodule Peep.EventHandler do
end
end

def detach(handler_ids) do
Enum.each(handler_ids, fn id -> :telemetry.detach(id) end)
end

def handle_event(_event, measurements, metadata, %{
tid: tid,
metrics: metrics,
Expand Down
22 changes: 22 additions & 0 deletions test/peep_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,26 @@ defmodule PeepTest do
assert String.contains?(logs, "Dropping #{inspect(event_name)}")
end
end

test "Handlers are detached on shutdown" do
prefix = [:peep, :shutdown_test]

metric =
Metrics.counter(prefix ++ [:counter])

{:ok, options} =
[
name: :"#{__MODULE__}_shutdown_test",
metrics: [metric]
]
|> Peep.Options.validate()

{:ok, pid} = GenServer.start(Peep, options, name: options.name)

assert length(:telemetry.list_handlers(prefix)) == 1

GenServer.stop(pid, :shutdown)

assert [] == :telemetry.list_handlers(prefix)
end
end

0 comments on commit f67e72b

Please sign in to comment.