module Opsgenie::Heartbeat

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/opsgenie/heartbeat/config.rb, line 6
def self.configure
  self.configuration ||= Config.new
  yield(configuration)
end
create(name:, interval: nil, unit: nil, description: nil, enabled: nil, team: nil) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 64
def create(name:, interval: nil, unit: nil, description: nil, enabled: nil, team: nil)
  return unless configuration.enabled
  create_or_update(:post, name: name, description: description, interval: interval, unit: unit, enabled: enabled, team: team)
end
delete(name) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 44
def delete(name)
  return unless configuration.enabled
  name = configuration.name_transformer.call(name)

  begin
    uri = URI.parse(url_for_resource(:delete, name))
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    http.delete(uri.path, {'Authorization': "GenieKey #{configuration.api_key}", "Content-Type": "application/json"})
  rescue => e
    resolve_exception e
  end
end
ensure(name:, interval:, unit: , description:, enabled: true, team: nil) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 28
def ensure(name:, interval:, unit: , description:, enabled: true, team: nil)
  return unless configuration.enabled
  original_name = name
  name =  configuration.name_transformer.call(name)

  uri = URI.parse(url_for_resource(:get, name))
  with_timeout_retries do
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    response = http.get(uri.path, {'Authorization': "GenieKey #{configuration.api_key}"})
    unless response.is_a?(Net::HTTPSuccess)
      create(name: original_name, description: description, interval: interval, unit: unit, enabled: enabled, team: team)
    end
  end
end
pulse(name) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 10
def pulse(name)
  return unless configuration.enabled
  name = configuration.name_transformer.call(name)
  begin
    uri = URI.parse("https://api.opsgenie.com/v2/heartbeats/#{Rack::Utils.escape name}/ping")
    with_timeout_retries do
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      response = http.get(uri.path, {'Authorization': "GenieKey #{configuration.api_key}"})
      if !response.is_a?(Net::HTTPSuccess)
        configuration.logger.info("Error creating or updating heartbeat: #{response}") if configuration.logger
      end
    end
  rescue => e
    resolve_exception e
  end
end
update(name:, interval: nil, unit: nil, description: nil, enabled: nil, team: nil) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 59
def update(name:, interval: nil, unit: nil, description: nil, enabled: nil, team: nil)
  return unless configuration.enabled
  create_or_update(:patch, name: name, description: description, interval: interval, unit: unit, enabled: enabled, team: team)
end

Private Class Methods

create_or_update(verb, name:,description:,interval:,unit:, enabled:, team:) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 98
def create_or_update(verb, name:,description:,interval:,unit:, enabled:, team:)
  name = configuration.name_transformer.call(name)

  begin
    uri = URI.parse(url_for_resource(verb, name))
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    team_to_assign = case team
    when nil then configuration.default_team
    when false then nil
    else
      team
    end
    doc = {
      name: name,
      description: description,
      interval: interval,
      intervalUnit: unit,
      enabled: enabled,
      ownerTeam: team_to_assign
    }.reject {|_, value| value.nil?}
    response = http.public_send(verb, uri.path, doc.to_json, {'Authorization': "GenieKey #{configuration.api_key}", "Content-Type": "application/json"})

    if !response.is_a?(Net::HTTPSuccess)
      configuration.logger.info("Error creating or updating heartbeat: #{response}") if configuration.logger
    end

  rescue => e
    resolve_exception e
  end
end
resolve_exception(e) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 130
def resolve_exception e
  configuration.logger.info("Exception raised during heartbeat: #{e.message} #{e.backtrace}") if configuration.logger
  raise if configuration.raise_error
end
url_for_resource(verb, name) click to toggle source
# File lib/opsgenie/heartbeat.rb, line 90
def url_for_resource(verb, name)
  if verb == :post
    'https://api.opsgenie.com/v2/heartbeats'
  else
    "https://api.opsgenie.com/v2/heartbeats/#{Rack::Utils.escape name}"
  end
end
with_timeout_retries() { || ... } click to toggle source
# File lib/opsgenie/heartbeat.rb, line 72
def with_timeout_retries
  attempts = 0
  begin
    yield
  rescue Net::OpenTimeout, Net::ReadTimeout => e
    if attempts < (configuration.retries || 0)
      configuration.logger.info("Retrying opsgenie api call after timeout #{e.message}")
      attempts += 1
      sleep(rand(2**attempts))
      retry
    else
      raise
    end
  end
end