Skip to content

Commit e6f9d47

Browse files
Add ProcessMonitor#emit(metrics).
1 parent f519601 commit e6f9d47

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

lib/async/service/supervisor/process_monitor.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,20 @@ def as_json
6767
{ppid: @ppid, metrics: self.metrics}
6868
end
6969

70-
# Run one iteration of the process monitor.
71-
def run_once
72-
metrics = self.metrics
73-
70+
# Emit the process metrics.
71+
#
72+
# @parameter metrics [Hash] The process metrics to emit.
73+
def emit(metrics)
7474
# Log each process individually for better searchability in log platforms:
7575
metrics.each do |process_id, general|
7676
Console.info(self, "Process metrics captured.", general: general)
7777
end
7878
end
79+
80+
# Run one iteration of the process monitor.
81+
def run_once
82+
self.emit(self.metrics)
83+
end
7984
end
8085
end
8186
end

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Add `ProcessMonitor#emit(metrics)` as an override point for subclasses to consume captured process metrics (e.g. emitting StatsD gauges).
6+
37
## v0.15.0
48

59
- Improve robustness and error handling of default monitors and server loop, ensuring that monitor failures either completely crash the server or retry appropriately, rather than leaving the server in a broken state.

test/async/service/process_monitor.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,61 @@
1818
it "defaults to the current parent process id" do
1919
expect(monitor.ppid).to be == Process.ppid
2020
end
21+
22+
it "can be set explicitly" do
23+
monitor = subject.new(ppid: 1)
24+
expect(monitor.ppid).to be == 1
25+
end
26+
end
27+
28+
with "#metrics" do
29+
it "returns a hash of process metrics" do
30+
metrics = monitor.metrics
31+
expect(metrics).to be_a(Hash)
32+
expect(metrics).not.to be(:empty?)
33+
end
34+
end
35+
36+
with "#emit" do
37+
it "logs each process" do
38+
metrics = {
39+
1234 => {process_id: 1234, command: "ruby"},
40+
5678 => {process_id: 5678, command: "worker"},
41+
}
42+
43+
monitor.emit(metrics)
44+
45+
expect(console_capture.records.length).to be == 2
46+
end
47+
end
48+
49+
with "#run_once" do
50+
it "captures metrics and passes them to emit" do
51+
emitted = nil
52+
monitor.define_singleton_method(:emit) {|metrics| emitted = metrics}
53+
54+
monitor.run_once
55+
56+
expect(emitted).to be_a(Hash)
57+
expect(emitted).not.to be(:empty?)
58+
end
59+
end
60+
61+
it "supports subclass overriding emit" do
62+
received = nil
63+
64+
subclass = Class.new(subject) do
65+
define_method(:emit) do |metrics|
66+
received = metrics
67+
super(metrics)
68+
end
69+
end
70+
71+
sub_monitor = subclass.new(interval: 1)
72+
sub_monitor.run_once
73+
74+
expect(received).to be_a(Hash)
75+
expect(received).not.to be(:empty?)
2176
end
2277

2378
with "#run" do

0 commit comments

Comments
 (0)