lib/fluent/plugin/out_raygun.rb in fluent-plugin-raygun-0.0.1 vs lib/fluent/plugin/out_raygun.rb in fluent-plugin-raygun-0.0.2
- old
+ new
@@ -1,20 +1,21 @@
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'
+ LOG_LEVEL = %w[fatal error warning info debug].freeze
+ EVENT_KEYS = %w[message timestamp time_spent level logger culprit server_name release tags].freeze
+ DEFAULT_HOSTNAME_COMMAND = 'hostname'.freeze
- 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 :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'
+ config_param :flush_interval, :time, default: 0
+ config_param :hostname_command, :string, default: 'hostname'
+ config_param :record_already_formatted, :bool, default: false
def initialize
require 'time'
super
@@ -46,46 +47,53 @@
@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.."
+ 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)
+ rescue StandardError => 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],
- }
- }
+ payload =
+ if @record_already_formatted
+ # Setting @record_already_formatted = true, means you
+ # are already happy with the formatting of 'record'
+ record
+ else
+ default_payload_format(tag, time, record)
+ end
post = Net::HTTP::Post.new(
- "#{@endpoint_url}/entries?apikey=#{URI::encode(@api_key)}",
+ "#{@endpoint_url}/entries?apikey=#{URI.encode(@api_key)}"
)
post.body = JSON.generate(payload)
- response = @http.request(@uri, post)
+ @http.request(@uri, post)
+ end
+
+ def default_payload_format(tag, time, record)
+ {
+ occurredOn: Time.at(time).utc.iso8601,
+ details: {
+ machineName: @hostname,
+ error: {
+ message: record['messages']
+ },
+ tags: [tag]
+ }
+ }
end
end