Skip to content

Server: Health Checking

Shaun McCormick edited this page Aug 14, 2022 · 1 revision

Gruf 2.16+ comes with a built-in gruf controller to serve the default gRPC health check from. The health check will simply respond with operational whenever called; you can customize its response.

Enabling the Health Check

The health check is disabled by default; you can enable it via either:

  • Setting GRUF_HEALTH_CHECK_ENABLED=1 in ENV
  • Using --health-check when calling the gruf command-line binstub
  • In your Gruf.configure block, set health_check_enabled to true, like so:
Gruf.configure do |c|
  c.health_check_enabled = true
end

Customizing the Health Check

Gruf provides a way to customize the health check for the process if enabled, via a health_check_hook configuration argument that can be set a Proc. This allows you to test more than just the gRPC server, but also other dependencies you may want to indicate status on.

The health check hook must return a ::Grpc::Health::V1::HealthCheckResponse object (see here for details on the protobuf object).

For example, you could check MySQL connectivity like so:

Gruf.configure do |config|
  config.health_check_hook = lambda do |_request, _error|
    db_connected = ::ActiveRecord::Base.connected?
    ::Grpc::Health::V1::HealthCheckResponse.new(
      status: db_connected ? ::Grpc::Health::V1::HealthCheckResponse::ServingStatus::SERVING : ::Grpc::Health::V1::HealthCheckResponse::ServingStatus::NOT_SERVING
    )
  end
end