Automatically wrap functions in a module with metaprogramming

Hi,

I want to create some helper which would automatically wrap functions with some logic.

defmodule Bar do
  # ...
end

defmodule Test do
  use Bar

  def call() do
    1
  end
end

Test.call()
# log: wrapping start
# log: 1
# log: wrapping end
# => 1

I’m using AppSignal and the way they suggest modifying each function in live view applications looks uneasy to me.

I’m wondering if it’s possible to avoid modifying all live view functions in an application in order to wrap each function with some monitoring logic.

https://hexdocs.pm/decorator/readme.html
I think this is what you are looking for.

1 Like

It looks like they were following this approach but for some reason it can’t be used for live view modules.

However, I’m unsure about the decorators. Since LiveView events can be called to in multiple ways, it seems like it’d be difficult to have decorators that work for all cases. For example, using send and handle_info with the live_view_event decorator currently breaks as there’s no function clause without parameters.