Skip to content

Commit 1dfc1ad

Browse files
committed
Document how to override path label generation in collector
Signed-off-by: Chris Sinjakli <[email protected]>
1 parent e0dd0f9 commit 1dfc1ad

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

examples/rack/README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,42 @@ example, if you want to [change the way IDs are stripped from the
5454
path](https://github.com/prometheus/client_ruby/blob/982fe2e3c37e2940d281573c7689224152dd791f/lib/prometheus/middleware/collector.rb#L97-L101)
5555
you could override the appropriate method:
5656

57-
```Ruby
57+
```ruby
5858
require 'prometheus/middleware/collector'
59-
module Prometheus
60-
module Middleware
61-
class MyCollector < Collector
62-
def strip_ids_from_path(path)
63-
super(path)
64-
.gsub(/8675309/, ':jenny\\1')
65-
end
66-
end
59+
60+
class MyCollector < Prometheus::Middleware::Collector
61+
def strip_ids_from_path(path)
62+
super(path)
63+
.gsub(/8675309/, ':jenny\\1')
6764
end
6865
end
6966
```
7067

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

70+
If you want to completely customise how the `path` label is generated, you can
71+
override `generate_path`. For example, to use
72+
[Sinatra](https://github.com/sinatra/sinatra)'s framework-specific route info
73+
from the request environment:
74+
75+
```ruby
76+
require 'prometheus/middleware/collector'
77+
78+
class MyCollector < Prometheus::Middleware::Collector
79+
def generate_path(env)
80+
# `sinatra.route` contains both the request method and the route, separated
81+
# by a space (e.g. "GET /payments/:id"). To get just the request path, you
82+
# can partition the string on " ".
83+
env['sinatra.route'].partition(' ').last
84+
end
85+
end
86+
```
87+
88+
Just make sure that your custom path generation logic strips IDs from the path
89+
it returns, or gets the path from a source that would never contain them in the
90+
first place (such as `sinatra.route`), otherwise you'll generate a huge number
91+
of label values!
92+
7393
**Note:** `Prometheus::Middleware::Collector` isn't explicitly designed to be
7494
subclassed, so the internals are liable to change at any time, including in
7595
patch releases. Overriding its methods is done at your own risk!

0 commit comments

Comments
 (0)