Skip to content

Commit 34ed4ab

Browse files
committed
Cover Faraday and chained HTTP.rb requests
Add Faraday GET/POST endpoints to rails7-sidekiq so the new automated Faraday integration is exercised (and its downstream Net::HTTP event suppressed). Add chained HTTP.rb requests -- a headers chain and a followed redirect -- which run through HTTP::Session on http 6 and were previously uninstrumented. Targets: appsignal/appsignal-ruby#1523 (Faraday) appsignal/appsignal-ruby#1528 (HTTP.rb chained)
1 parent 5286243 commit 34ed4ab

5 files changed

Lines changed: 95 additions & 2 deletions

File tree

ruby/rails7-sidekiq/app/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem "sidekiq", "~> 7.1.6"
1212
gem "appsignal", :path => "/integration"
1313
gem "excon"
1414
gem "http"
15+
gem "faraday"
1516

1617
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
1718
gem "sprockets-rails"

ruby/rails7-sidekiq/app/Gemfile.lock

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
PATH
22
remote: /integration
33
specs:
4-
appsignal (4.0.0)
5-
rack
4+
appsignal (4.8.6)
5+
logger
6+
rack (>= 2.0.0)
67

78
GEM
89
remote: https://rubygems.org/
@@ -105,11 +106,30 @@ GEM
105106
debug (1.8.0)
106107
irb (>= 1.5.0)
107108
reline (>= 0.3.1)
109+
domain_name (0.6.20240107)
108110
drb (2.2.1)
109111
erubi (1.12.0)
110112
excon (0.110.0)
113+
faraday (2.14.3)
114+
faraday-net_http (>= 2.0, < 3.5)
115+
json
116+
logger
117+
faraday-net_http (3.4.4)
118+
net-http (~> 0.5)
119+
ffi (1.17.4)
120+
ffi-compiler (1.4.2)
121+
ffi (>= 1.15.5)
122+
rake
111123
globalid (1.2.1)
112124
activesupport (>= 6.1)
125+
http (5.3.1)
126+
addressable (~> 2.8)
127+
http-cookie (~> 1.0)
128+
http-form_data (~> 2.2)
129+
llhttp-ffi (~> 0.5.0)
130+
http-cookie (1.1.6)
131+
domain_name (~> 0.5)
132+
http-form_data (2.3.0)
113133
i18n (1.14.5)
114134
concurrent-ruby (~> 1.0)
115135
importmap-rails (1.2.1)
@@ -122,6 +142,11 @@ GEM
122142
jbuilder (2.11.5)
123143
actionview (>= 5.0.0)
124144
activesupport (>= 5.0.0)
145+
json (2.20.0)
146+
llhttp-ffi (0.5.1)
147+
ffi-compiler (~> 1.0)
148+
rake (~> 13.0)
149+
logger (1.7.0)
125150
loofah (2.22.0)
126151
crass (~> 1.0.2)
127152
nokogiri (>= 1.12.0)
@@ -139,6 +164,8 @@ GEM
139164
mustermann (3.0.0)
140165
ruby2_keywords (~> 0.0.1)
141166
mutex_m (0.2.0)
167+
net-http (0.9.1)
168+
uri (>= 0.11.1)
142169
net-imap (0.4.11)
143170
date
144171
net-protocol
@@ -250,6 +277,7 @@ GEM
250277
railties (>= 6.0.0)
251278
tzinfo (2.0.6)
252279
concurrent-ruby (~> 1.0)
280+
uri (1.1.1)
253281
web-console (4.2.1)
254282
actionview (>= 6.0.0)
255283
activemodel (>= 6.0.0)
@@ -277,6 +305,8 @@ DEPENDENCIES
277305
capybara
278306
debug
279307
excon
308+
faraday
309+
http
280310
importmap-rails
281311
jbuilder
282312
pg (~> 1.1)

ruby/rails7-sidekiq/app/app/controllers/requests_controller.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,56 @@ def perform_http_rb_get
4646
redirect_to requests_path, :notice => %(Request "#{params[:action]}" made)
4747
end
4848

49+
# In http 6 a chained request (`HTTP.follow.get`, `HTTP.headers(...).get`,
50+
# etc.) runs through `HTTP::Session#request` instead of `HTTP::Client#request`.
51+
# These actions exercise that path so it records a `request.http_rb` event.
52+
def perform_http_rb_headers
53+
HTTP.headers("X-Custom-Header" => "AppSignal")
54+
.get("http://localhost:4001/requests/http_rb_target")
55+
redirect_to requests_path, :notice => %(Request "#{params[:action]}" made)
56+
end
57+
58+
# A followed redirect should stay a single `request.http_rb` event spanning
59+
# every hop, not one event per hop.
60+
def perform_http_rb_follow
61+
HTTP.follow.get("http://localhost:4001/requests/http_rb_redirect")
62+
redirect_to requests_path, :notice => %(Request "#{params[:action]}" made)
63+
end
64+
65+
def http_rb_target
66+
sleep 1
67+
render :html => "HTTP.rb request target"
68+
end
69+
70+
def http_rb_redirect
71+
redirect_to http_rb_target_requests_path
72+
end
73+
74+
# Faraday defaults to the Net::HTTP adapter, so this also exercises AppSignal
75+
# suppressing the downstream Net::HTTP event: the request should be recorded
76+
# once, as a `request.faraday` event.
77+
def perform_faraday_get
78+
handle_response Faraday.get("http://localhost:4001/requests/faraday_get?query=param")
79+
end
80+
81+
def perform_faraday_post
82+
handle_response Faraday.post(
83+
"http://localhost:4001/requests/faraday_post",
84+
URI.encode_www_form(:language => "ruby", :library => "faraday"),
85+
"Content-Type" => "application/x-www-form-urlencoded"
86+
)
87+
end
88+
89+
def faraday_get
90+
sleep 1
91+
render :html => "Faraday GET request"
92+
end
93+
94+
def faraday_post
95+
sleep 1
96+
render :html => "Faraday POST request"
97+
end
98+
4999
def handle_response(response)
50100
unless response.status == 200
51101
raise "Request failed with status #{response.status}"

ruby/rails7-sidekiq/app/app/views/requests/index.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
<li><%= link_to "Excon HTTP OPTIONS", perform_excon_options_requests_path %></li>
1212
<li><%= link_to "Excon HTTP TRACE", perform_excon_trace_requests_path %></li>
1313
<li><%= link_to "HTTP.rb GET", perform_http_rb_get_requests_path %></li>
14+
<li><%= link_to "HTTP.rb GET (chained headers)", perform_http_rb_headers_requests_path %></li>
15+
<li><%= link_to "HTTP.rb GET (followed redirect)", perform_http_rb_follow_requests_path %></li>
16+
<li><%= link_to "Faraday HTTP GET", perform_faraday_get_requests_path %></li>
17+
<li><%= link_to "Faraday HTTP POST", perform_faraday_post_requests_path %></li>
1418
</ul>

ruby/rails7-sidekiq/app/config/routes.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ class SinatraApp < Sinatra::Base
4949
get :perform_excon_trace
5050
match "/excon_trace" => "requests#excon_trace", :via => :trace
5151
get :perform_http_rb_get
52+
get :perform_http_rb_headers
53+
get :perform_http_rb_follow
54+
get :http_rb_target
55+
get :http_rb_redirect
56+
get :perform_faraday_get
57+
get :faraday_get
58+
get :perform_faraday_post
59+
post :faraday_post
5260
end
5361
end
5462

0 commit comments

Comments
 (0)