Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions examples/rack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,20 @@ Prometheus server can be used to [play around with the metrics][rate-query].
The example shown in [`config.ru`](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:

```ruby
use Prometheus::Middleware::Collector, counter_label_builder: ->(env, code) {
next { code: nil, method: nil, host: nil, path: nil } if env.empty?

{
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'),
}
}
Modifying the labels is a subject under development (see #111) but one option is to subclass `Prometheus::Middleware::Collector` and override the methods you need. For example, if you want to [strip IDs from the path](https://github.com/prometheus/client_ruby/blob/982fe2e3c37e2940d281573c7689224152dd791f/lib/prometheus/middleware/collector.rb#L97-L101) you could override the appropriate method:

```Ruby
require 'prometheus/middleware/collector'
module Prometheus
module Middleware
class MyCollector < Collector
def strip_ids_from_path(path)
super(path)
.gsub(/8675309/, ':jenny\\1')
end
end
end
end
```

and in `config.ru` use your class instead.