appsignal

AppSignal for Phoenix 2.1: Automatic LiveView Instrumentation

Jeff Kreeftmeijer

Jeff Kreeftmeijer on

AppSignal for Phoenix 2.1: Automatic LiveView Instrumentation

We're very excited to release AppSignal for Phoenix 2.1, which adds automatic instrumentation for LiveView through Telemetry. You can now receive performance and error insights for your LiveView, without manually instrumenting each of its methods.

The Old Way of Instrumenting LiveView

We added Appsignal.Phoenix.LiveView.instrument/4-5 in version 2.0.0 of the AppSignal for Phoenix integration. In order to instrument a LiveView method, such as mount, the helpers were used like this:

Elixir
def mount(_session, socket) do # Wrap the contents of the mount/2 function with a call to # Appsignal.Phoenix.LiveView.instrument/4 instrument(__MODULE__, "mount", socket, fn -> :timer.send_interval(1000, self(), :tick) {:ok, assign(socket, state: Time.utc_now())} end) end

Note: Upgrading to 2.1 doesn't require any code changes. The new automatic instrumentation setup is disabled by default, you can continue to use the instrument method as you did before.

Automatic LiveView Instrumentation

Since we released 2.0.0, the LiveView library gained instrumentation hooks through Telemetry. Instrumentation hooks allow us to gain performance and error insights without adding custom code to functions (our previous solution).

The new setup is a single call to Appsignal.Phoenix.LiveView.attach/0 to attach the LiveView Telemetry handlers. We recommend calling this function from your app's application.ex:

Elixir
defmodule AppsignalPhoenixExample.Application do # ... def start(_type, _args) do Appsignal.Phoenix.LiveView.attach() # <--- attach the LiveView Telemetry handlers children = [ # Start the Ecto repository AppsignalPhoenixExample.Repo, # Start the Telemetry supervisor AppsignalPhoenixExampleWeb.Telemetry, # Start the PubSub system {Phoenix.PubSub, name: AppsignalPhoenixExample.PubSub}, # Start the Endpoint (http/https) AppsignalPhoenixExampleWeb.Endpoint # Start a worker by calling: AppsignalPhoenixExample.Worker.start_link(arg) # {AppsignalPhoenixExample.Worker, arg} ] # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: AppsignalPhoenixExample.Supervisor] Supervisor.start_link(children, opts) end end

That's it! AppSignal will now automatically instrument all mount, handle_event and handle_param events, meaning you'll receive both performance and error insights into your LiveView.

This is what you could expect to see on the performance issue list in AppSignal:

Liveview List

Here are all the details of the first issue:

Liveview Details

Instrumenting LiveView through Telemetry: A Community Project

This version started as a pull request by Christian Blavier, who turned his custom instrumentation into a patch for us to include in the main AppSignal for Phoenix package. Along the way, and with AJ Foster's insights, we were able to add the new setup to the already existing implementation, allowing for a clean upgrade.

Thanks for your help, Christian and AJ! 🙌

P.S. If you haven't had the chance to try AppSignal out for your Elixir project yet, this is a perfect time to do so. We also ship batches of stroopwafels wordlwide to our new users 🍪

Become our next author!

Find out more

AppSignal monitors your apps

AppSignal provides insights for Ruby, Rails, Elixir, Phoenix, Node.js, Express and many other frameworks and libraries. We are located in beautiful Amsterdam. We love stroopwafels. If you do too, let us know. We might send you some!

Discover AppSignal
AppSignal monitors your apps