Skip to content

Commit e8f39e5

Browse files
authored
Initial metrics for health check failure. (#41)
1 parent 4cc4dc8 commit e8f39e5

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

config/metrics.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
def prepare
7+
require "metrics/provider/async/container"
8+
end

config/sus.rb

+11
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,14 @@
77
include Covered::Sus
88

99
ENV["CONSOLE_LEVEL"] ||= "fatal"
10+
ENV["METRICS_BACKEND"] ||= "metrics/backend/test"
11+
12+
def prepare_instrumentation!
13+
require "metrics"
14+
end
15+
16+
def before_tests(...)
17+
prepare_instrumentation!
18+
19+
super
20+
end

examples/health_check/test.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
# Copyright, 2022, by Anton Sozontov.
66
# Copyright, 2024, by Samuel Williams.
77

8-
require "../../lib/async/container/controller"
8+
require "metrics"
9+
require_relative "../../lib/async/container/controller"
910

1011
NAMES = [
1112
"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow", "Nougat", "Oreo", "Pie", "Apple Tart"

gems.rb

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
gem "decode"
2222
gem "rubocop"
2323

24+
gem "metrics"
25+
2426
gem "bake-test"
2527
gem "bake-test-external"
2628
end

lib/async/container/generic.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ def stop(timeout = true)
145145
@running = true
146146
end
147147

148+
protected def health_check_failed!(child, age_clock, health_check_timeout)
149+
Console.warn(self, "Child failed health check!", child: child, age: age_clock.total, health_check_timeout: health_check_timeout)
150+
151+
# If the child has failed the health check, we assume the worst and kill it immediately:
152+
child.kill!
153+
end
154+
148155
# Spawn a child instance into the container.
149156
# @parameter name [String] The name of the child instance.
150157
# @parameter restart [Boolean] Whether to restart the child instance if it fails.
@@ -176,9 +183,7 @@ def spawn(name: nil, restart: false, key: nil, health_check_timeout: nil, &block
176183
case message
177184
when :health_check!
178185
if health_check_timeout&.<(age_clock.total)
179-
Console.warn(self, "Child failed health check!", child: child, age: age_clock.total, health_check_timeout: health_check_timeout)
180-
# If the child has failed the health check, we assume the worst and kill it immediately:
181-
child.kill!
186+
health_check_failed!(child, age_clock, health_check_timeout)
182187
end
183188
else
184189
state.update(message)
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
require_relative "container/generic"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
require_relative "../../../../async/container/generic"
7+
require "metrics/provider"
8+
9+
Metrics::Provider(Async::Container::Generic) do
10+
ASYNC_CONTAINER_GENERIC_HEALTH_CHECK_FAILED = Metrics.metric("async.container.generic.health_check_failed", :counter, description: "The number of health checks that failed.")
11+
12+
protected def health_check_failed!(child, age_clock, health_check_timeout)
13+
ASYNC_CONTAINER_GENERIC_HEALTH_CHECK_FAILED.emit(1)
14+
15+
super
16+
end
17+
end

0 commit comments

Comments
 (0)