Add an option to fire http status events only when a change occurs. (#1582)

This commit is contained in:
Darren Cauthon 2016-07-14 20:12:20 -05:00 committed by Andrew Cantino
parent 8c0a10fc4b
commit 29007c9019
2 changed files with 50 additions and 2 deletions

View file

@ -11,7 +11,8 @@ module Agents
default_schedule "every_12h"
form_configurable :url
form_configurable :disable_redirect_follow, type: :array, values: ['true', 'false']
form_configurable :disable_redirect_follow, type: :boolean
form_configurable :changes_only, type: :boolean
form_configurable :headers_to_save
description <<-MD
@ -20,6 +21,8 @@ module Agents
Specify a `Url` and the Http Status Agent will produce an event with the HTTP status code. If you specify one or more `Headers to save` (comma-delimited) as well, that header or headers' value(s) will be included in the event.
The `disable redirect follow` option causes the Agent to not follow HTTP redirects. For example, setting this to `true` will cause an agent that receives a 301 redirect to `http://yahoo.com` to return a status of 301 instead of following the redirect and returning 200.
The `changes only` option causes the Agent to report an event only when the status changes. If set to false, an event will be created for every check. If set to true, an event will only be created when the status changes (like if your site goes from 200 to 500).
MD
event_description <<-MD
@ -72,11 +75,14 @@ module Agents
# Track time
measured_result = TimeTracker.track { ping(url) }
current_status = measured_result.result ? measured_result.status.to_s : ''
return if options['changes_only'] == 'true' && current_status == memory['last_status'].to_s
payload = { 'url' => url, 'response_received' => false, 'elapsed_time' => measured_result.elapsed_time }
# Deal with failures
if measured_result.result
payload.merge!({ 'response_received' => true, 'status' => measured_result.status.to_s })
payload.merge!({ 'response_received' => true, 'status' => current_status })
# Deal with headers
if local_headers.present?
header_results = measured_result.result.headers.select {|header, value| local_headers.include?(header)}

View file

@ -147,6 +147,48 @@ describe 'HttpStatusAgent' do
expect(agent.the_created_events[0][:payload]['headers']).to be_nil
end
describe "but the last status code was 200" do
before { agent.memory['last_status'] = '200' }
describe "and no duplication settings have been set" do
it "should create one event" do
agent.receive events
expect(agent.the_created_events.count).to eq(1)
end
end
describe "and change settings have been set to true" do
before { agent.options['changes_only'] = 'true' }
it "should NOT create any events" do
agent.receive events
expect(agent.the_created_events.count).to eq(0)
end
describe "but actually, the ping failed" do
let(:failing_url) { SecureRandom.uuid }
let(:event_with_a_failing_ping) { Event.new.tap { |e| e.payload = { url: failing_url, headers_to_save: "" } } }
let(:events) do
[event_with_a_successful_ping, event_with_a_failing_ping]
end
it "should create an event" do
agent.receive events
expect(agent.the_created_events.count).to eq(1)
end
end
end
describe "and change settings have been set to false" do
before { agent.options['changes_only'] = 'false' }
it "should create one event" do
agent.receive events
expect(agent.the_created_events.count).to eq(1)
end
end
end
describe "but the status code is not 200" do
let(:status_code) { 500 }