Commit 90c0d701 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'ab-monitor-demo-environments-2' into 'master'

Add Scheduled Job for Monitoring Monitor Group Demo Environments

See merge request gitlab-org/gitlab!27360
parents 743c531e c10e6187
...@@ -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)
......
...@@ -627,6 +627,14 @@ ...@@ -627,6 +627,14 @@
:weight: 1 :weight: 1
:idempotent: :idempotent:
:tags: [] :tags: []
- :name: incident_management:clusters_applications_check_prometheus_health
:feature_category: :incident_management
:has_external_dependencies: true
:urgency: :low
:resource_boundary: :unknown
:weight: 2
:idempotent: true
:tags: []
- :name: incident_management:incident_management_process_alert - :name: incident_management:incident_management_process_alert
:feature_category: :incident_management :feature_category: :incident_management
:has_external_dependencies: :has_external_dependencies:
......
# 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
queue_namespace :incident_management
feature_category :incident_management
urgency :low
idempotent!
worker_has_external_dependencies!
def perform
demo_project_ids = Gitlab::Monitor::DemoProjects.primary_keys
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
---
title: Add Scheduled Job for Monitoring Monitor Group Demo Environments
merge_request: 27360
author:
type: added
...@@ -36,6 +36,7 @@ module Gitlab ...@@ -36,6 +36,7 @@ module Gitlab
end end
COM_URL = 'https://gitlab.com' COM_URL = 'https://gitlab.com'
STAGING_COM_URL = 'https://staging.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
VERSION = File.read(root.join("VERSION")).strip.freeze VERSION = File.read(root.join("VERSION")).strip.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
...@@ -75,6 +80,10 @@ module Gitlab ...@@ -75,6 +80,10 @@ module Gitlab
Rails.env.development? || com? Rails.env.development? || com?
end end
def self.dev_or_test_env?
Rails.env.development? || Rails.env.test?
end
def self.ee? def self.ee?
@is_ee ||= @is_ee ||=
# We use this method when the Rails environment is not loaded. This # We use this method when the Rails environment is not loaded. This
......
# frozen_string_literal: true
module Gitlab
module Monitor
# See Demo Project documentation
# https://about.gitlab.com/handbook/engineering/development/ops/monitor/#demo-environments
module DemoProjects
# [https://gitlab.com/gitlab-org/monitor/tanuki-inc, https://gitlab.com/gitlab-org/monitor/monitor-sandbox]
DOT_COM_IDS = [14986497, 12507547].freeze
# [https://staging.gitlab.com/gitlab-org/monitor/monitor-sandbox]
STAGING_IDS = [4422333].freeze
def self.primary_keys
# .com? returns true for staging
if ::Gitlab.com? && !::Gitlab.staging?
DOT_COM_IDS
elsif ::Gitlab.staging?
STAGING_IDS
elsif ::Gitlab.dev_or_test_env?
Project.limit(100).pluck(:id) # rubocop: disable CodeReuse/ActiveRecord
else
[]
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Monitor::DemoProjects do
describe '#primary_keys' do
subject { described_class.primary_keys }
it 'fetches primary_keys when on gitlab.com' do
allow(Gitlab).to receive(:com?).and_return(true)
allow(Gitlab).to receive(:staging?).and_return(false)
expect(subject).to eq(Gitlab::Monitor::DemoProjects::DOT_COM_IDS)
end
it 'fetches primary_keys when on staging' do
allow(Gitlab).to receive(:com?).and_return(true)
allow(Gitlab).to receive(:staging?).and_return(true)
expect(subject).to eq(Gitlab::Monitor::DemoProjects::STAGING_IDS)
end
it 'fetches all keys when in the dev or test env' do
project = create(:project)
allow(Gitlab).to receive(:dev_or_test_env?).and_return(true)
expect(subject).to eq([project.id])
end
it 'falls back on empty array' do
stub_config_setting(url: 'https://helloworld')
allow(Gitlab).to receive(:dev_or_test_env?).and_return(false)
expect(subject).to eq([])
end
end
end
...@@ -96,6 +96,28 @@ describe Gitlab do ...@@ -96,6 +96,28 @@ describe Gitlab do
end end
end end
describe '.staging?' do
subject { described_class.staging? }
it 'is false when on GitLab.com' do
stub_config_setting(url: 'https://gitlab.com')
expect(subject).to eq false
end
it 'is true when on staging' do
stub_config_setting(url: 'https://staging.gitlab.com')
expect(subject).to eq true
end
it 'is false when not on staging' do
stub_config_setting(url: 'https://example.gitlab.com')
expect(subject).to eq false
end
end
describe '.canary?' do describe '.canary?' do
it 'is true when CANARY env var is set to true' do it 'is true when CANARY env var is set to true' do
stub_env('CANARY', '1') stub_env('CANARY', '1')
...@@ -186,6 +208,26 @@ describe Gitlab do ...@@ -186,6 +208,26 @@ describe Gitlab do
end end
end end
describe '.dev_or_test_env?' do
subject { described_class.dev_or_test_env? }
it 'is true when test env' do
expect(subject).to eq true
end
it 'is true when dev env' do
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('development'))
expect(subject).to eq true
end
it 'is false when env is not dev or test' do
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('production'))
expect(subject).to eq false
end
end
describe '.ee?' do describe '.ee?' do
before do before do
stub_env('FOSS_ONLY', nil) # Make sure the ENV is clean stub_env('FOSS_ONLY', nil) # Make sure the ENV is clean
......
...@@ -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!(:alerts_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(:primary_keys)
allow(Clusters::Cluster).to receive_message_chain(:with_application_prometheus, :with_project_alert_service_data).and_return([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