Skip to content

Instantly share code, notes, and snippets.

@clarkdave
Last active May 15, 2023 11:34
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save clarkdave/edaab9be9eaa9bf1ee5f to your computer and use it in GitHub Desktop.
Save clarkdave/edaab9be9eaa9bf1ee5f to your computer and use it in GitHub Desktop.
(Logstash) Sentry output plugin
# The MIT License (MIT)
# Copyright (c) 2014 Dave Clark
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
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 = "https://app.getsentry.com/api/#{project_id}/store/"
@uri = URI.parse(@url)
@client = Net::HTTP.new(@uri.host, @uri.port)
@client.use_ssl = true
@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['message']
}
packet[:level] = event['[fields][level]']
packet[:platform] = 'logstash'
packet[:server_name] = event['host']
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
end
@reedy
Copy link

reedy commented Oct 8, 2014

Is there a license to go with this? :)

@clarkdave
Copy link
Author

@reedy updated with MIT licence

@reedy
Copy link

reedy commented Oct 11, 2014

Great, thankyou :D

@vassilevsky
Copy link

Is there a reason this plugin is not yet in Logstash?

@cad
Copy link

cad commented Oct 5, 2015

I second @vassilevsky

@moebiuseye
Copy link

moebiuseye commented Jul 26, 2016

Any info on compatibility issues with latest versions of logstash? (And/or people using this in production?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment