class Fluent::SentryOutput

Constants

DEFAULT_HOSTNAME_COMMAND
EVENT_KEYS

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_sentry.rb, line 62
def shutdown
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_sentry.rb, line 54
def start
  super
end
write(chunk) click to toggle source
# 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