class ProcessMonitor
Monitors process metrics and logs them periodically.
Uses the process-metrics gem to capture CPU and memory metrics for a process tree.
Unlike class Async::Container::Supervisor::MemoryMonitor, this monitor captures metrics for the entire process tree
by tracking the parent process ID (ppid), which is more efficient than tracking
individual processes.
Definitions
def initialize(interval: 60, ppid: nil)
Create a new process monitor.
Signature
-
parameter
intervalInteger The interval in seconds at which to log process metrics.
-
parameter
ppidInteger The parent process ID to monitor. If nil, uses the current process to capture its children.
Implementation
def initialize(interval: 60, ppid: nil)
@interval = interval
@ppid = ppid || Process.ppid
end
attr :ppid
Signature
-
attribute
Integer The parent process ID being monitored.
def register(connection)
Register a connection with the process monitor.
This is provided for consistency with class Async::Container::Supervisor::MemoryMonitor, but since we monitor
the entire process tree via ppid, we don't need to track individual connections.
Signature
-
parameter
connectionConnection The connection to register.
Implementation
def register(connection)
Console.debug(self, "Connection registered.", connection: connection, state: connection.state)
end
def remove(connection)
Remove a connection from the process monitor.
This is provided for consistency with class Async::Container::Supervisor::MemoryMonitor, but since we monitor
the entire process tree via ppid, we don't need to track individual connections.
Signature
-
parameter
connectionConnection The connection to remove.
Implementation
def remove(connection)
Console.debug(self, "Connection removed.", connection: connection, state: connection.state)
end
def metrics
Capture current process metrics for the entire process tree.
Signature
-
returns
Hash A hash mapping process IDs to their metrics.
Implementation
def metrics
Process::Metrics::General.capture(ppid: @ppid)
end
def status(call)
Dump the current status of the process monitor.
Signature
-
parameter
callConnection::Call The call to respond to.
Implementation
def status(call)
metrics = self.metrics
call.push(process_monitor: {ppid: @ppid, metrics: metrics})
end
def run
Run the process monitor.
Periodically captures and logs process metrics for the entire process tree.
Signature
-
returns
Async::Task The task that is running the process monitor.
Implementation
def run
Async do
while true
metrics = self.metrics
# Log each process individually for better searchability in log platforms:
metrics.each do |process_id, general|
Console.info(self, "Process metrics captured.", general: general)
end
sleep(@interval)
end
end
end