Class: AppMonitor::EventNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/appmonitor/event_notifier.rb

Constant Summary collapse

API_MESSAGE_URL =
'http://team10-14.ucebne.fiit.stuba.sk/api/message'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ EventNotifier

Returns a new instance of EventNotifier.



12
13
14
15
16
# File 'lib/appmonitor/event_notifier.rb', line 12

def initialize(app)
  @app = app
  configure
  @config[:ignore_exceptions] ||= self.class.default_ignore_exceptions
end

Class Method Details

.default_ignore_exceptionsObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/appmonitor/event_notifier.rb', line 37

def self.default_ignore_exceptions
  [].tap do |exceptions|
    exceptions << 'ActionController::RoutingError'
    exceptions << 'ActionController::InvalidAuthenticityToken'
    exceptions << 'CGI::Session::CookieStore::TamperedWithCookie'
    exceptions << 'ActionController::UnknownAction'
    exceptions << 'ActionController::UnknownFormat'
    exceptions << 'ActionController::UnknownHttpMethod'
    exceptions << 'Mongoid::Errors::DocumentNotFound'
    exceptions << 'AbstractController::ActionNotFound'
  end
end

Instance Method Details

#call(env) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/appmonitor/event_notifier.rb', line 54

def call(env)
  @app.call(env)
rescue Exception => exception
  if @config[:monitoring]["#{Rails.env.to_s.downcase}"]
    @request = ActionDispatch::Request.new(env)
    send_exception(exception)
  end
  raise exception
end

#configure(opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/appmonitor/event_notifier.rb', line 18

def configure(opts = {})
  @config = {
      api_key: '',
      project_id: '',
      monitoring: {development: '', staging: '', production: ''}
  }
  @valid_config_keys = @config.keys
  path_to_yaml_file = "#{Rails.root}/config/appmonitor.yml"
  begin
    config = YAML::load(IO.read(path_to_yaml_file))
    Rails.logger.info(config)
  rescue Errno::ENOENT
    return
  rescue Psych::SyntaxError
    return
  end
  config.each { |k, v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym }
end

#ignored_exception(ignore_array, exception) ⇒ Object



50
51
52
# File 'lib/appmonitor/event_notifier.rb', line 50

def ignored_exception(ignore_array, exception)
  Array.wrap(ignore_array).map(&:to_s).include?(exception.class.name)
end

#notify_on_event(type) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/appmonitor/event_notifier.rb', line 86

def notify_on_event(type)
  uri = URI.parse(API_MESSAGE_URL)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = false
  request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => "application/json",
                                                        'appm_apiKey' => @config[:api_key],
                                                        'appm_projectId' => @config[:project_id],
                                                        'appm_type' => type}
  )

  request.body = @event
  response = https.request(request)

  Rails.logger.debug ("Appmonitor: Event has been sent")
  puts 'Appmonitor: Event has been sent'

  unless response.kind_of? Net::HTTPSuccess
    Rails.logger.warn('Appmonitor WARNING: Something went wrong, please check your config')
    Rails.logger.warn("AppMonitor WARNING: Response ''#{response.code}: #{response.message}''")
    puts 'Appmonitor WARNING: Something went wrong, please check your config'
    puts "AppMonitor WARNING: Response #{response.inspect}"
  end
end

#send_custom_event(type, options = {}) ⇒ Object



81
82
83
84
# File 'lib/appmonitor/event_notifier.rb', line 81

def send_custom_event(type, options={})
  @event = options.to_json
  notify_on_event(type)
end

#send_exception(exception) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/appmonitor/event_notifier.rb', line 64

def send_exception(exception)
  unless ignored_exception(@config[:ignore_exceptions], exception)
    event = EventNotification.build_exception_hash(exception, @request)
    @event = event.to_json
    notify_on_event("Error")
  end
end

#send_rake_event(exception, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/appmonitor/event_notifier.rb', line 72

def send_rake_event(exception, options = {})
  configure
  unless (ignored_exception(@config[:ignore_exceptions], exception) ||  !@config[:monitoring]["#{Rails.env.to_s.downcase}"])
    event = EventNotification.build_rake_event(exception, options)
    @event = event.to_json
    notify_on_event("RakeError")
  end
end