prometheus_plugs v0.9.0 Prometheus.PlugsInstrumenter

Plug for collecting http metrics.

To use it, plug it into the desired module. You also want to call setup/0 before using plug, for example on application start!

plug Prometheus.PlugsInstrumenter

Currently maintains two metrics.

  • http_requests_total - Total nubmer of HTTP requests made. This one is a counter.
  • http_request_duration_microseconds - The HTTP request latencies in microseconds. This one is a histogram.
# on app startup (e.g. supervisor setup)
Prometheus.PlugsInstrumenter.setup()

# in your plugs pipeline
plug Prometheus.PlugsInstrumenter

Configuration

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

All metrics support configurable labels:

 - status_code - http code
 - status_class - http code class, like "success", "redirect", "client-error", etc
 - method - http method
 - host - requested host
 - port - requested port
 - scheme - request scheme (like http or https)

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)

Summary

Functions

Callback implementation for c:Plug.call/2

Callback implementation for c:Plug.init/1

Functions

call(conn, labels)

Callback implementation for c:Plug.call/2.

init(labels)

Callback implementation for c:Plug.init/1.

setup(opts \\ [])