BigW Consortium Gitlab

simple_abstract_check.rb 1.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
module Gitlab
  module HealthChecks
    module SimpleAbstractCheck
      include BaseAbstractCheck

      def readiness
        check_result = check
        if is_successful?(check_result)
          HealthChecks::Result.new(true)
        elsif check_result.is_a?(Timeout::Error)
          HealthChecks::Result.new(false, "#{human_name} check timed out")
        else
          HealthChecks::Result.new(false, "unexpected #{human_name} check result: #{check_result}")
        end
      end

      def metrics
        with_timing method(:check) do |result, elapsed|
          Rails.logger.error("#{human_name} check returned unexpected result #{result}") unless is_successful?(result)
          [
            metric("#{metric_prefix}_timeout", result.is_a?(Timeout::Error) ? 1 : 0),
            metric("#{metric_prefix}_success", is_successful?(result) ? 1 : 0),
            metric("#{metric_prefix}_latency", elapsed)
          ]
        end
      end

      private

      def metric_prefix
        raise NotImplementedError
      end

      def is_successful?(result)
        raise NotImplementedError
      end

      def check
        raise NotImplementedError
      end
    end
  end
end