Automatically wrap functions in a module with metaprogramming


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

defmodule Bar do
  # ...

defmodule Test do
  use Bar

  def call() do
# 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.
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.