prometheus_plugs v1.0.0-alpha8 Prometheus.PlugInstrumenter

Helps you create a plug that instruments another plug.

Usage

  1. Define your instrumenter:
defmodule MyApp.Guardian.Plug.EnsureAuthenticatedInstrumenter do
  use Prometheus.PlugInstrumenter, [plug: Guardian.Plug.EnsureAuthenticated,
                                    counter: :guardian_ensure_authenticated_total,
                                    histogram: :guardian_ensure_authenticated_duration_microseconds,
                                    labels: [:authenticated]]

  def label_value(:authenticated, {conn, _}) do
    conn.status != 401
  end
end

2. Call `MyApp.Guardian.Plug.EnsureAuthenticatedInstrumenter.setup/0` when application starts
(e.g. supervisor setup):

MyApp.Guardian.Plug.EnsureAuthenticatedInstrumenter.setup()


3. Add `MyApp.Guardian.Plug.EnsureAuthenticatedInstrumenter` to your plug pipeline or replace
`Guardian.Plug.EnsureAuthenticated` if it already present.

```elixir:
plug MyApp.Guardian.Plug.EnsureAuthenticatedInstrumenter, handler: Guardian.Plug.ErrorHandler

As you can see you can transparently pass options to the underlying plug.

Metrics

Currently PlugInstrumenter supports two metrics - counter for total calls count and histogram for calls duration. Metric can be disabled by setting it’s name to false:

counter: false

There should be at least one active metric.

Configuration

Mandatory keys:

  • plug - Plug to instrument;
  • counter or histogram.

Optional keys with defaults:

  • registry - Prometheus registry for metrics (:default);
  • counter - counter metric name (false);
  • histogram - histogram metric name (false);
  • histogram_buckets - histogram metric buckets (Prometheus.Contrib.HTTP.microseconds_duration_buckets());
  • labels - labels for counter and histogram ([]);
  • duration_units - duration units for the histogram, if histogram name already has known duration unit this can be omitted (:undefined).

As noted above at least one metric name should be supplied. Of course you must tell what plug to instrument through :plug key.

We don’t have default labels because they are highly specific to a particular plug hence default is [].

It’s possible to customize histogram buckets via :histogram_buckets key. Bear in mind that bounds are microseconds (1s is 1_000_000us)