summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-26 00:07:58 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-26 00:07:58 +0000
commit101c30f4dffc624af3cbe3f12edc2cfee570a5f3 (patch)
treece7c63b2d73a42ad41d004020410871968974537 /app
parente1549c75843d235607aebac9ed64f2fd10e549ed (diff)
downloadgitlab-ce-101c30f4dffc624af3cbe3f12edc2cfee570a5f3.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app')
-rw-r--r--app/models/merge_request.rb2
-rw-r--r--app/services/clusters/applications/prometheus_health_check_service.rb85
2 files changed, 86 insertions, 1 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 7934b0f8f59..2a6f498269c 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -189,7 +189,7 @@ class MergeRequest < ApplicationRecord
end
# rubocop: disable CodeReuse/ServiceClass
- after_transition unchecked: :cannot_be_merged do |merge_request, transition|
+ after_transition [:unchecked, :checking] => :cannot_be_merged do |merge_request, transition|
if merge_request.notify_conflict?
NotificationService.new.merge_request_unmergeable(merge_request)
TodoService.new.merge_request_became_unmergeable(merge_request)
diff --git a/app/services/clusters/applications/prometheus_health_check_service.rb b/app/services/clusters/applications/prometheus_health_check_service.rb
new file mode 100644
index 00000000000..e609d9f0b7b
--- /dev/null
+++ b/app/services/clusters/applications/prometheus_health_check_service.rb
@@ -0,0 +1,85 @@
+# frozen_string_literal: true
+
+module Clusters
+ module Applications
+ class PrometheusHealthCheckService
+ include Gitlab::Utils::StrongMemoize
+ include Gitlab::Routing
+
+ def initialize(cluster)
+ @cluster = cluster
+ @logger = Gitlab::AppJsonLogger.build
+ end
+
+ def execute
+ raise 'Invalid cluster type. Only project types are allowed.' unless @cluster.project_type?
+
+ return unless prometheus_application.installed?
+
+ project = @cluster.clusterable
+
+ @logger.info(
+ message: 'Prometheus health check',
+ cluster_id: @cluster.id,
+ newly_unhealthy: became_unhealthy?,
+ currently_healthy: currently_healthy?,
+ was_healthy: was_healthy?
+ )
+
+ send_notification(project) if became_unhealthy?
+
+ prometheus_application.update_columns(healthy: currently_healthy?) if health_changed?
+ end
+
+ private
+
+ def prometheus_application
+ strong_memoize(:prometheus_application) do
+ @cluster.application_prometheus
+ end
+ end
+
+ def currently_healthy?
+ strong_memoize(:currently_healthy) do
+ prometheus_application.prometheus_client.healthy?
+ end
+ end
+
+ def became_unhealthy?
+ strong_memoize(:became_unhealthy) do
+ (was_healthy? || was_healthy?.nil?) && !currently_healthy?
+ end
+ end
+
+ def was_healthy?
+ strong_memoize(:was_healthy) do
+ prometheus_application.healthy
+ end
+ end
+
+ def health_changed?
+ was_healthy? != currently_healthy?
+ end
+
+ def send_notification(project)
+ notification_payload = build_notification_payload(project)
+ token = project.alerts_service.data.token
+ Projects::Alerting::NotifyService.new(project, nil, notification_payload).execute(token)
+ @logger.info(message: 'Successfully notified of Prometheus newly unhealthy', cluster_id: @cluster.id, project_id: project.id)
+ end
+
+ def build_notification_payload(project)
+ cluster_path = namespace_project_cluster_path(
+ project_id: project.path,
+ namespace_id: project.namespace.path,
+ id: @cluster.id
+ )
+
+ {
+ title: "Prometheus is Unhealthy. Cluster Name: #{@cluster.name}",
+ description: "Prometheus is unhealthy for the cluster: [#{@cluster.name}](#{cluster_path}) attached to project #{project.name}."
+ }
+ end
+ end
+ end
+end