Skip to content

Commit

Permalink
Merge pull request #126 from swalberg/patch-1
Browse files Browse the repository at this point in the history
Update example README for #111
  • Loading branch information
Chris Sinjakli authored May 31, 2019
2 parents 982fe2e + 661fa88 commit 710579c
Showing 1 changed file with 16 additions and 17 deletions.
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.

0 comments on commit 710579c

Please sign in to comment.