Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created August 7, 2013 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danshultz/6176766 to your computer and use it in GitHub Desktop.
Save danshultz/6176766 to your computer and use it in GitHub Desktop.
Errbit Errors to ElasticSearch/Kibana
require 'net/http'
require 'json'
require 'time'
es_host = "127.0.0.1"
es_port = "9200"
host = Net::HTTP.new(es_host, es_port)
apps = %w(App1 App2)
build_notices = lambda { |problem|
lambda { |notice|
server_environment = notice.server_environment
{
"@source" => "errbit",
"@tags" => [problem.app_name],
"@timestamp" => notice.created_at,
"@source_host" => server_environment["hostname"],
"@message" => notice.message,
"@type" => problem.app_name,
"@fields" => {
"notice_id" => notice.id.to_s,
"error_class" => problem.error_class,
"url" => notice.request["url"]
}
}
}
}
post_to_es = lambda { |payload|
ts = payload["@timestamp"]
key = "errbit-#{ts.strftime('%Y.%m.%d')}"
type = payload["@type"]
id = payload["@fields"]["notice_id"]
url = "/#{key}/#{type}/#{id}"
puts "Posting data to #{url}"
host.request_post(url, JSON.generate(payload))
}
log_problem_to_es = lambda { |problem|
notices = problem.notices.map(&build_notices.call(problem))
notices.each(&post_to_es)
}
log_problems_to_es = lambda { |app_name|
problems = Problem.where(:app_name => app_name)
problems.each(&log_problem_to_es)
}
apps.each(&log_problems_to_es)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment