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.



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

def initialize
  require 'time'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



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

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



107
108
109
# File 'lib/fluent/plugin/out_sentry.rb', line 107

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

#formatted_to_msgpack_binaryObject



111
112
113
# File 'lib/fluent/plugin/out_sentry.rb', line 111

def formatted_to_msgpack_binary
  true
end

#write(chunk) ⇒ Object



42
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fluent/plugin/out_sentry.rb', line 42

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
        event.message = record['message'] || tag
        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({ :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

        event['logger'] = tag
      elsif @type === :exception
        event.tags = { :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

        event['logger'] = tag

        frame = Array.new
        if record.include?(@e_stack)
          record[@e_stack].split("\n") do |value|
            match = value.match(/\#(?<no>\d+) (?<filename>.*?)\((?<lineno>\d+)\): (?<context_line>[\s\S]+)/)
            if match != nil
              frame.unshift(Sentry::CustomStacktraceFrame.new(
                filename: match[:filename],
                context_line: match[:context_line],
                pre_context: "//...\n",
                post_context: "//...\n",
                lineno: Integer(match[:lineno]),
              ))
            end
          end
        elsif
          frame.push(Sentry::CustomStacktraceFrame.new(
            filename: record.include?(@e_filename) ? record[@e_filename] : '',
            context_line: record.include?(@e_describe) ? record[@e_describe] : '',
            pre_context: "//...\n",
            post_context: "//...\n",
            lineno: record.include?(@e_line) ? Integer(record[@e_line]) : 1
          ))
        end

        event['exception'] = Sentry::CustomExceptionInterface.new(
          type: record['message'] || '',
          message: record.include?(@e_describe) ? record[@e_describe] : '',
          stacktrace: Sentry::StacktraceInterface.new(frames: frame)
        ).to_hash
        event['message'] = record.include?(@e_describe) ? (record[@e_describe] + ' in ' + record[@e_filename] + ' on line ' + record[@e_line]) : ''
      end

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