Skip to content

Commit

Permalink
Merge pull request #28 from cronitorio/extract-pint-api-endpoint-into…
Browse files Browse the repository at this point in the history
…-setting

Extract pint api endpoint into setting
  • Loading branch information
aflanagan authored Feb 21, 2024
2 parents bbc8817 + f5875a9 commit 858ccb0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
3 changes: 0 additions & 3 deletions lib/cronitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
8 changes: 5 additions & 3 deletions lib/cronitor/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions lib/cronitor/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand 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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand All @@ -180,17 +179,18 @@ 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
"https://cronitor.io/p/#{api_key}/#{key}"
end

def monitor_api_url
"#{Cronitor.monitor_api_url}/#{key}"
"https://cronitor.io/api/monitors"
end


private

def fetch
Expand Down
17 changes: 17 additions & 0 deletions spec/cronitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 858ccb0

Please sign in to comment.