Created
July 25, 2014 08:25
-
-
Save stonith/f2589fc0a5e43ef3c243 to your computer and use it in GitHub Desktop.
sentry logstash plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logstash/outputs/base' | |
require 'logstash/namespace' | |
class LogStash::Outputs::Sentry < LogStash::Outputs::Base | |
config_name 'sentry' | |
milestone 1 | |
config :key, :validate => :string, :required => true | |
config :secret, :validate => :string, :required => true | |
config :project_id, :validate => :string, :required => true | |
public | |
def register | |
require 'net/https' | |
require 'uri' | |
@url = "http://sentry.prodlabs.local/api/#{project_id}/store/" | |
@uri = URI.parse(@url) | |
@client = Net::HTTP.new(@uri.host, @uri.port) | |
@client.use_ssl = false | |
@client.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
@logger.debug("Client", :client => @client.inspect) | |
end | |
public | |
def receive(event) | |
return unless output?(event) | |
require 'securerandom' | |
packet = { | |
:event_id => SecureRandom.uuid.gsub('-', ''), | |
:timestamp => event['@timestamp'], | |
:message => event['fullmessage'] | |
} | |
packet[:level] = event['[fields][level]'] | |
#packet[:level] = event['level'] | |
packet[:platform] = 'logstash' | |
packet[:server_name] = event['hostname'] | |
packet[:extra] = event['fields'].to_hash | |
@logger.debug("Sentry packet", :sentry_packet => packet) | |
auth_header = "Sentry sentry_version=5," + | |
"sentry_client=raven_logstash/1.0," + | |
"sentry_timestamp=#{event['@timestamp'].to_i}," + | |
"sentry_key=#{@key}," + | |
"sentry_secret=#{@secret}" | |
request = Net::HTTP::Post.new(@uri.path) | |
begin | |
request.body = packet.to_json | |
request.add_field('X-Sentry-Auth', auth_header) | |
response = @client.request(request) | |
@logger.info("Sentry response", :request => request.inspect, :response => response.inspect) | |
raise unless response.code == '200' | |
rescue Exception => e | |
@logger.warn("Unhandled exception", :request => request.inspect, :response => response.inspect, :exception => e.inspect) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice rip from https://gist.github.com/clarkdave/edaab9be9eaa9bf1ee5f