Skip to content

Commit

Permalink
Add time tracking to http status agent (#1517)
Browse files Browse the repository at this point in the history
* Add time tracking to http status agent

* Replace time tracking to monotonic

* Add tests for time tracker
  • Loading branch information
pcarranza authored and cantino committed May 29, 2016
1 parent c15a569 commit 74a3c88
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
13 changes: 7 additions & 6 deletions app/models/agents/http_status_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HttpStatusAgent < Agent
form_configurable :disable_redirect_follow, type: :array, values: ['true', 'false']

description <<-MD
The HttpStatusAgent will check a url and emit the resulting HTTP status code.
The HttpStatusAgent will check a url and emit the resulting HTTP status code with the time that it waited for a reply.
Specify a `Url` and the Http Status Agent will produce an event with the http status code.
Expand All @@ -27,6 +27,7 @@ class HttpStatusAgent < Agent
{
"url": "...",
"status": "..."
"elapsed_time": "..."
}
MD

Expand Down Expand Up @@ -60,11 +61,12 @@ def receive(incoming_events)
private

def check_this_url(url)
if result = ping(url)
create_event payload: { 'url' => url, 'status' => result.status.to_s, 'response_received' => true }
memory['last_status'] = result.status.to_s
measured_result = TimeTracker.track { ping(url) }
if measured_result.result
create_event payload: { 'url' => url, 'status' => measured_result.status.to_s, 'response_received' => true, 'elapsed_time' => measured_result.elapsed_time }
memory['last_status'] = measured_result.status.to_s
else
create_event payload: { 'url' => url, 'response_received' => false }
create_event payload: { 'url' => url, 'response_received' => false, 'elapsed_time' => measured_result.elapsed_time }
memory['last_status'] = nil
end
end
Expand All @@ -75,7 +77,6 @@ def ping(url)
rescue
nil
end

end

end
22 changes: 22 additions & 0 deletions lib/time_tracker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class TimeTracker
attr_accessor :elapsed_time, :result

def self.track
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = yield
new(Process.clock_gettime(Process::CLOCK_MONOTONIC) - start, result)
end

def initialize(elapsed_time, result)
@elapsed_time = elapsed_time
@result = result
end

def method_missing(method_sym, *arguments, &block)
if @result.respond_to?(method_sym)
@result.send(method_sym, *arguments, &block)
else
super
end
end
end
11 changes: 11 additions & 0 deletions spec/controllers/http_status_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def agent.checked_url
expect(agent.memory['last_status']).to eq('200')
end

it "should record the time spent waiting for the reply" do
agent.receive events
expect(agent.the_created_events[0][:payload]['elapsed_time']).not_to be_nil
end

describe "but the status code is not 200" do
let(:status_code) { 500 }

Expand Down Expand Up @@ -236,6 +241,12 @@ def agent.checked_url
expect(agent.the_created_events[1][:payload]['url']).to eq(failing_url)
end

it "should record the time spent waiting for the reply" do
agent.receive events
expect(agent.the_created_events[0][:payload]['elapsed_time']).not_to be_nil
expect(agent.the_created_events[1][:payload]['elapsed_time']).not_to be_nil
end

end

end
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/time_tracker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rails_helper'

describe TimeTracker do
describe "#track" do
it "tracks execution time" do
tracked_result = TimeTracker.track { sleep(0.01) }
expect(tracked_result.elapsed_time).to satisfy {|v| v > 0.01 && v < 0.1}
end

it "returns the proc return value" do
tracked_result = TimeTracker.track { 42 }
expect(tracked_result.result).to eq(42)
end

it "returns an object that behaves like the proc result" do
tracked_result = TimeTracker.track { 42 }
expect(tracked_result.to_i).to eq(42)
expect(tracked_result + 1).to eq(43)
end
end
end

0 comments on commit 74a3c88

Please sign in to comment.