Skip to content

Commit

Permalink
Use function capture for attaching handle_event/4 (#107)
Browse files Browse the repository at this point in the history
The documentation for `:telemetry.attach/4` states that function
capture should be used for the event handler, rather than local
function capture for performance reasons. Using local function capture
also leads to warnings like:

```
https://hexdocs.pm/telemetry/telemetry.html#attach/4
[info] The function passed as a handler with ID {Telemetry.Metrics.ConsoleReporter, [:phoenix, :router_dispatch, :start], #PID<0.576.0>} is a local function.
This means that it is either an anonymous function or a capture of a function without a module specified. That may cause a performance penalty when calling that handler. For more details see the note in `telemetry:attach/4` documentation.
```

Unfortunately, to allow using function capture, I've had to make
`handle_event/4` a public function.

Co-authored-by: Nick Spain <nicholas.spain96@gmail.com>
  • Loading branch information
nick96 and Nick Spain committed May 13, 2023
1 parent 084a430 commit b869701
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/telemetry_metrics/console_reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule Telemetry.Metrics.ConsoleReporter do

for {event, metrics} <- groups do
id = {__MODULE__, event, self()}
:telemetry.attach(id, event, &handle_event/4, {metrics, device})
:telemetry.attach(id, event, &__MODULE__.handle_event/4, {metrics, device})
end

{:ok, Map.keys(groups)}
Expand All @@ -72,7 +72,7 @@ defmodule Telemetry.Metrics.ConsoleReporter do
:ok
end

defp handle_event(event_name, measurements, metadata, {metrics, device}) do
def handle_event(event_name, measurements, metadata, {metrics, device}) do
prelude = """
[#{inspect(__MODULE__)}] Got new event!
Event name: #{Enum.join(event_name, ".")}
Expand Down

0 comments on commit b869701

Please sign in to comment.