Class: Fluent::RaygunOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
HandleTagNameMixin
Defined in:
lib/fluent/plugin/out_raygun.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initializeRaygunOutput

Returns a new instance of RaygunOutput.



18
19
20
21
22
# File 'lib/fluent/plugin/out_raygun.rb', line 18

def initialize
  require 'time'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/out_raygun.rb', line 24

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

#default_payload_format(tag, time, record) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fluent/plugin/out_raygun.rb', line 87

def default_payload_format(tag, time, record)
  {
    occurredOn: Time.at(time).utc.iso8601,
    details: {
      machineName: @hostname,
      error: {
        message: record['messages']
      },
      tags: [tag]
    }
  }
end

#format(tag, time, record) ⇒ Object



55
56
57
# File 'lib/fluent/plugin/out_raygun.rb', line 55

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#notify_raygun(tag, time, record) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/out_raygun.rb', line 69

def notify_raygun(tag, time, record)
  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)}"
  )
  post.body = JSON.generate(payload)

  @http.request(@uri, post)
end

#startObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/out_raygun.rb', line 39

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

#write(chunk) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin/out_raygun.rb', line 59

def write(chunk)
  chunk.msgpack_each do |tag, time, record|
    begin
      notify_raygun(tag, time, record)
    rescue StandardError => e
      $log.error('Raygun Error:', error_class: e.class, error: e.message)
    end
  end
end