Commit 697ef021 authored by allison.browne's avatar allison.browne Committed by Ash McKenzie

Add worker that checks prometheus health

Call a health check service for cluster applications
prometheus on monitor group demo projects
parent 7e369ce4
...@@ -131,6 +131,11 @@ module Clusters ...@@ -131,6 +131,11 @@ module Clusters
scope :with_management_project, -> { where.not(management_project: nil) } scope :with_management_project, -> { where.not(management_project: nil) }
scope :for_project_namespace, -> (namespace_id) { joins(:projects).where(projects: { namespace_id: namespace_id }) } scope :for_project_namespace, -> (namespace_id) { joins(:projects).where(projects: { namespace_id: namespace_id }) }
scope :with_application_prometheus, -> { includes(:application_prometheus).joins(:application_prometheus) }
scope :with_project_alert_service_data, -> (project_ids) do
conditions = { projects: { alerts_service: [:data] } }
includes(conditions).joins(conditions).where(projects: { id: project_ids })
end
def self.ancestor_clusters_for_clusterable(clusterable, hierarchy_order: :asc) def self.ancestor_clusters_for_clusterable(clusterable, hierarchy_order: :asc)
return [] if clusterable.is_a?(Instance) return [] if clusterable.is_a?(Instance)
......
# frozen_string_literal: true
module Clusters
module Applications
class CheckPrometheusHealthWorker
include ApplicationWorker
# rubocop:disable Scalability/CronWorkerContext
# This worker does not perform work scoped to a context
include CronjobQueue
# rubocop:enable Scalability/CronWorkerContext
feature_category :incident_management
idempotent!
def perform
demo_project_ids = Gitlab::Monitor::DemoProjects.oids
clusters = Clusters::Cluster.with_application_prometheus
.with_project_alert_service_data(demo_project_ids)
# Move to a seperate worker with scoped context if expanded to do work on customer projects
clusters.each { |cluster| Clusters::Applications::PrometheusHealthCheckService.new(cluster).execute }
end
end
end
end
...@@ -35,6 +35,7 @@ module Gitlab ...@@ -35,6 +35,7 @@ module Gitlab
end end
end end
STAGING_COM_URL = 'https://staging.gitlab.com'
COM_URL = 'https://gitlab.com' COM_URL = 'https://gitlab.com'
APP_DIRS_PATTERN = %r{^/?(app|config|ee|lib|spec|\(\w*\))}.freeze APP_DIRS_PATTERN = %r{^/?(app|config|ee|lib|spec|\(\w*\))}.freeze
SUBDOMAIN_REGEX = %r{\Ahttps://[a-z0-9]+\.gitlab\.com\z}.freeze SUBDOMAIN_REGEX = %r{\Ahttps://[a-z0-9]+\.gitlab\.com\z}.freeze
...@@ -47,6 +48,10 @@ module Gitlab ...@@ -47,6 +48,10 @@ module Gitlab
Gitlab.config.gitlab.url == COM_URL || gl_subdomain? Gitlab.config.gitlab.url == COM_URL || gl_subdomain?
end end
def self.staging?
Gitlab.config.gitlab.url == STAGING_COM_URL
end
def self.canary? def self.canary?
Gitlab::Utils.to_boolean(ENV['CANARY']) Gitlab::Utils.to_boolean(ENV['CANARY'])
end end
......
# frozen_string_literal: true
module Gitlab
module Monitor
module DemoProjects
DOT_COM_IDS = [14986497, 12507547].freeze
STAGING_IDS = [4422333].freeze
def self.oids
if ::Gitlab.com?
DOT_COM_IDS
elsif ::Gitlab.staging?
STAGING_IDS
elsif Rails.env.development? || Rails.env.test?
Project.pluck(:id) # rubocop: disable CodeReuse/ActiveRecord
end || []
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Monitor::DemoProjects do
describe '#oids' do
subject { described_class.oids }
it 'fetches oids when in test env' do
project = create(:project)
expect(subject).to eq([project.id])
end
it 'fetches oids when on gitlab.com' do
expect(Gitlab).to receive(:'com?').and_return(true)
expect(subject).to eq(Gitlab::Monitor::DemoProjects::DOT_COM_IDS)
end
it 'fetches oids when on staging' do
expect(Gitlab).to receive(:staging?).and_return(true)
expect(subject).to eq(Gitlab::Monitor::DemoProjects::STAGING_IDS)
end
it 'falls back on empty array' do
stub_config_setting(url: 'https://helloworld')
expect(Rails).to receive(:env).and_return(
double(development?: false, test?: false)
).twice
expect(subject).to eq([])
end
end
end
...@@ -172,6 +172,41 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do ...@@ -172,6 +172,41 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do
end end
end end
describe '.with_application_prometheus' do
subject { described_class.with_application_prometheus }
let!(:cluster) { create(:cluster) }
context 'cluster has prometheus application' do
let!(:application) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
it { is_expected.to include(cluster) }
end
context 'cluster does not have prometheus application' do
let(:cluster) { create(:cluster) }
it { is_expected.not_to include(cluster) }
end
end
describe '.with_project_alert_service_data' do
subject { described_class.with_project_alert_service_data(project_id) }
let!(:cluster) { create(:cluster, :project) }
let(:project_id) { cluster.first_project.id }
context 'project has alert service data' do
let(:alert_service) { create(:alerts_service, project: cluster.clusterable) }
it { is_expected.to include(cluster) }
end
context 'project has no alert service data' do
it { is_expected.not_to include(cluster) }
end
end
describe '.for_project_namespace' do describe '.for_project_namespace' do
subject { described_class.for_project_namespace(namespace_id) } subject { described_class.for_project_namespace(namespace_id) }
......
# frozen_string_literal: true
require 'spec_helper'
describe Clusters::Applications::CheckPrometheusHealthWorker, '#perform' do
subject { described_class.new.perform }
it 'triggers health service' do
cluster = create(:cluster)
allow(Gitlab::Monitor::DemoProjects).to receive(:oids)
allow(Clusters::Cluster).to receive(:with_application_prometheus).and_return(double(with_project_alert_service_data: [cluster]))
service_instance = instance_double(Clusters::Applications::PrometheusHealthCheckService)
expect(Clusters::Applications::PrometheusHealthCheckService).to receive(:new).with(cluster).and_return(service_instance)
expect(service_instance).to receive(:execute)
subject
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment