https://github.com/recruitee/plug_collect
Instrumentation library to intercept and collect Plug requests.
https://github.com/recruitee/plug_collect
elixir phoenix phoenix-framework plug
Last synced: 8 months ago
JSON representation
Instrumentation library to intercept and collect Plug requests.
- Host: GitHub
- URL: https://github.com/recruitee/plug_collect
- Owner: Recruitee
- License: apache-2.0
- Created: 2022-03-22T14:40:40.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T14:54:54.000Z (over 3 years ago)
- Last Synced: 2025-01-27T07:16:01.765Z (over 1 year ago)
- Topics: elixir, phoenix, phoenix-framework, plug
- Language: Elixir
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 18
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PlugCollect
[](https://hex.pm/packages/plug_collect)
[](https://hexdocs.pm/plug_collect)
[](https://github.com/Recruitee/plug_collect/actions/workflows/ci.yml)
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:
* [Appsignal.Plug](https://github.com/appsignal/appsignal-elixir-plug),
* [Sentry.PlugCapture](https://github.com/getsentry/sentry-elixir).
## Installation
Add `plug_collect` to your application dependencies list in `mix.exs`:
```elixir
#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:
```elixir
#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:
```elixir
defmodule MyApp.Monitor do
def my_collect(_status, %Plug.Conn{assigns: assigns} = _conn),
do: Logger.metadata(user_id: Map.get(assigns, :user_id))
end
```