diff --git a/lib/cronitor.rb b/lib/cronitor.rb index 3192595..082ad75 100644 --- a/lib/cronitor.rb +++ b/lib/cronitor.rb @@ -63,9 +63,6 @@ def self.job(key, &block) end end - def self.monitor_api_url - 'https://cronitor.io/api/monitors' - end end Cronitor.read_config(Cronitor.config) unless Cronitor.config.nil? diff --git a/lib/cronitor/config.rb b/lib/cronitor/config.rb index acc6e70..2c31a08 100644 --- a/lib/cronitor/config.rb +++ b/lib/cronitor/config.rb @@ -9,20 +9,22 @@ module Cronitor YAML_KEYS = MONITOR_TYPES.map { |t| "#{t}s" } class << self - attr_accessor :api_key, :api_version, :environment, :logger, :config, :timeout, :ping_timeout, :auto_discover_sidekiq + attr_accessor :api_key, :api_version, :environment, :logger, :config, :timeout, :ping_timeout, :auto_discover_sidekiq, :telemetry_domain def configure(&block) block.call(self) end + end self.api_key = ENV.fetch('CRONITOR_API_KEY', nil) self.api_version = ENV.fetch('CRONITOR_API_VERSION', nil) self.environment = ENV.fetch('CRONITOR_ENVIRONMENT', nil) - self.timeout = ENV.fetch('CRONITOR_TIMEOUT', nil) || 10 - self.ping_timeout = ENV.fetch('CRONITOR_PING_TIMEOUT', nil) || 5 + self.timeout = ENV.fetch('CRONITOR_TIMEOUT', 10) + self.ping_timeout = ENV.fetch('CRONITOR_PING_TIMEOUT', 5) self.config = ENV.fetch('CRONITOR_CONFIG', nil) self.auto_discover_sidekiq = ENV.fetch('CRONITOR_AUTO_DISCOVER_SIDEKIQ', 'true').casecmp('true').zero? # https://github.com/cronitorio/cronitor-sidekiq + self.telemetry_domain = ENV.fetch('CRONITOR_TELEMETRY_DOMAIN', 'cronitor.link') self.logger = Logger.new($stdout) logger.level = Logger::INFO end diff --git a/lib/cronitor/monitor.rb b/lib/cronitor/monitor.rb index 7e01089..c029d91 100644 --- a/lib/cronitor/monitor.rb +++ b/lib/cronitor/monitor.rb @@ -4,7 +4,7 @@ module Cronitor class Monitor attr_reader :key, :api_key, :api_version, :env - PING_RETRY_THRESHOLD = 5 + PING_RETRY_THRESHOLD = 3 module Formats ALL = [ @@ -24,19 +24,19 @@ module Headers }) end + def self.put(opts = {}) rollback = opts[:rollback] || false opts.delete(:rollback) monitors = opts[:monitors] || [opts] - + url = "https://cronitor.io/api/monitors" if opts[:format] == Cronitor::Monitor::Formats::YAML - url = "#{Cronitor.monitor_api_url}.yaml" + url = "#{url}.yaml" monitors['rollback'] = true if rollback body = YAML.dump(monitors) headers = Cronitor::Monitor::Headers::YAML else - url = Cronitor.monitor_api_url body = { monitors: monitors, rollback: rollback @@ -120,8 +120,7 @@ def ping(params = {}) begin ping_url = ping_api_url - ping_url = fallback_ping_api_url if retry_count > (PING_RETRY_THRESHOLD / 2) - + ping_url = fallback_ping_api_url if retry_count > Monitor::PING_RETRY_THRESHOLD response = HTTParty.get( ping_url, query: clean_params(params), @@ -159,7 +158,7 @@ def ok end def pause(hours = nil) - pause_url = "#{monitor_api_url}/pause" + pause_url = "#{monitor_api_url}/#{key}/pause" pause_url += "/#{hours}" unless hours.nil? resp = HTTParty.get( @@ -180,7 +179,7 @@ def unpause end def ping_api_url - "https://cronitor.link/p/#{api_key}/#{key}" + "https://#{Cronitor.telemetry_domain}/p/#{api_key}/#{key}" end def fallback_ping_api_url @@ -188,9 +187,10 @@ def fallback_ping_api_url end def monitor_api_url - "#{Cronitor.monitor_api_url}/#{key}" + "https://cronitor.io/api/monitors" end + private def fetch diff --git a/spec/cronitor_spec.rb b/spec/cronitor_spec.rb index 52bf6d5..351e1f7 100644 --- a/spec/cronitor_spec.rb +++ b/spec/cronitor_spec.rb @@ -27,11 +27,13 @@ cronitor.api_key = 'foo' cronitor.api_version = 'bar' cronitor.environment = 'baz' + cronitor.telemetry_domain = 'https://ping.com' end expect(Cronitor.api_key).to eq('foo') expect(Cronitor.api_version).to eq('bar') expect(Cronitor.environment).to eq('baz') + expect(Cronitor.telemetry_domain).to eq('https://ping.com') end end @@ -190,6 +192,21 @@ monitor.ping() end end + + context "when a custom ping domain is set" do + it "uses the custom domain in the ping request" do + Cronitor.telemetry_domain = 'ping.com' + Cronitor.api_key = FAKE_API_KEY + expect(HTTParty).to receive(:get).with( + "https://ping.com/p/#{FAKE_API_KEY}/test-key", + hash_including({ + headers: Cronitor::Monitor::Headers::JSON, + timeout: 5, + }) + ).and_return(instance_double(HTTParty::Response, code: 200)) + monitor.ping() + end + end end describe 'Monitor' do