Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 1.7 KB

README.md

File metadata and controls

62 lines (45 loc) · 1.7 KB

Rack example

A simple Rack application which shows how to use the included Prometheus::Middleware::Exporter and Prometheus::Middleware::Collector middlwares.

Run the example

Standalone

Execute the provided run script:

bundle install
bundle exec ./run

This will start the rack app, run a few requests against it, print the output of /metrics and terminate.

With a Prometheus server

Start a Prometheus server with the provided config:

prometheus -config.file ./prometheus.yml

In another terminal, start the application server:

bundle install
bundle exec unicorn -c ./unicorn.conf

You can now open the example app and its metrics page to inspect the output. The running Prometheus server can be used to play around with the metrics.

Collector

The example shown in config.ru is a trivial rack application using the default collector and exporter middlewares.

In order to use custom label builders in the collector, change the line to something like this:

use Prometheus::Middleware::Collector, counter_label_builder: ->(env, code) {
  {
    code:         code,
    method:       env['REQUEST_METHOD'].downcase,
    # Include the HTTP Host header as label.
    host:         env['HTTP_HOST'].to_s,
    # Include path, but replace all numeric IDs to keep cardinality low.
    # Think '/users/1234/comments' -> '/users/:id/comments'
    path:         env['PATH_INFO'].to_s.gsub(/\/\d+(\/|$)/, '/:id\\1'),
  }
}