Class: RailsRequestStats::NotificationSubscribers

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_request_stats/notification_subscribers.rb

Constant Summary collapse

SCHEMA_NAME =
'SCHEMA'.freeze
CACHE_NAME =
'CACHE'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.after_object_spaceObject

Returns the value of attribute after_object_space.



7
8
9
# File 'lib/rails_request_stats/notification_subscribers.rb', line 7

def after_object_space
  @after_object_space
end

.before_object_spaceObject

Returns the value of attribute before_object_space.



7
8
9
# File 'lib/rails_request_stats/notification_subscribers.rb', line 7

def before_object_space
  @before_object_space
end

.cache_hit_countObject

Returns the value of attribute cache_hit_count.



7
8
9
# File 'lib/rails_request_stats/notification_subscribers.rb', line 7

def cache_hit_count
  @cache_hit_count
end

.cache_read_countObject

Returns the value of attribute cache_read_count.



7
8
9
# File 'lib/rails_request_stats/notification_subscribers.rb', line 7

def cache_read_count
  @cache_read_count
end

.cached_query_countObject

Returns the value of attribute cached_query_count.



7
8
9
# File 'lib/rails_request_stats/notification_subscribers.rb', line 7

def cached_query_count
  @cached_query_count
end

.query_countObject

Returns the value of attribute query_count.



7
8
9
# File 'lib/rails_request_stats/notification_subscribers.rb', line 7

def query_count
  @query_count
end

.requestsObject

Returns the value of attribute requests.



7
8
9
# File 'lib/rails_request_stats/notification_subscribers.rb', line 7

def requests
  @requests
end

Class Method Details

.at_exit_handlerObject



55
56
57
58
59
# File 'lib/rails_request_stats/notification_subscribers.rb', line 55

def self.at_exit_handler
  @requests.each_value do |request_stats|
    Rails.logger.info { Report.new(request_stats).exit_report_text }
  end
end

.handle_cache_fetch_hit_event(event) ⇒ Object



66
67
68
# File 'lib/rails_request_stats/notification_subscribers.rb', line 66

def self.handle_cache_fetch_hit_event(event)
  @cache_hit_count += 1
end

.handle_cache_read_event(event) ⇒ Object



61
62
63
64
# File 'lib/rails_request_stats/notification_subscribers.rb', line 61

def self.handle_cache_read_event(event)
  @cache_read_count += 1
  @cache_hit_count += 1 if event.fetch(:hit, false)
end

.handle_process_action_event(event) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rails_request_stats/notification_subscribers.rb', line 86

def self.handle_process_action_event(event)
  @after_object_space = ObjectSpace.count_objects(@after_object_space)

  GC.enable

  request_key = { action: event[:action], format: event[:format], method: event[:method], path: event[:path] }
  request_stats = @requests[request_key] || RequestStats.new(request_key)
  @requests[request_key] = request_stats.tap do |stats|
    stats.add_cache_stats(@cache_read_count, @cache_hit_count)
    stats.add_database_query_stats(@query_count, @cached_query_count)
    stats.add_object_space_stats(@before_object_space, @after_object_space)
    stats.add_runtime_stats(event[:view_runtime] || 0, event[:db_runtime] || 0)
  end

  print_report(request_stats)
end

.handle_sql_event(event) ⇒ Object



70
71
72
73
74
75
# File 'lib/rails_request_stats/notification_subscribers.rb', line 70

def self.handle_sql_event(event)
  return if event[:name] == SCHEMA_NAME

  @cached_query_count += 1 if event[:name] == CACHE_NAME
  @query_count += 1
end

.handle_start_processing_event(event) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/rails_request_stats/notification_subscribers.rb', line 77

def self.handle_start_processing_event(event)
  reset_counts

  GC.start
  GC.disable

  @before_object_space = ObjectSpace.count_objects(@before_object_space)
end


103
104
105
# File 'lib/rails_request_stats/notification_subscribers.rb', line 103

def self.print_report(request_stats)
  Rails.logger.info { Report.new(request_stats).report_text }
end

.reset_countsObject



16
17
18
19
20
21
22
23
# File 'lib/rails_request_stats/notification_subscribers.rb', line 16

def self.reset_counts
  @query_count = 0
  @cached_query_count = 0
  @cache_read_count = 0
  @cache_hit_count = 0
  @before_object_space = {}
  @after_object_space = {}
end

.reset_requestsObject



26
27
28
# File 'lib/rails_request_stats/notification_subscribers.rb', line 26

def self.reset_requests
  @requests = {}
end