Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Airbrake client api
#!/usr/bin/env ruby
#v3 api
#https://airbrake.io/api/v3/projects?key=KEY
#https://airbrake.io/api/v3/projects/PROJECT_ID/groups?key=KEY
#https://airbrake.io/api/v3/projects/PROJECT_ID/groups/GROUP_ID?key=KEY
#https://airbrake.io/api/v3/projects/PROJECT_ID/groups/GROUP_ID/notices?key=KEY
#https://airbrake.io/api/v3/projects/PROJECT_ID/groups/GROUP_ID/last-notice?key=KEY
#https://airbrake.io/api/v3/projects/PROJECT_ID/group-environments?key=KEY
#https://airbrake.io/api/v3/projects/PROJECT_ID/deploys?key=KEY
#https://airbrake.io/api/v3/projects/PROJECT_ID/deploys/GROUP_ID?key=KEY
#https://help.airbrake.io/kb/api-2/notifier-api-v3
#v2 api
#https://help.airbrake.io/kb/api-2/api-overview
#https://SUBDOMAIN.airbrake.io/errors.xml?auth_token=KEY
#https://SUBDOMAIN.airbrake.io/errors/GROUP_ID.xml?auth_token=KEY
#https://github.com/spagalloco/airbrake-api
require 'open-uri'
require 'net/http'
require 'net/https'
require 'json'
class AirbrakeClient
def initialize(key, project_id, options={:environment => 'production', :resolved => 'true'})
@api_url_base = "https://airbrake.io/api/v3/projects/"
@project_id = project_id
@global_options =
{
:key => key,
}.merge(options)
end
def groups(page=1, error_types)
url = request_url('groups', :page => page)
response = make_request(url)
case response.code.to_i
when 200..299
json = JSON.load(response.body)
# succeeding = json['succeeding']
groups = json['groups']
errors = groups.select {|group|
if error_types.empty?
true
else
group['errors'].any? {|error|
error_type = error['type']
error_types.include?(error_type)
}
end
}
errors.map {|error| error['id'] }.to_a
else
fail "bad response #{response}"
end
end
def group_errors(group_id)
url = request_url("groups/#{group_id}/notices")
response = make_request(url)
case response.code.to_i
when 200..299
json = JSON.load(response.body)
# succeeding = json['succeeding']
notices = json['notices']
notices.flat_map {|notice|
notice.values_at 'params', 'session'
}
else
fail "bad response #{response}"
end
end
def make_request(url)
# puts "Making request to #{url}"
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
if http.use_ssl = (uri.scheme == 'https')
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(uri.request_uri)
http.request(request)
end
def request_url(path, options={:page => 1})
params = {
# :groups_start => 'GROUP_HASH',
}.
merge(@global_options).
merge(options).
map {|k,v|
"#{k}=#{v}"
}.join("&")
"#{@api_url_base}#{@project_id}/#{path}?#{params}"
end
end
if $0 == __FILE__
require 'pp'
range = (1..10)
error_types = ARGV.to_a
key = KEY
project_id = PROJECT_ID
puts "getting resolved groups"
client = AirbrakeClient.new(key, project_id, :resolved => 'true', :environment => 'production')
resolved_groups = range.flat_map {|page|
print '.'
client.groups(page, error_types)
}.compact.uniq
puts "getting unresolved groups"
client = AirbrakeClient.new(key, project_id, :resolved => 'false', :environment => 'production')
unresolved_groups = range.flat_map {|page|
print '.'
client.groups(page, error_types)
}.compact.uniq
puts
groups = resolved_groups | unresolved_groups
puts "got groups #{groups}"
puts "getting errors"
groups.map { |group_id|
errors = client.group_errors(group_id)
puts
pp errors
puts
errors
}.uniq
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.