A simple Rack application which shows how to use the included
Prometheus::Middleware::Exporter
and Prometheus::Middleware::Collector
middlwares.
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.
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.
The example shown in config.ru
is a trivial rack application
using the default collector and exporter middlewares.
Currently, the collector middleware doesn't offer any flexibility around label keys or values (see #111). If you have more sophisticated requirements, we recommend creating your own collector middleware.
If your requirements are minimal, one option is to subclass
Prometheus::Middleware::Collector
and override the methods you need to. For
example, if you want to change the way IDs are stripped from the
path
you could override the appropriate method:
require 'prometheus/middleware/collector'
class MyCollector < Prometheus::Middleware::Collector
def strip_ids_from_path(path)
super(path)
.gsub(/8675309/, ':jenny\\1')
end
end
and use your class in config.ru
instead.
If you want to completely customise how the path
label is generated, you can
override generate_path
. For example, to use
Sinatra's framework-specific route info
from the request environment:
require 'prometheus/middleware/collector'
class MyCollector < Prometheus::Middleware::Collector
def generate_path(env)
# `sinatra.route` contains both the request method and the route, separated
# by a space (e.g. "GET /payments/:id"). To get just the request path, you
# can partition the string on " ".
env['sinatra.route'].partition(' ').last
end
end
Just make sure that your custom path generation logic strips IDs from the path
it returns, or gets the path from a source that would never contain them in the
first place (such as sinatra.route
), otherwise you'll generate a huge number
of label values!
Note: Prometheus::Middleware::Collector
isn't explicitly designed to be
subclassed, so the internals are liable to change at any time, including in
patch releases. Overriding its methods is done at your own risk!