BigW Consortium Gitlab

health_controller.rb 1.55 KB
Newer Older
1
class HealthController < ActionController::Base
2
  protect_from_forgery with: :exception, except: :storage_check
3
  include RequiresWhitelistedMonitoringClient
4 5 6

  CHECKS = [
    Gitlab::HealthChecks::DbCheck,
7 8 9 10
    Gitlab::HealthChecks::Redis::RedisCheck,
    Gitlab::HealthChecks::Redis::CacheCheck,
    Gitlab::HealthChecks::Redis::QueuesCheck,
    Gitlab::HealthChecks::Redis::SharedStateCheck,
11 12
    Gitlab::HealthChecks::FsShardsCheck,
    Gitlab::HealthChecks::GitalyCheck
13 14 15 16 17 18 19 20 21 22 23 24 25 26
  ].freeze

  def readiness
    results = CHECKS.map { |check| [check.name, check.readiness] }

    render_check_results(results)
  end

  def liveness
    results = CHECKS.map { |check| [check.name, check.liveness] }

    render_check_results(results)
  end

27 28 29 30 31 32 33 34 35
  def storage_check
    results = Gitlab::Git::Storage::Checker.check_all

    render json: {
             check_interval: Gitlab::CurrentSettings.current_application_settings.circuitbreaker_check_interval,
             results: results
           }
  end

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  private

  def render_check_results(results)
    flattened = results.flat_map do |name, result|
      if result.is_a?(Gitlab::HealthChecks::Result)
        [[name, result]]
      else
        result.map { |r| [name, r] }
      end
    end
    success = flattened.all? { |name, r| r.success }

    response = flattened.map do |name, r|
      info = { status: r.success ? 'ok' : 'failed' }
      info['message'] = r.message if r.message
      info[:labels] = r.labels if r.labels
      [name, info]
    end
    render json: response.to_h, status: success ? :ok : :service_unavailable
  end
end