Class: RailsPerformance::Extensions::ResourceMonitor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, role) ⇒ ResourceMonitor

Returns a new instance of ResourceMonitor.



6
7
8
9
10
11
12
13
14
15
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 6

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.



4
5
6
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 4

def context
  @context
end

#roleObject (readonly)

Returns the value of attribute role.



4
5
6
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 4

def role
  @role
end

Instance Method Details

#fetch_disk_usage(path = "/") ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 69

def fetch_disk_usage(path = "/")
  stat = Sys::Filesystem.stat(path)
  {
    available: stat.blocks_available * stat.block_size,
    total: stat.blocks * stat.block_size,
    used: (stat.blocks - stat.blocks_available) * stat.block_size
  }
rescue => e
  ::Rails.logger.error "Error fetching disk space: #{e.message}"
  {available: 0, total: 0, used: 0}
end

#fetch_process_cpu_usageObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 50

def fetch_process_cpu_usage
  load_averages = Sys::CPU.load_avg
  {
    one_min: load_averages[0],
    five_min: load_averages[1],
    fifteen_min: load_averages[2]
  }
rescue => e
  ::Rails.logger.error "Error fetching CPU usage: #{e.message}"
  {one_min: 0.0, five_min: 0.0, fifteen_min: 0.0}
end

#fetch_process_memory_usageObject



62
63
64
65
66
67
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 62

def fetch_process_memory_usage
  GetProcessMem.new.bytes
rescue => e
  ::Rails.logger.error "Error fetching memory usage: #{e.message}"
  0
end

#runObject



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

def run
  cpu = fetch_process_cpu_usage
  memory = fetch_process_memory_usage
  disk = fetch_disk_usage

  store_data({cpu:, memory:, disk:})
end

#server_idObject



96
97
98
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 96

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

#start_monitoringObject



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

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



33
34
35
36
37
38
39
40
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 33

def stop_monitoring
  @mutex.synchronize do
    return unless @thread

    @thread.kill
    @thread = nil
  end
end

#store_data(data) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_performance/extensions/resources_monitor.rb', line 81

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

  now = RailsPerformance::Utils.time
  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