-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17842 from jrafanie/detect_long_running_requests
Detect and log long running http(s) requests
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Add to config/application.rb: | ||
# | ||
# config.middleware.use 'RequestStartedOnMiddleware' | ||
# | ||
class RequestStartedOnMiddleware | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
start_request(env['PATH_INFO'], Time.zone.now) | ||
@app.call(env) | ||
ensure | ||
complete_request | ||
end | ||
|
||
def start_request(path, started_on) | ||
Thread.current[:current_request] = path | ||
Thread.current[:current_request_started_on] = started_on | ||
end | ||
|
||
def complete_request | ||
Thread.current[:current_request] = nil | ||
Thread.current[:current_request_started_on] = nil | ||
end | ||
|
||
def self.long_running_requests | ||
requests = [] | ||
allowable_request_start_time = long_request.ago | ||
|
||
relevant_thread_list.each do |thread| | ||
request = thread[:current_request] | ||
started_on = thread[:current_request_started_on] | ||
|
||
# There's a race condition where the complete_request method runs in another | ||
# thread after we set one or more of the above local variables. The fallout | ||
# of this is we return a false positive for a request that finished very close | ||
# to the 2 minute timeout. | ||
if request.present? && started_on.kind_of?(Time) && started_on < allowable_request_start_time | ||
duration = (Time.zone.now - started_on).to_f | ||
requests << [request, duration, thread] | ||
end | ||
end | ||
|
||
requests | ||
end | ||
|
||
LONG_REQUEST = 1.minute | ||
private_class_method def self.long_request | ||
LONG_REQUEST | ||
end | ||
|
||
# For testing: mocking Thread.list feels dangerous | ||
private_class_method def self.relevant_thread_list | ||
Thread.list | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
describe RequestStartedOnMiddleware do | ||
context ".long_running_requests" do | ||
before do | ||
allow(described_class).to receive(:relevant_thread_list) { fake_threads } | ||
allow(described_class).to receive(:request_timeout).and_return(2.minutes) | ||
end | ||
|
||
let(:fake_threads) { [@fake_thread] } | ||
|
||
it "returns request, duration and thread" do | ||
@fake_thread = {:current_request => "/api/ping", :current_request_started_on => 3.minutes.ago} | ||
long_requests = described_class.long_running_requests.first | ||
expect(long_requests[0]).to eql "/api/ping" | ||
expect(long_requests[1]).to be_within(0.1).of(Time.now.utc - 3.minutes.ago) | ||
expect(long_requests[2]).to eql @fake_thread | ||
end | ||
|
||
it "skips threads that haven't timed out yet" do | ||
@fake_thread = {:current_request => "/api/ping", :current_request_started_on => 30.seconds.ago} | ||
expect(described_class.long_running_requests).to be_empty | ||
end | ||
|
||
it "skips threads with no requests" do | ||
@fake_thread = {} | ||
expect(described_class.long_running_requests).to be_empty | ||
end | ||
end | ||
end |