Skip to content

Instrumentation library to intercept and collect Plug requests.

License

Notifications You must be signed in to change notification settings

Recruitee/plug_collect

Repository files navigation

PlugCollect

Hex Version Hex Docs CI

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

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

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

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