class Hotdog::Sources::Datadog
Public Class Methods
new(application)
click to toggle source
Calls superclass method
Hotdog::Sources::BaseSource::new
# File lib/hotdog/sources/datadog.rb, line 12 def initialize(application) super(application) options[:endpoint] = ENV.fetch("DATADOG_HOST", "https://app.datadoghq.com") options[:api_key] = ENV["DATADOG_API_KEY"] options[:application_key] = ENV["DATADOG_APPLICATION_KEY"] @dog = nil # lazy initialization end
Public Instance Methods
api_key()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 32 def api_key() if options[:api_key] options[:api_key] else update_api_key! if options[:api_key] options[:api_key] else raise("DATADOG_API_KEY is not set") end end end
application_key()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 45 def application_key() if options[:application_key] options[:application_key] else update_application_key! if options[:application_key] options[:application_key] else raise("DATADOG_APPLICATION_KEY is not set") end end end
cancel_downtime(id, options={})
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 67 def cancel_downtime(id, options={}) code, cancel = dog.cancel_downtime(id) if code.to_i / 100 != 2 raise("dog.cancel_downtime(%s) returns [%s, %s]" % [id.inspect, code.inspect, cancel.inspect]) end cancel end
endpoint()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 28 def endpoint() options[:endpoint] end
get_all_downtimes(options={})
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 75 def get_all_downtimes(options={}) now = Time.new.to_i Array(datadog_get("/api/v1/downtime")).select { |downtime| # active downtimes downtime["active"] and ( downtime["start"].nil? or downtime["start"] < now ) and ( downtime["end"].nil? or now <= downtime["end"] ) and downtime["monitor_id"].nil? } end
id()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 20 def id() Hotdog::SOURCE_DATADOG end
name()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 24 def name() "datadog" end
schedule_downtime(scope, options={})
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 58 def schedule_downtime(scope, options={}) code, schedule = dog.schedule_downtime(scope, :start => options[:start].to_i, :end => (options[:start]+options[:downtime]).to_i) logger.debug("dog.schedule_donwtime(%s, :start => %s, :end => %s) #==> [%s, %s]" % [scope.inspect, options[:start].to_i, (options[:start]+options[:downtime]).to_i, code.inspect, schedule.inspect]) if code.to_i / 100 != 2 raise("dog.schedule_downtime(%s, ...) returns [%s, %s]" % [scope.inspect, code.inspect, schedule.inspect]) end schedule end
Private Instance Methods
datadog_get(request_path, query=nil)
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 124 def datadog_get(request_path, query=nil) query ||= URI.encode_www_form(api_key: self.api_key, application_key: self.application_key) uri = URI.join(self.endpoint, "#{request_path}?#{query}") begin response = uri.open("User-Agent" => "hotdog/#{Hotdog::VERSION}") { |fp| fp.read } MultiJson.load(response) rescue OpenURI::HTTPError => error code, _body = error.io.status raise(RuntimeError.new("datadog: GET #{request_path} returns [#{code.inspect}, ...]")) end end
dog()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 120 def dog() @dog ||= Dogapi::Client.new(self.api_key, self.application_key) end
update_api_key!()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 136 def update_api_key!() if options[:api_key_command] logger.info("api_key_command> #{options[:api_key_command]}") options[:api_key] = IO.popen(options[:api_key_command]) do |io| io.read.strip end unless $?.success? raise("failed: #{options[:api_key_command]}") end else update_keys! end end
update_application_key!()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 150 def update_application_key!() if options[:application_key_command] logger.info("application_key_command> #{options[:application_key_command]}") options[:application_key] = IO.popen(options[:application_key_command]) do |io| io.read.strip end unless $?.success? raise("failed: #{options[:application_key_command]}") end else update_keys! end end
update_keys!()
click to toggle source
# File lib/hotdog/sources/datadog.rb, line 164 def update_keys!() if options[:key_command] logger.info("key_command> #{options[:key_command]}") options[:api_key], options[:application_key] = IO.popen(options[:key_command]) do |io| io.read.strip.split(":", 2) end unless $?.success? raise("failed: #{options[:key_command]}") end end end