@@ -72,12 +72,12 @@ def record(env, code, duration)
7272 counter_labels = {
7373 code : code ,
7474 method : env [ 'REQUEST_METHOD' ] . downcase ,
75- path : strip_ids_from_path ( path ) ,
75+ path : path ,
7676 }
7777
7878 duration_labels = {
7979 method : env [ 'REQUEST_METHOD' ] . downcase ,
80- path : strip_ids_from_path ( path ) ,
80+ path : path ,
8181 }
8282
8383 @requests . increment ( labels : counter_labels )
@@ -87,52 +87,10 @@ def record(env, code, duration)
8787 nil
8888 end
8989
90- # While `PATH_INFO` is framework agnostic, and works for any Rack app, some Ruby web
91- # frameworks pass a more useful piece of information into the request env - the
92- # route that the request matched.
93- #
94- # This means that rather than using our generic `:id` and `:uuid` replacements in
95- # the `path` label for any path segments that look like dynamic IDs, we can put the
96- # actual route that matched in there, with correctly named parameters. For example,
97- # if a Sinatra app defined a route like:
98- #
99- # get "/foo/:bar" do
100- # ...
101- # end
102- #
103- # instead of containing `/foo/:id`, the `path` label would contain `/foo/:bar`.
104- #
105- # Sadly, Rails is a notable exception, and (as far as I can tell at the time of
106- # writing) doesn't provide this info in the request env.
10790 def generate_path ( env )
108- if env [ 'sinatra.route' ]
109- route = env [ 'sinatra.route' ] . partition ( ' ' ) . last
110- elsif env [ 'grape.routing_args' ]
111- # We are deep in the weeds of an object that Grape passes into the request env,
112- # but don't document any explicit guarantees about. Let's have a fallback in
113- # case they change it down the line.
114- #
115- # This code would be neater with the safe navigation operator (`&.`) here rather
116- # than the much more verbose `respond_to?` calls, but unlike Rails' `try`
117- # method, it still raises an error if the object is non-nil, but doesn't respond
118- # to the method being called on it.
119- route = nil
120-
121- route_info = env . dig ( 'grape.routing_args' , :route_info )
122- if route_info . respond_to? ( :pattern )
123- pattern = route_info . pattern
124- if pattern . respond_to? ( :origin )
125- route = pattern . origin
126- end
127- end
128-
129- # Fall back to PATH_INFO if Grape change the structure of `grape.routing_args`
130- route ||= env [ 'PATH_INFO' ]
131- else
132- route = env [ 'PATH_INFO' ]
133- end
134-
135- [ env [ 'SCRIPT_NAME' ] , route ] . join
91+ full_path = [ env [ 'SCRIPT_NAME' ] , env [ 'PATH_INFO' ] ] . join
92+
93+ strip_ids_from_path ( full_path )
13694 end
13795
13896 def strip_ids_from_path ( path )
0 commit comments