Skip to content

Commit 9cbcf34

Browse files
committed
Add request env to tracing ActionDispatch contrib
ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin23] Warming up -------------------------------------- w: gsub 72.280k i/100ms w: sub 81.689k i/100ms w: delete_suffix 502.700k i/100ms w/o: gsub 116.580k i/100ms w/o: sub 195.557k i/100ms w/o: delete_suffix 225.558k i/100ms Calculating ------------------------------------- w: gsub 1.609M (±68.8%) i/s (621.46 ns/i) - 10.914M in 30.723566s w: sub 3.185M (±65.6%) i/s (313.95 ns/i) - 17.481M in 30.198337s w: delete_suffix 4.903M (±118.8%) i/s (203.97 ns/i) - 45.746M in 31.428342s w/o: gsub 4.935M (±64.8%) i/s (202.61 ns/i) - 46.632M in 30.271821s w/o: sub 4.876M (±63.5%) i/s (205.09 ns/i) - 46.934M in 30.233153s w/o: delete_suffix 10.564M (±78.8%) i/s (94.66 ns/i) - 48.495M in 30.639072s
1 parent f7e85f9 commit 9cbcf34

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/datadog/tracing/contrib/action_pack/action_dispatch/instrumentation.rb

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@ module ActionPack
99
module ActionDispatch
1010
# Instrumentation for ActionDispatch components
1111
module Instrumentation
12+
HTTP_ROUTE_KEY = 'datadog.http.route'
13+
SCRIPT_NAME_KEY = 'SCRIPT_NAME'
14+
FORMAT_SUFFIX = '(.:format)'
15+
1216
module_function
1317

14-
def set_http_route_tags(route_spec, script_name)
18+
def set_http_route_tags(route_spec, route_path)
1519
return unless Tracing.enabled?
1620

1721
return unless route_spec
1822

1923
request_trace = Tracing.active_trace
2024
return unless request_trace
2125

22-
request_trace.set_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE, route_spec.to_s.gsub(/\(.:format\)\z/, ''))
26+
request_trace.set_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE, route_spec)
2327

24-
if script_name && !script_name.empty?
25-
request_trace.set_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE_PATH, script_name)
28+
if route_path && !route_path.empty?
29+
request_trace.set_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE_PATH, route_path)
2630
end
2731
end
2832

@@ -40,16 +44,18 @@ module Journey
4044
# Instrumentation for ActionDispatch::Journey::Router for Rails versions older than 7.1
4145
module Router
4246
def find_routes(req)
47+
# result is an array of [match, parameters, route] tuples
4348
result = super
49+
result.each do |_, _, route|
50+
next unless Instrumentation.dispatcher_route?(route)
4451

45-
# result is an array of [match, parameters, route] tuples
46-
routes = result.map(&:last)
52+
http_route = route.path.spec.to_s
53+
http_route.delete_suffix!(FORMAT_SUFFIX)
4754

48-
routes.each do |route|
49-
if Instrumentation.dispatcher_route?(route)
50-
Instrumentation.set_http_route_tags(route.path.spec, req.env['SCRIPT_NAME'])
51-
break
52-
end
55+
req.env[HTTP_ROUTE_KEY] = http_route
56+
Instrumentation.set_http_route_tags(http_route, req.env[SCRIPT_NAME_KEY])
57+
58+
break
5359
end
5460

5561
result
@@ -62,7 +68,11 @@ module LazyRouter
6268
def find_routes(req)
6369
super do |match, parameters, route|
6470
if Instrumentation.dispatcher_route?(route)
65-
Instrumentation.set_http_route_tags(route.path.spec, req.env['SCRIPT_NAME'])
71+
http_route = route.path.spec.to_s
72+
http_route.delete_suffix!(FORMAT_SUFFIX)
73+
74+
req.env[HTTP_ROUTE_KEY] = http_route
75+
Instrumentation.set_http_route_tags(http_route, req.env[SCRIPT_NAME_KEY])
6676
end
6777

6878
yield [match, parameters, route]

0 commit comments

Comments
 (0)