From 29007c9019e35220964b16fb578c44c6effd0763 Mon Sep 17 00:00:00 2001 From: Darren Cauthon Date: Thu, 14 Jul 2016 20:12:20 -0500 Subject: [PATCH] Add an option to fire http status events only when a change occurs. (#1582) --- app/models/agents/http_status_agent.rb | 10 ++++-- spec/controllers/http_status_agent_spec.rb | 42 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/models/agents/http_status_agent.rb b/app/models/agents/http_status_agent.rb index 0eef8b24..669017e5 100644 --- a/app/models/agents/http_status_agent.rb +++ b/app/models/agents/http_status_agent.rb @@ -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)} diff --git a/spec/controllers/http_status_agent_spec.rb b/spec/controllers/http_status_agent_spec.rb index d0881f7a..aceecf9c 100644 --- a/spec/controllers/http_status_agent_spec.rb +++ b/spec/controllers/http_status_agent_spec.rb @@ -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 }