Skip to content

Commit 4cdd0ab

Browse files
author
Daniel Magliola
authored
Merge pull request #206 from prometheus/add_script_name_in_collector_path
Include SCRIPT_NAME when determining path in Collector
2 parents 726536c + 0761f59 commit 4cdd0ab

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)