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 warn warning info debug).to_set()
EVENT_KEYS =
%w(message msg timestamp level logger stacktrace backtrace).to_set()
DEFAULT_HOSTNAME_COMMAND =
'hostname'

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fluent/plugin/out_sentry.rb', line 25

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
 
  @report_levels = @report_levels.to_set()
  @report_levels.each do |report_level|
    unless LOG_LEVEL.include?(report_level)
      raise Fluent::ConfigError, "sentry: unsupported level in report_levels for 'report_level'"
    end
  end

  @tags_key = @tags_key.to_set()

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

  @configuration = Raven::Configuration.new
  @configuration.server = @endpoint_url
  @configuration.server_name = @hostname
  @configuration.send_modules = false
  @configuration.release = nil
  @client = Raven::Client.new(@configuration)
  @context = Raven::Context.new
end

#extract_tags(record, path = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fluent/plugin/out_sentry.rb', line 111

def extract_tags(record, path=nil)
  r = {}
  record.each { |k, v| 
    kpath = path.nil? ? k : path + "." + k
    if v.is_a?(Hash)
      r.merge!(extract_tags(v, kpath))
    else
      if @tags_key.include?(kpath)
        r[kpath] = v
      end
    end
  }
  return r
end

#format(tag, time, record) ⇒ Object



61
62
63
# File 'lib/fluent/plugin/out_sentry.rb', line 61

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

#notify_sentry(tag, time, record) ⇒ Object



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
106
107
108
109
# File 'lib/fluent/plugin/out_sentry.rb', line 79

def notify_sentry(tag, time, record)
  level = (record['level'] || @default_level).downcase
  if not @report_levels.include?(level)
    return
  end

  record["fluentd_tag"] = tag

  event = Raven::Event.new(
    :configuration => @configuration, 
    :context => @context, 
    :timestamp => record['timestamp'] || Time.at(time).utc.strftime('%Y-%m-%dT%H:%M:%S'),
    :level => level,
    :logger => record['logger'] || @default_logger,
    :message => record['message'] || record['msg'] || "",
  )

  stacktrace = record['backtrace'] || record['stacktrace']
  if stacktrace
    if @stacktrace_expand_json_escaping
      stacktrace = stacktrace.gsub(/\\[nt]/, '\n' => "\n", '\t' => "\t")
    end
    event.interface(:stacktrace) do |int|
      int.frames = event.stacktrace_interface_from(stacktrace)
    end
  end

  event.tags = event.tags.merge(extract_tags(record))
  event.extra = record.reject{ |key| EVENT_KEYS.include?(key) }
  @client.send_event(event)
end

#shutdownObject



65
66
67
# File 'lib/fluent/plugin/out_sentry.rb', line 65

def shutdown
  super
end

#startObject



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

def start
  super
end

#write(chunk) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/fluent/plugin/out_sentry.rb', line 69

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