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 warning info debug)
EVENT_KEYS =
%w(message timestamp time_spent level logger culprit release tags)
DEFAULT_ENVIRONMENT =

DEFAULT_HOSTNAME_COMMAND = 'hostname'

'default'

Instance Method Summary collapse

Constructor Details

#initializeSentryOutput

config_param :hostname_command, :string, :default => 'hostname'



18
19
20
21
22
23
24
# File 'lib/fluent/plugin/out_sentry.rb', line 18

def initialize
  require 'time'
  require 'sentry-ruby'
  require 'fluent/plugin/output'

  super
end

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
# 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

  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.transport.ssl_verification = false
  @configuration.transport.open_timeout = 5
  @configuration.transport.timeout = 5
  @configuration.dsn = @endpoint_url
  @configuration.server_name = nil
  @client = Sentry::Client.new(@configuration)
end

#format(tag, time, record) ⇒ Object



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

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

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 = {
    'container_name': record['kubernetes']['container_name'],
    'messageTemplate': record['messageTemplate'],
    'tenantId': record['fields']['TenantId'],
    'statusCode': record['fields']['StatusCode']
  }
  event.contexts[:Fields] = record['fields'] || {}
  event.contexts[:Kubernetes] = record['kubernetes'] || {}
  @client.send_event(event)
end

#shutdownObject



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

def shutdown
  super
end

#startObject



52
53
54
# File 'lib/fluent/plugin/out_sentry.rb', line 52

def start
  super
end

#write(chunk) ⇒ Object



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

def write(chunk)
  chunk.msgpack_each do |tag, time, record|
    begin
      if record.key?('level') && ['error'].include?(record['level'])
        notify_sentry(tag, time, record)
      end
    rescue => e
      $log.error("Sentry Error:", :error_class => e.class, :error => e.message)
    end
  end
end