class RollbarClient

Public Class Methods

new(config) click to toggle source
# File lib/integrations/rollbar_client.rb, line 11
def initialize(config)
  @user = config["username"]
  @key = config["api_key"]
  @project = config["project"]
  @environment = config["environment"]
  @errors = @key.nil? ? [] : errors()
  @deploys = @key.nil? ? [] : deploys()
end

Private Instance Methods

deploys() click to toggle source
# File lib/integrations/rollbar_client.rb, line 22
def deploys()
  fetch_deploys()
    .map do |deploy|
      {
        commit_id: deploy["revision"].to_sym,
        environment: deploy["environment"],
        timestamp: Time.at(deploy["start_time"]).to_datetime
      }
    end
    .sort_by { |deploy| deploy[:timestamp] }
    .select { |deploy| deploy[:environment] == @environment }
end
detail_error(item_id) click to toggle source
# File lib/integrations/rollbar_client.rb, line 117
def detail_error(item_id)
  Loggr.instance.info("FETCHING ERROR: #{item_id}")
  JSON.parse(Net::HTTP.get(
    URI("https://api.rollbar.com/api/1/item/#{item_id}/instances/?access_token=#{@key}")
  ))
end
errors() click to toggle source

TODO: Cached errors are already applied, maybe skip…

# File lib/integrations/rollbar_client.rb, line 36
def errors()
  cached_errors = load_from_cache()

  errors = fetch_errors(cached_errors)["items"]
    .flat_map(&method(:select_traces))
    .reject(&:nil?)
    .map do |trace|
      stack_trace = trace["frames"].map do |frame|
        file = frame["filename"]
        line = frame["lineno"]
        function = frame["method"]

        {
          file: file,
          line: line,
          function: function
        }
      end

      {
        error_id: trace[:id],
        first_time: trace[:first_time],
        last_time: trace[:last_time],
        environment: trace[:environment],
        type: trace[:type],
        message: trace[:message],
        link: "https://rollbar.com/#{@user}/#{@project}/items/#{trace[:counter]}/",
        total_occurrences: trace[:total_occurrences],
        stack_trace: stack_trace.reverse()
      }
    end
    .concat(cached_errors)
    .select { |error| error[:environment] == @environment }
    .sort_by { |error| error[:last_time] }

  errors.each { |error| Store::Error::index(error) }
  Store::Error::cache()

  errors
end
fetch_deploys(page=1) click to toggle source
# File lib/integrations/rollbar_client.rb, line 99
def fetch_deploys(page=1)
  deploys = []
  while true do
    resp = JSON.parse(Net::HTTP.get(
      URI("https://api.rollbar.com/api/1/deploys/?access_token=#{@key}&page=#{page}")
    ))["result"]["deploys"]

    break if resp.count == 0

    deploys.concat(resp)
    page += 1
  end
  Cache.write(DEPLOY_CACHE_KEY, deploys)
  deploys
rescue
  deploys
end
fetch_errors(cached_errors, page=1) click to toggle source
# File lib/integrations/rollbar_client.rb, line 124
def fetch_errors(cached_errors, page=1)
  errors = {"items" => [], "count" => 1}
  while errors["items"].length < errors["count"] do
    resp = JSON.parse(Net::HTTP.get(
      URI("https://api.rollbar.com/api/1/items/?access_token=#{@key}&page=#{page}")
    ))

    errors["count"] = resp["total_count"]

    while item = resp["result"]["items"].shift() do
      if cached_errors.detect { |cached_item| cached_item[:error_id] == item["id"].to_s.to_sym }
        return errors
      else
        errors["items"] << item
      end
    end

    page += 1
  end
  errors
rescue
  errors
end
select_traces(item) click to toggle source
# File lib/integrations/rollbar_client.rb, line 77
def select_traces(item)
  error = detail_error(item["id"])["result"]["instances"].first
  traces = error["data"]["body"]["trace"].present? ?
    [error["data"]["body"]["trace"]] :
    error["data"]["body"]["trace_chain"]

  return nil if traces.nil?

  traces.map do |trace|
    trace[:id] = item["id"].to_s.to_sym
    trace[:environment] = item["environment"]
    trace[:total_occurrences] = item["total_occurrences"]
    trace[:error] = error
    trace[:type] = item["level"]
    trace[:message] = item["title"]
    trace[:first_time] = Time.at(item["first_occurrence_timestamp"]).to_datetime
    trace[:last_time] = Time.at(item["last_occurrence_timestamp"]).to_datetime
    trace[:counter] = item["counter"]
    trace
  end
end