Class: Fluent::Plugin::SentryOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_sentry.rb

Instance Method Summary collapse

Constructor Details

#initializeSentryOutput

Returns a new instance of SentryOutput.



26
27
28
29
30
# File 'lib/fluent/plugin/out_sentry.rb', line 26

def initialize
  require 'time'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/fluent/plugin/out_sentry.rb', line 32

def configure(conf)
  super

  config = Sentry::Configuration.new
  config.dsn = @dsn
  config.send_modules = false
  config.transport.timeout = 5

  @client = Sentry::Client.new(config)
end

#format(tag, time, record) ⇒ Object



86
87
88
# File 'lib/fluent/plugin/out_sentry.rb', line 86

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

#formatted_to_msgpack_binaryObject



90
91
92
# File 'lib/fluent/plugin/out_sentry.rb', line 90

def formatted_to_msgpack_binary
  true
end

#write(chunk) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fluent/plugin/out_sentry.rb', line 43

def write(chunk)
  chunk.msgpack_each do |tag, time, record|
    begin
      event = Sentry::Event.new(configuration: @client.configuration)

      event.level = (record['level'] || @level).downcase
      event.environment = record['env'] || @environment

      if @type === :event
        log.warn(record, @tag_keys, @user_keys, @data_keys)
        event.message = record['message']
        event.user = record.select{ |key| @user_keys.include?(key) }
        event.extra = @data_keys.length() > 0 ? record.select{ |key| @data_keys.include?(key) } : record
        event.contexts = {'data' => { origin_data: record }}
        event.tags = event.tags.merge({ :tag => tag })
          .merge({ :timestamp => Time.at(time).strftime('%Y-%m-%d %H:%M:%S %Z') })
          .merge(record.select{ |key| (@tag_keys + @user_keys).include?(key) })
        event = event.to_hash
      elsif @type === :exception
        event = event.to_hash
        event['exception'] = Sentry::CustomExceptionInterface.new(
          type: record['message'] || '',
          message: (record[@e_describe] + (record[@e_line] ? (' on line:' + record[@e_line]) : '')) || '',
          stacktrace: Sentry::StacktraceInterface.new(frames: [Sentry::CustomStacktraceFrame.new(
            filename: record[@e_filename] || '',
            context_line: (record[@e_describe] + (record[@e_line] ? (' on line:' + record[@e_line]) : '')) || '',
            post_context: record[@e_stack] || (record[@e_describe] || ''),
          )])
        ).to_hash
        event['tags'] = { :tag => tag }
          .merge({ :timestamp => Time.at(time).strftime('%d-%b-%Y %H:%M:%S %Z') })
          .merge(record.select{ |key| (@tag_keys + @user_keys).include?(key) })
      end

      event['logger'] = @title

      @client.send_event(event)
    rescue => e
      log.error("Sentry Error:", :error_class => e.class, :error => e.message)
    end
  end
end