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.



22
23
24
25
26
# File 'lib/fluent/plugin/out_sentry.rb', line 22

def initialize
  require 'time'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/out_sentry.rb', line 28

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



76
77
78
# File 'lib/fluent/plugin/out_sentry.rb', line 76

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

#formatted_to_msgpack_binaryObject



80
81
82
# File 'lib/fluent/plugin/out_sentry.rb', line 80

def formatted_to_msgpack_binary
  true
end

#write(chunk) ⇒ Object



39
40
41
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
# File 'lib/fluent/plugin/out_sentry.rb', line 39

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

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

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

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