Class: Fluent::SentryOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
HandleTagNameMixin
Defined in:
lib/fluent/plugin/out_sentry.rb

Constant Summary collapse

EVENT_KEYS =
%w(message msg timestamp level logger).to_set()
DEFAULT_HOSTNAME_COMMAND =
'hostname'

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



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

def configure(conf)
  super

  if @endpoint_url.nil?
    raise Fluent::ConfigError, "sentry: missing parameter for 'endpoint_url'"
  end

  @report_levels = @report_levels.to_set()
  @tags_key = @tags_key.to_set()

  @userid_key_patterns = []
  @userid_key.each do |key_pattern|
    keys = key_pattern.split("/")
    @userid_key_patterns.push(keys)
  end

  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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fluent/plugin/out_sentry.rb', line 117

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

#extract_userid(record) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/fluent/plugin/out_sentry.rb', line 132

def extract_userid(record)
  @userid_key_patterns.each do |pattern|
    values = pattern.each.map do |key| record[key] end
    if values.all?
      return values.join("/")
    end
  end
  return nil
end

#format(tag, time, record) ⇒ Object



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

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

#notify_sentry(tag, time, record) ⇒ Object



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
106
107
108
109
110
111
112
113
114
115
# File 'lib/fluent/plugin/out_sentry.rb', line 76

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'] || "",
    :release => record['release'] || nil,
    :environment => @environment,
  )

  stacktrace = record['stacktrace']
  if stacktrace
    if @stacktrace_expand_json_escaping
      stacktrace = stacktrace.gsub(/\\[nt]/, '\n' => "\n", '\t' => "\t")
      record['stacktrace'] = stacktrace
    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) }

  user_id = extract_userid(record)
  if user_id != nil
    event.user = {"id": user_id}
  end

  @client.send_event(event)
end

#shutdownObject



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

def shutdown
  super
end

#startObject



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

def start
  super
end

#write(chunk) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/fluent/plugin/out_sentry.rb', line 66

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