class Fluent::SentryOutput < Fluent::BufferedOutput Fluent::Plugin.register_output('sentry', self) include Fluent::HandleTagNameMixin LOG_LEVEL = %w(fatal error warning info debug) EVENT_KEYS = %w(message timestamp time_spent level logger culprit server_name release tags) DEFAULT_HOSTNAME_COMMAND = 'hostname' DEFAULT_ENVIRONMENT = 'default' config_param :default_level, :string, :default => 'error' config_param :default_environment, :string, :default => 'default' config_param :default_logger, :string, :default => 'fluentd' config_param :endpoint_url, :string config_param :flush_interval, :time, :default => 0 config_param :hostname_command, :string, :default => 'hostname' def initialize require 'time' require 'json' require 'sentry-ruby' require 'fluent/plugin/output' super end 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 hostname_command = @hostname_command || DEFAULT_HOSTNAME_COMMAND @hostname = `#{hostname_command}`.chomp @default_environment = DEFAULT_ENVIRONMENT @configuration = Sentry::Configuration.new @configuration.send_modules = false @configuration.debug = true @configuration.dsn = @endpoint_url @configuration.server_name = @hostname @client = Sentry::Client.new(@configuration) end def start super end def format(tag, time, record) [tag, time, record].to_msgpack end def shutdown super end def write(chunk) chunk.msgpack_each do |tag, time, record| begin if record.key?('level') and record['level'] == 'error' notify_sentry(tag, time, record) end rescue => e $log.error("Sentry Error:", :error_class => e.class, :error => e.message) end end end def notify_sentry(tag, time, record) event = Sentry::Event.new( :configuration => @configuration, :message => record['message'] ) event.timestamp = record['timestamp'] || Time.at(time).utc.strftime('%Y-%m-%dT%H:%M:%S') event.level = record['level'] || @default_level event.environment = record['clustername'] || @default_environment event.tags = event.tags.merge({ :tag => tag }.merge(record['kubernetes'] || {})) event.contexts[:Fields] = record['fields'] || {} @client.send_event(event) end end