Prometheus Plugs Hex.pm

Elixir plugs for prometheus.erl

TL;DRExample app

Plugs

Prometheus Plugs currently comes with two Plugs. One is for collecting http metrics while another provides endpoint for scraping by Prometheus daemon.

Prometheus.PlugsInstrumenter

Currently maintains two metrics.

Setup

# on app startup (e.g. supervisor setup)
Prometheus.PlugsInstrumenter.setup()

# in your plugs pipeline
plug Prometheus.PlugsInstrumenter

Plugs instrumenter can be configured via PlugsInstrumenter key of prometheus app env.

All metrics support configurable labels:

Default configuration:

config :prometheus, PlugsInstrumenter,
  labels: [:status_class, :method, :host, :scheme],
  duration_buckets:[10, 100, 1_000, 10_000, 100_000,
                    300_000, 500_000, 750_000, 1_000_000,
                    1_500_000, 2_000_000, 3_000_000],
  registry: :default

In fact almost any Plug.Conn field value can be used as metric label. In order to create a custom label simply provide a fun as either a key-value pair where the value is a fun which will be given the label and conn as parameters:

defmodule CustomLabels do
  def label_value(key, conn) do
    Map.get(conn.private, key, "unknown") |> to_string
  end

  def phoenix_controller_action(%Plug.Conn{private: private}) do
    case [private[:phoenix_controller], private[:phoenix_action]] do
      [nil, nil] -> "unknown"
      [controller, action] -> "#{controller}/#{action}"
    end
  end
end

labels: [:status_class, phoenix_controller: CustomLabels, phoenix_controller_action: {CustomLabels, :phoenix_controller_action}]

Bear in mind that bounds are microseconds (1s is 1_000_000us)

Plug.PrometheusExporter

Exports metrics in text format via configurable endpoint:

# on app startup (e.g. supervisor setup)
Plug.PrometheusExporter.setup()

# in your plugs pipeline
plug Plug.PrometheusExporter

Defautl Configuration:

config :prometheus, PlugsExporter,
  path: "/metrics",
  format: :text,
  registry: :default

Installation

The package can be installed as:

  1. Add prometheus_plug to your list of dependencies in mix.exs:

    def deps do

     [{:prometheus_plugs, "~> 0.7"}]

    end

  2. Ensure prometheus is started before your application:

    def application do

     [applications: [:prometheus]]

    end

License

MIT