View Source PlugCollect

Hex Version Hex Docs

Basic instrumentation library to intercept and collect Plug pipeline connection parameters for further reporting, monitoring or analysis with user provided function.

PlugCollect is inspired by other libraries:

installation

Installation

Add plug_collect to your application dependencies list in mix.exs:

#mix.exs
def deps do
  [
    {:plug_collect, "~> 0.2.0"}
  ]
end

usage

Usage

Add use PlugCollect to your application's Phoenix endpoint or router, for example:

#endpoint.ex
defmodule MyAppWeb.Endpoint do
  use PlugCollect, collectors: [&MyApp.Monitor.my_collect/2]
  use Phoenix.Endpoint, otp_app: :my_app
  # ...

Callback function MyApp.Monitor.my_collect/2 will be invoked on each request. Example my_collect/2 callback implementation:

defmodule MyApp.Monitor do
  def my_collect(_status, %Plug.Conn{assigns: assigns} = _conn),
    do: Logger.metadata(user_id: Map.get(assigns, :user_id))
end