Class: Fluent::SentryOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
HandleTagNameMixin
Defined in:
lib/fluent/plugin/out_sentry.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

#initializeSentryOutput

Returns a new instance of SentryOutput.



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

def initialize
  require 'time'
  require 'raven'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fluent/plugin/out_sentry.rb', line 23

def configure(conf)
  super

  if @endpoint_url.nil?
    raise Fluent::ConfigError, "sentry: missing parameter for 'endpoint_url'"
  end

  unless LOG_LEVEL.include?(@default_level)
    raise Fluent::ConfigError, "sentry: unsupported default reporting log level for 'default_level'"
  end

  hostname_command = @hostname_command || DEFAULT_HOSTNAME_COMMAND
  @hostname = `#{hostname_command}`.chomp

  @configuration = Raven::Configuration.new
  @configuration.server = @endpoint_url
  @configuration.server_name = @hostname
  @client = Raven::Client.new(@configuration)
end

#format(tag, time, record) ⇒ Object



47
48
49
# File 'lib/fluent/plugin/out_sentry.rb', line 47

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

#notify_sentry(tag, time, record) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/out_sentry.rb', line 65

def notify_sentry(tag, time, record)
  event = Raven::Event.new(
    :configuration => @configuration, 
    :context => Raven::Context.new, 
    :message => record['message']
  )
  event.timestamp = record['timestamp'] || Time.at(time).utc.strftime('%Y-%m-%dT%H:%M:%S')
  event.time_spent = record['time_spent'] || nil
  event.level = record['level'] || @default_level
  event.logger = record['logger'] || @default_logger
  event.culprit = record['culprit'] || nil
  event.server_name = record['server_name'] || @hostname
  event.release = record['release'] if record['release']
  event.tags = event.tags.merge({ :tag => tag }.merge(record['tags'] || {}))
  event.extra = record.reject{ |key| EVENT_KEYS.include?(key) }
  @client.send_event(event)
end

#shutdownObject



51
52
53
# File 'lib/fluent/plugin/out_sentry.rb', line 51

def shutdown
  super
end

#startObject



43
44
45
# File 'lib/fluent/plugin/out_sentry.rb', line 43

def start
  super
end

#write(chunk) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/out_sentry.rb', line 55

def write(chunk)
  chunk.msgpack_each do |tag, time, record|
    begin
      notify_sentry(tag, time, record)
    rescue => e
      $log.error("Sentry Error:", :error_class => e.class, :error => e.message)
    end
  end
end