TelemetryDecorator - @decorate functions for :telemetry.span/3

TelemetryDecorator uses Decorator to wrap a function’s body with a :telemetry.span/3 call. I’ve found the results easier to read and maintain than when I make the call explicitly.

To use it, take the dependency and add a @decorate attribute before any function for which you want telemetry:

defmodule MyApp.MyModule do
  use TelemetryDecorator

  @decorate telemetry([:my_app, :succeed])
  def succeed(arg1, arg2) do
    :...
  end
end

Your event metadata will contain:

  • Any variables matched by your arguments for :start , :stop , and :exception events
  • Your function’s result for :stop events (overriding any variable named result )
  • Any other variables you name with the include option

I also include TelemetryDecorator.watch/1 for convenient console watching of any telemetry following the :telemetry.span/3 conventions.

10 Likes

TBH this is probably the only use case of decorator that I find ok with my internal Programmer Zen.

5 Likes