Skip to content

Commit 0761f59

Browse files
dmagliolaiankswoto
committed
Include SCRIPT_NAME when determining path in Collector
When determining the path for a request, `Rack::Request` prefixes the `SCRIPT_NAME`, [as seen here][1]. This is a problem with our current code when using mountable engines, where the engine part of the path gets lost. This patch fixes that to include `SCRIPT_NAME` as part of the path. NOTE: This is not backwards compatible. Labels will change in existing metrics. We will cut a new major version once we ship this. [1]: https://github.com/rack/rack/blob/294fd239a71aab805877790f0a92ee3c72e67d79/lib/rack/request.rb#L512 Co-authored-by: Ian Ker-Seymer <[email protected]> Co-authored-by: Ruslan Kornev <[email protected]> Signed-off-by: Daniel Magliola <[email protected]>
1 parent 726536c commit 0761f59

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/prometheus/middleware/collector.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ def trace(env)
6767
end
6868

6969
def record(env, code, duration)
70+
path = [env["SCRIPT_NAME"], env['PATH_INFO']].join
71+
7072
counter_labels = {
7173
code: code,
7274
method: env['REQUEST_METHOD'].downcase,
73-
path: strip_ids_from_path(env['PATH_INFO']),
75+
path: strip_ids_from_path(path),
7476
}
7577

7678
duration_labels = {
7779
method: env['REQUEST_METHOD'].downcase,
78-
path: strip_ids_from_path(env['PATH_INFO']),
80+
path: strip_ids_from_path(path),
7981
}
8082

8183
@requests.increment(labels: counter_labels)

spec/prometheus/middleware/collector_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@
5555
expect(registry.get(metric).get(labels: labels)).to include("0.1" => 0, "0.25" => 1)
5656
end
5757

58+
it 'includes SCRIPT_NAME in the path if provided' do
59+
metric = :http_server_requests_total
60+
61+
get '/foo'
62+
expect(registry.get(metric).values.keys.last[:path]).to eql("/foo")
63+
64+
env('SCRIPT_NAME', '/engine')
65+
get '/foo'
66+
env('SCRIPT_NAME', nil)
67+
expect(registry.get(metric).values.keys.last[:path]).to eql("/engine/foo")
68+
end
69+
5870
it 'normalizes paths containing numeric IDs by default' do
5971
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.3)
6072

0 commit comments

Comments
 (0)