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)
EVENT_KEYS =
%w(message timestamp time_spent level logger culprit server_name release tags)
DEFAULT_HOSTNAME_COMMAND =
'hostname'

Instance Method Summary collapse

Constructor Details

#initializeRaygunOutput

Returns a new instance of RaygunOutput.



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

def initialize
  require 'time'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



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

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

#format(tag, time, record) ⇒ Object



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

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

#notify_raygun(tag, time, record) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent/plugin/out_raygun.rb', line 72

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

#shutdownObject



58
59
60
# File 'lib/fluent/plugin/out_raygun.rb', line 58

def shutdown
  super
end

#startObject



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

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



62
63
64
65
66
67
68
69
70
# File 'lib/fluent/plugin/out_raygun.rb', line 62

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