prometheus_plugs v1.1.5 Prometheus.PlugPipelineInstrumenter View Source

Generates a plug for collecting http metrics. Instruments whole pipeline.

First lets define plug for your instrumenter:

defmodule PlugPipelineInstrumenter do
  use Prometheus.PlugPipelineInstrumenter
end

Then add call setup/0 before using plug, for example on application start!

# on app startup (e.g. supervisor setup)
PlugPipelineInstrumenter.setup()

And finally add plug to pipeline:

# in your plug pipeline
plug PlugPipelineInstrumenter

Metrics

Currently maintains two metrics.

  • http_requests_total - Total nubmer of HTTP requests made. This one is a counter.
  • http_request_duration_<duration_unit> - The HTTP request latencies in . This one is a histogram.

Configuration

Plug pipeline instrumenter can be configured via PlugPipelineInstrumenter (you should replace this with the name of your plug) 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, PlugPipelineInstrumenter,
  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,
  duration_unit: :microseconds

Available duration units:

  • microseconds;
  • milliseconds;
  • seconds;
  • minutes;
  • hours;
  • days.

In fact almost any Plug.Conn field value can be used as metric label. Label value can be generated using custom function. In order to create a custom label simply provide a fun reference as a key-value pair where key is a label name and value is either module name which exports label_value/2 function or {module, fun/2} tuple. By default your plug’s label_value is called when label is unknown.

defmodule PlugPipelineInstrumenter do
  use Prometheus.PlugPipelineInstrumenter

  def label_value(:request_path, conn) do
    conn.request_path
  end
end

labels: [:status_class, :request_path]

Bear in mind that buckets are so if you are not using default unit you also have to override buckets.