Skip to content

Initial metrics for health check failure. #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

def prepare
require "metrics/provider/async/container"
end
11 changes: 11 additions & 0 deletions config/sus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@
include Covered::Sus

ENV["CONSOLE_LEVEL"] ||= "fatal"
ENV["METRICS_BACKEND"] ||= "metrics/backend/test"

def prepare_instrumentation!
require "metrics"
end

def before_tests(...)
prepare_instrumentation!

super
end
3 changes: 2 additions & 1 deletion examples/health_check/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# Copyright, 2022, by Anton Sozontov.
# Copyright, 2024, by Samuel Williams.

require "../../lib/async/container/controller"
require "metrics"
require_relative "../../lib/async/container/controller"

NAMES = [
"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow", "Nougat", "Oreo", "Pie", "Apple Tart"
Expand Down
2 changes: 2 additions & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
gem "decode"
gem "rubocop"

gem "metrics"

gem "bake-test"
gem "bake-test-external"
end
11 changes: 8 additions & 3 deletions lib/async/container/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ def stop(timeout = true)
@running = true
end

protected def health_check_failed!(child, age_clock, health_check_timeout)
Console.warn(self, "Child failed health check!", child: child, age: age_clock.total, health_check_timeout: health_check_timeout)

# If the child has failed the health check, we assume the worst and kill it immediately:
child.kill!
end

# Spawn a child instance into the container.
# @parameter name [String] The name of the child instance.
# @parameter restart [Boolean] Whether to restart the child instance if it fails.
Expand Down Expand Up @@ -176,9 +183,7 @@ def spawn(name: nil, restart: false, key: nil, health_check_timeout: nil, &block
case message
when :health_check!
if health_check_timeout&.<(age_clock.total)
Console.warn(self, "Child failed health check!", child: child, age: age_clock.total, health_check_timeout: health_check_timeout)
# If the child has failed the health check, we assume the worst and kill it immediately:
child.kill!
health_check_failed!(child, age_clock, health_check_timeout)
end
else
state.update(message)
Expand Down
6 changes: 6 additions & 0 deletions lib/metrics/provider/async/container.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require_relative "container/generic"
17 changes: 17 additions & 0 deletions lib/metrics/provider/async/container/generic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require_relative "../../../../async/container/generic"
require "metrics/provider"

Metrics::Provider(Async::Container::Generic) do
ASYNC_CONTAINER_GENERIC_HEALTH_CHECK_FAILED = Metrics.metric("async.container.generic.health_check_failed", :counter, description: "The number of health checks that failed.")

protected def health_check_failed!(child, age_clock, health_check_timeout)
ASYNC_CONTAINER_GENERIC_HEALTH_CHECK_FAILED.emit(1)

super
end
end
Loading