@@ -54,22 +54,42 @@ example, if you want to [change the way IDs are stripped from the
5454path] ( https://github.com/prometheus/client_ruby/blob/982fe2e3c37e2940d281573c7689224152dd791f/lib/prometheus/middleware/collector.rb#L97-L101 )
5555you could override the appropriate method:
5656
57- ``` Ruby
57+ ``` ruby
5858require ' 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
6865end
6966```
7067
7168and 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
7494subclassed, so the internals are liable to change at any time, including in
7595patch releases. Overriding its methods is done at your own risk!
0 commit comments