Class: LogStash::Outputs::Sentry

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/sentry.rb

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/logstash/outputs/sentry.rb', line 84

def receive(event)
  return unless output?(event)

  require 'securerandom'

  #use message from event if exists, if not from static
  message_to_send = event["#{msg}"] || "#{msg}"
  if strip_timestamp
    #remove timestamp from message if available
    message_matched = message_to_send.match(/\d\d\d\d\-\d\d\-\d\d\s[0-9]{1,2}\:\d\d\:\d\d,\d{1,}\s(.*)/)
    message_to_send = message_matched ? message_matched[1] : message_to_send
  end

  packet = {
    :event_id => SecureRandom.uuid.gsub('-', ''),
    :timestamp => event['@timestamp'],
    :message => message_to_send,
    :level => "#{level_tag}",
    :platform => 'logstash',
    :server_name => event['host'],
    :extra => event.to_hash,
  }

  if fields_to_tags
    packet[:tags] = event.to_hash
  end

  @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

#registerObject


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/logstash/outputs/sentry.rb', line 69

def register
  require 'net/https'
  require 'uri'

  @url = "%{proto}://#{host}/api/#{project_id}/store/" % { :proto => use_ssl ? 'https' : 'http' }
  @uri = URI.parse(@url)

  @client = Net::HTTP.new(@uri.host, @uri.port)
  @client.use_ssl = use_ssl
  @client.verify_mode = OpenSSL::SSL::VERIFY_NONE

  @logger.debug('Client', :client => @client.inspect)
end