Class: RailsPerformance::SystemMonitor::ResourcesMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_performance/system_monitor/resources_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, role) ⇒ ResourcesMonitor

Returns a new instance of ResourcesMonitor.



8
9
10
11
12
13
14
15
16
17
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 8

def initialize(context, role)
  @context = context
  @role = role
  @mutex = Mutex.new
  @thread = nil

  return unless RailsPerformance._resource_monitor_enabled

  start_monitoring
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 6

def context
  @context
end

#roleObject (readonly)

Returns the value of attribute role.



6
7
8
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 6

def role
  @role
end

Instance Method Details

#monitorsObject



50
51
52
53
54
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 50

def monitors
  @monitors ||= RailsPerformance.system_monitors.map do |class_name|
    RailsPerformance::SystemMonitor.const_get(class_name).new(nil)
  end
end

#payloadObject



44
45
46
47
48
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 44

def payload
  monitors.reduce({}) do |data, monitor|
    data.merge(monitor.key => monitor.measure)
  end
end

#runObject



56
57
58
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 56

def run
  store_data(payload)
end

#server_idObject



75
76
77
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 75

def server_id
  @server_id ||= ENV["RAILS_PERFORMANCE_SERVER_ID"] || `hostname`.strip
end

#start_monitoringObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 19

def start_monitoring
  @mutex.synchronize do
    return if @thread

    @thread = Thread.new do
      loop do
        run
      rescue => e
        ::Rails.logger.error "Monitor error: #{e.message}"
      ensure
        sleep 60
      end
    end
  end
end

#stop_monitoringObject



35
36
37
38
39
40
41
42
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 35

def stop_monitoring
  @mutex.synchronize do
    return unless @thread

    @thread.kill
    @thread = nil
  end
end

#store_data(data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 60

def store_data(data)
  ::Rails.logger.info("Server: #{server_id}, Context: #{context}, Role: #{role}, data: #{data}")

  now = RailsPerformance::Utils.kind_of_now
  now = now.change(sec: 0, usec: 0)
  RailsPerformance::Models::ResourceRecord.new(
    server: server_id,
    context: context,
    role: role,
    datetime: now.strftime(RailsPerformance::FORMAT),
    datetimei: now.to_i,
    json: data
  ).save
end