class Fluent::RaygunOutput < Fluent::BufferedOutput Fluent::Plugin.register_output('raygun', self) include Fluent::HandleTagNameMixin LOG_LEVEL = %w(fatal error warning info debug) EVENT_KEYS = %w(message timestamp time_spent level logger culprit server_name release tags) DEFAULT_HOSTNAME_COMMAND = 'hostname' config_param :default_level, :string, :default => 'error' config_param :default_logger, :string, :default => 'fluentd' config_param :endpoint_url, :string, :default => 'https://api.raygun.com' config_param :api_key, :string config_param :flush_interval, :time, :default => 0 config_param :hostname_command, :string, :default => 'hostname' def initialize require 'time' super end def configure(conf) super if @endpoint_url.nil? raise Fluent::ConfigError, "Raygun: missing parameter for 'endpoint_url'" end unless LOG_LEVEL.include?(@default_level) raise Fluent::ConfigError, "Raygun: unsupported default reporting log level for 'default_level'" end hostname_command = @hostname_command || DEFAULT_HOSTNAME_COMMAND @hostname = `#{hostname_command}`.chomp end def start super require 'net/http/persistent' require 'uri' @uri = URI @endpoint_url @http = Net::HTTP::Persistent.new @http.headers['Content-Type'] = 'text/json' @http.headers['X-ApiKey'] = @api_key @http.idle_timeout = 10 @http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1] log.debug "Started Raygun fluent shipper.." end def format(tag, time, record) [tag, time, record].to_msgpack end def shutdown super end def write(chunk) chunk.msgpack_each do |tag, time, record| begin notify_raygun(tag, time, record) rescue => e $log.error("Raygun Error:", :error_class => e.class, :error => e.message) end end end def notify_raygun(tag, time, record) payload = { occurredOn: Time.at(time).utc.iso8601, details: { machineName: @hostname, error: { message: record['messages'] }, tags: [tag], } } post = Net::HTTP::Post.new( "#{@endpoint_url}/entries?apikey=#{URI::encode(@api_key)}", ) post.body = JSON.generate(payload) response = @http.request(@uri, post) end end