Logo of AppSignal

Menu
Docs navigation

Integrating AppSignal into Plug

Note: Support for custom namespaces was added in version 1.3.0 of the AppSignal for Elixir package.

The AppSignal for Elixir package integrates with Plug. To set up the integration, please follow our installation guide.

This page describes how to set up AppSignal in a Plug application, and how to add instrumentation for events within requests. For more information about custom instrumentation, read the Elixir instrumentation documentation.

More information can be found in the AppSignal Hex package documentation.

Table of Contents

Incoming HTTP requests

We'll start out with a small Plug app that accepts GET requests to / and returns a welcome message. To start logging HTTP requests in this app, we'll use the AppSignal.Plug module to the end of our app.

1
2
3
4
5
6
7
8
9
10
11
12
defmodule AppsignalPlugExample do
  use Plug.Router

  plug :match
  plug :dispatch

  get "/" do
    send_resp(conn, 200, "Welcome")
  end

  use Appsignal.Plug # <-- Add this
end

This will create a transaction for every HTTP request which is performed on the endpoint.

Custom instrumentation

Although Appsignal.Plug will start transactions for you, it won't instrument events in your app just yet. To do that, we need to add some custom instrumentation.

This example app looks like the one we used before, but it has a slow function (aptly named slow/0) we'd like to add instrumentation for. To do that, we need to

  1. use Appsignal.Instrumentation.Decorators to allow us to use AppSignal's instrumentation decorators
  2. Decorate the function we want to instrument with the transaction_event/0 decorator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
defmodule AppsignalPlugExample do
  use Plug.Router
  use Appsignal.Instrumentation.Decorators # <-- 1

  plug :match
  plug :dispatch

  get "/" do
    slow()
    send_resp(conn, 200, "Welcome")
  end

  @decorate transaction_event() # <-- 2
  defp slow do
    :timer.sleep(1000)
  end

  use Appsignal.Plug
end

This will add an event for the slow/0 function to the current transaction whenever it's called. For more information about custom instrumentation, read the Elixir instrumentation documentation.

Instrumentation for included Plugs

Exceptions in included Plugs are automatically caught by AppSignal, but performance samples need to be set up manually using the custom instrumentation helpers or decorators.

Plug instrumentation with decorators

To add instrumentation to Plugs, use the Appsignal.Instrumentation.Decorators module, and decorate your Plug's call/2 function using the transaction_event/0-1 function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
defmodule SlowPlugWithDecorators do
  import Plug.Conn
  # use Appsignal's decorators
  use Appsignal.Instrumentation.Decorators

  def init(opts), do: opts

  # Decorate the call/2 function to add custom instrumentation
  @decorate transaction_event()
  def call(conn, _) do
    :timer.sleep(1000)
    conn
  end
end

See the transaction_event documentation for more information.

Plug instrumentation without decorators

Instead of using the decorators, you can use the instrument/3 method to decorate a block of code directly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
defmodule SlowPlugWithInstrumentationHelpers do
  import Plug.Conn
  # Import the instrument-function
  import Appsignal.Instrumentation.Helpers, only: [instrument: 3]

  def init(opts), do: opts

  def call(conn, _) do
    # Wrap the contents of the call/2 function in an instrumentation block
    instrument("timer.sleep", "Sleeping", fn() ->
      :timer.sleep(1000)
      conn
    end)
  end
end

See the instrumentation helpers documentation for more information.

We'd like to set cookies, read why.