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'
DEFAULT_ENVIRONMENT =
'default'

Instance Method Summary collapse

Constructor Details

#initializeSentryOutput

Returns a new instance of SentryOutput.



18
19
20
21
22
23
24
25
# File 'lib/fluent/plugin/out_sentry.rb', line 18

def initialize
  require 'time'
  require 'json'
  require 'sentry-ruby'
  require 'fluent/plugin/output'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/out_sentry.rb', line 27

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
  @default_environment = DEFAULT_ENVIRONMENT

  @configuration = Sentry::Configuration.new
  @configuration.send_modules = false
  @configuration.debug = true
  @configuration.dsn = @endpoint_url
  @configuration.server_name = @hostname
  @client = Sentry::Client.new(@configuration)
end

#format(tag, time, record) ⇒ Object



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

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

#notify_sentry(tag, time, record) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/out_sentry.rb', line 74

def notify_sentry(tag, time, record)
  event = Sentry::Event.new(
    :configuration => @configuration, 
    :message => record['message']
  )
  event.timestamp = record['timestamp'] || Time.at(time).utc.strftime('%Y-%m-%dT%H:%M:%S')
  event.level = record['level'] || @default_level
  event.environment = record['clustername'] || @default_environment
  event.tags = event.tags.merge({ :tag => tag }.merge(record['kubernetes'] || {}))
  event.contexts = event.contexts.merge(record['fields'])
  @client.send_event(event)
end

#shutdownObject



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

def shutdown
  super
end

#startObject



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

def start
  super
end

#write(chunk) ⇒ Object



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

def write(chunk)
  chunk.msgpack_each do |tag, time, record|
    begin
      if record.key?('level') and record['level'] == 'error'
        notify_sentry(tag, time, record)
      end
    rescue => e
      $log.error("Sentry Error:", :error_class => e.class, :error => e.message)
    end
  end
end