A configurable Collector middleware for prometheus-client at 42.
If you're using Bundler add this line to your Gemfile
.
gem 'prometheus-client-forty_two'
And then execute:
$ bundle
Or install it yourself as:
$ gem install prometheus-client-forty_two
The Prometheus Ruby Client gem comes with nice standard middlewares to collect simple metrics on routes visited on the app, but it fails in some specific cases:
- Non-numeric, non-uuid identifiers are not cleaned up.
- When static files are served by the server, those queries are collected.
- Some other non-static routes may not be that relevant anyways
This gem offers another Collector that can be customized to:
-
Clean custom identifiers.
use Prometheus::FortyTwo::Collector, specific_id_stripper: lambda { |path| path .gsub(%r{/users/[^/]*}, '/users/:name') .gsub(%r{/[en|es|fr]/}, '/:locale/') } # '/en/users/albert/posts/10/articles' # '/fr/users/julie/posts/223/articles' # '/es/users/zoe/posts/68/articles' # would be stripped as one route: # '/:locale/users/:name/posts/:id/articles'
-
Not collect metrics on static files.
use Prometheus::FortyTwo::Collector, static_files_path: File.join(File.dirname(__FILE__), 'public') # this will recursively list all files in the `/public` directory # and not collect any metrics on those
-
Ignore other custom routes.
use Prometheus::Client::FortyTwo::Middleware::Collector, irrelevant_paths: labmda { |path| path == '/metrics' || path =~ %r{\A/assets/} } # this will ignore /metrics and all routes under /assets # no metric will be collected on those
See Prometheus Client to build your own metrics.
# config.ru
require 'rack'
require 'prometheus/forty_two/collector'
require 'prometheus/middleware/exporter'
use Rack::Deflater
use Prometheus::FortyTwo::Collector,
# TODO: add your configuration for the collector
use Prometheus::Middleware::Exporter
run ->(_) { [200, {'Content-Type' => 'text/html'}, ['OK']] }
This gem has been built for ruby 2.2.3 compatibility. Since this version is now
deprecated, and therefore difficult to install on a recent system, this
repository has been provided a Dockerfile
and a docher-compose.yml
to work
inside a container.
To build the container:
$ docker-compose build gem
To run commands in the container:
$ docker-compose run gem {COMMAND}
To run as session in the container:
$ docker-compose run gem bash
See next section for development commands
After checking out the repo, run bin/setup
to install dependencies. Then, run
rake spec
to run the tests. You can also run bin/console
for an interactive
prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
To release a new version, update the version number in version.rb
, and then
run bundle exec rake release
, which will create a git tag for the version,
push git commits and tags, and push the .gem
file to
rubygems.org.
Bug reports and pull requests are welcome on GitHub at 42School/prometheus-client-forty_two.
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.