Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-08 18:09:29 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-08 18:09:29 +0300
commit5372e109c0660e4670aa987568a51082beca1b3c (patch)
tree76f8f1178d5f304f0aea8c0c610729f695c9e18e /app/services
parent403678e00406edc8094f087ec70e00aa29e49bef (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r--app/services/prometheus/create_default_alerts_service.rb88
1 files changed, 88 insertions, 0 deletions
diff --git a/app/services/prometheus/create_default_alerts_service.rb b/app/services/prometheus/create_default_alerts_service.rb
new file mode 100644
index 00000000000..3eb5ad7711a
--- /dev/null
+++ b/app/services/prometheus/create_default_alerts_service.rb
@@ -0,0 +1,88 @@
+# frozen_string_literal: true
+
+module Prometheus
+ class CreateDefaultAlertsService < BaseService
+ include Gitlab::Utils::StrongMemoize
+
+ attr_reader :project
+
+ DEFAULT_ALERTS = [
+ {
+ identifier: 'response_metrics_nginx_ingress_16_http_error_rate',
+ operator: 'gt',
+ threshold: 0.1
+ },
+ {
+ identifier: 'response_metrics_nginx_ingress_http_error_rate',
+ operator: 'gt',
+ threshold: 0.1
+ }
+ ].freeze
+
+ def initialize(project:)
+ @project = project
+ end
+
+ def execute
+ return ServiceResponse.error(message: 'Invalid project') unless project
+ return ServiceResponse.error(message: 'Invalid environment') unless environment
+
+ create_alerts
+
+ ServiceResponse.success
+ end
+
+ private
+
+ def create_alerts
+ DEFAULT_ALERTS.each do |alert_hash|
+ identifier = alert_hash[:identifier]
+ next if alerts_by_identifier(environment).key?(identifier)
+
+ metric = metrics_by_identifier[identifier]
+ next unless metric
+
+ create_alert(alert: alert_hash, metric: metric)
+ end
+ end
+
+ def metrics_by_identifier
+ strong_memoize(:metrics_by_identifier) do
+ metric_identifiers = DEFAULT_ALERTS.map { |alert| alert[:identifier] }
+
+ PrometheusMetricsFinder
+ .new(identifier: metric_identifiers, common: true)
+ .execute
+ .index_by(&:identifier)
+ end
+ end
+
+ def alerts_by_identifier(environment)
+ strong_memoize(:alerts_by_identifier) do
+ Projects::Prometheus::AlertsFinder
+ .new(project: project, metric: metrics_by_identifier.values, environment: environment)
+ .execute
+ .index_by { |alert| alert.prometheus_metric.identifier }
+ end
+ end
+
+ def environment
+ strong_memoize(:environment) do
+ EnvironmentsFinder.new(project, nil, name: 'production').find.first ||
+ project.environments.first
+ end
+ end
+
+ def create_alert(alert:, metric:)
+ PrometheusAlert.create!(
+ project: project,
+ prometheus_metric: metric,
+ environment: environment,
+ threshold: alert[:threshold],
+ operator: alert[:operator]
+ )
+ rescue ActiveRecord::RecordNotUnique
+ # Ignore duplicate creations although it unlikely to happen
+ end
+ end
+end