6

AppSignal for Phoenix 2.1: Automatic LiveView Instrumentation

 2 years ago
source link: https://blog.appsignal.com/2022/06/20/appsignal-for-phoenix-2-1-automatic-liveview-instrumentation.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

appsignal

AppSignal for Phoenix 2.1: Automatic LiveView Instrumentation

Jeff Kreeftmeijer

Jeff Kreeftmeijer on Jun 20, 2022

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:

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:

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:

Article Illustration

Here are all the details of the first issue:

Article Illustration

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 🍪


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK