> ## Documentation Index
> Fetch the complete documentation index at: https://docs.appsignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AppSignal integreren in Phoenix

Het AppSignal voor Elixir-pakket integreert met Phoenix. Om de integratie op
te zetten, volg onze [installatiehandleiding](/elixir/installation).

Deze pagina beschrijft verdere manieren om AppSignal te configureren voor het [Phoenix-
framework][phoenix]. Lees de [Elixir-
instrumentatie](/elixir/instrumentation)-documentatie om meer aangepaste
instrumentatie toe te voegen aan uw Phoenix-applicatie.

Meer informatie vindt u in de [AppSignal Hex-pakketdocumentatie][hex-appsignal].

## Installatie

1. De AppSignal-instrumentatie voor Phoenix is onderdeel van een
   apart pakket, dat afhankelijk is van het primaire `appsignal`-pakket. Voeg het pakket `appsignal_phoenix` toe aan uw `mix.exs`-bestand.

<CodeGroup>
  ```elixir Elixir theme={null}
  # mix.exs
  defmodule AppsignalPhoenixExample.MixProject do
    # ...

    defp deps do
      [
        {:phoenix, "~> 1.5.3"},
        {:appsignal_phoenix, "~> 2.0"}
        # ...
      ]
    end
  end
  ```
</CodeGroup>

2. Voer vervolgens `mix deps.get` uit.
3. Voer vervolgens `mix appsignal.install YOUR_PUSH_API_KEY` uit.

Na installeren en configureren begint het AppSignal voor Phoenix-pakket automatisch met het instrumenteren van HTTP-requests, wat betekent dat er geen verdere setup nodig is.

Om diepere inzichten te krijgen, kan AppSignal worden ingesteld om channels en live views te instrumenteren.

## Channels

### Channel-instrumentatie met een channel's handle

Inkomende channel-requests kunnen worden geïnstrumenteerd door de code in uw
`handle_in/3`-functies te wikkelen met `Appsignal.Phoenix.Channel.instrument/5`:

<CodeGroup>
  ```elixir Elixir theme={null}
  defmodule AppsignalPhoenixExampleWeb.RoomChannel do
    use Phoenix.Channel

    # ...

    def handle_in("new_msg", %{"body" => body} = params, socket) do
      Appsignal.Phoenix.Channel.instrument(__MODULE__, "new_msg", params, socket, fn ->
        broadcast!(socket, "new_msg", %{body: body})
        {:noreply, socket}
      end)
    end
  end
  ```
</CodeGroup>

Alternatief kunt u function decorators gebruiken om channels te instrumenteren. Hoewel
minder flexibel dan de instrumentatiefunctie, minimaliseren decorators de hoeveelheid
code die u aan de channels van uw applicatie hoeft toe te voegen.

<CodeGroup>
  ```elixir Elixir theme={null}
  defmodule SomeApp.MyChannel do
    use Appsignal.Instrumentation.Decorators

    @decorate channel_action()
    def handle_in("ping", _payload, socket) do
      # your code here..
    end
  end
  ```
</CodeGroup>

Channel-events worden weergegeven onder het tabblad "Background jobs" en tonen de
channel-module en het action-argument dat u heeft ingevoerd.

## LiveView

Er zijn twee verschillende manieren om live views te instrumenteren. U kunt onze
automatische telemetry-handlers gebruiken, of u kunt uw live view-handlers
handmatig instrumenteren met onze hulpfuncties.

### Automatische telemetry-handlers

<Tip>
  **Opmerking**: De LiveView Telemetry-integratie is beschikbaar vanaf AppSignal voor
  Phoenix-versie `2.1.0`.
</Tip>

AppSignal's LiveView Telemetry-integratie instrumenteert live view-events automatisch.

Om dit op te zetten, roept u `Appsignal.Phoenix.LiveView.attach/0` aan om de LiveView Telemetry-handlers te koppelen.
We raden aan om deze functie aan te roepen vanuit `application.ex` van uw app:

<CodeGroup>
  ```elixir Elixir theme={null}
  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
  ```
</CodeGroup>

AppSignal instrumenteert nu automatisch alle `mount`-, `handle_event`- en `handle_param`-events onder de namespace `live_view`, wat betekent dat u zowel performance- als foutinzichten in uw live views ontvangt.

### Handmatige hulpfuncties

<Tip>
  Als u de automatische telemetry-instrumentatie hierboven al heeft geconfigureerd,
  hoeft u uw live view-handlers niet handmatig te instrumenteren.
</Tip>

<Tip>
  **Opmerking**: De LiveView-hulpfuncties zijn beschikbaar vanaf AppSignal voor
  Elixir-versie `1.13.0`.
</Tip>

Een LiveView-action kan ook handmatig worden geïnstrumenteerd door de inhoud ervan in een
`Appsignal.Phoenix.LiveView.instrument/4`-blok te wikkelen.

Gegeven een live view die zijn eigen status elke seconde bijwerkt, kunnen we
AppSignal-instrumentatie toevoegen door zowel de mount/2- als de handle\_info/2-
functies te wikkelen met een aanroep naar `Appsignal.Phoenix.LiveView.instrument`/4:

<CodeGroup>
  ```elixir Elixir theme={null}
  defmodule AppsignalPhoenixExampleWeb.ClockLive do
    use Phoenix.LiveView
    import Appsignal.Phoenix.LiveView, only: [instrument: 4]

    def render(assigns) do
      AppsignalPhoenixExampleWeb.ClockView.render("index.html", assigns)
    end

    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

    def handle_info(:tick, socket) do
      # Wrap the contents of the handle_info/2 function with a call to
      # Appsignal.Phoenix.LiveView.instrument/4:

      instrument(__MODULE__, "tick", socket, fn ->
        {:ok, assign(socket, state: Time.utc_now())}
      end)
    end
  end
  ```
</CodeGroup>

Het aanroepen van een van deze functies in uw app maakt nu automatisch een
sample aan die naar AppSignal wordt gestuurd. Deze worden weergegeven onder de namespace
`:live_view`.

## Instrumentatie voor opgenomen Plugs

Uitzonderingen in opgenomen Plugs worden automatisch opgevangen door AppSignal, maar
performance samples moeten handmatig worden opgezet met behulp van de aangepaste instrumentatie-
helpers of decorators. Zie de
[Plug](/elixir/integrations/plug#instrumentation-for-included-plugs)-
documentatie voor meer informatie.

## Aangepaste instrumentatie

[Voeg aangepaste instrumentatie toe](/elixir/instrumentation/instrumentation) om
complexere code bij te houden en completere uitsplitsingen te krijgen van trage
requests.

[phoenix]: http://www.phoenixframework.org/

[hex-appsignal]: https://hexdocs.pm/appsignal/
