diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 15:09:29 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 15:09:29 +0000 |
commit | 5372e109c0660e4670aa987568a51082beca1b3c (patch) | |
tree | 76f8f1178d5f304f0aea8c0c610729f695c9e18e /app | |
parent | 403678e00406edc8094f087ec70e00aa29e49bef (diff) | |
download | gitlab-ce-5372e109c0660e4670aa987568a51082beca1b3c.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app')
-rw-r--r-- | app/models/concerns/prometheus_adapter.rb | 2 | ||||
-rw-r--r-- | app/models/project_services/chat_notification_service.rb | 5 | ||||
-rw-r--r-- | app/models/project_services/prometheus_service.rb | 8 | ||||
-rw-r--r-- | app/services/prometheus/create_default_alerts_service.rb | 88 | ||||
-rw-r--r-- | app/workers/all_queues.yml | 7 | ||||
-rw-r--r-- | app/workers/prometheus/create_default_alerts_worker.rb | 27 |
6 files changed, 133 insertions, 4 deletions
diff --git a/app/models/concerns/prometheus_adapter.rb b/app/models/concerns/prometheus_adapter.rb index 2c171eecbd5..abc41a1c476 100644 --- a/app/models/concerns/prometheus_adapter.rb +++ b/app/models/concerns/prometheus_adapter.rb @@ -5,8 +5,6 @@ module PrometheusAdapter included do include ReactiveCaching - # We can't prepend outside of this model due to the use of `included`, so this must stay here. - prepend_if_ee('EE::PrometheusAdapter') # rubocop: disable Cop/InjectEnterpriseEditionModule self.reactive_cache_lease_timeout = 30.seconds self.reactive_cache_refresh_interval = 30.seconds diff --git a/app/models/project_services/chat_notification_service.rb b/app/models/project_services/chat_notification_service.rb index 7bd011101dd..1ec983223f3 100644 --- a/app/models/project_services/chat_notification_service.rb +++ b/app/models/project_services/chat_notification_service.rb @@ -84,10 +84,11 @@ class ChatNotificationService < Service event_type = data[:event_type] || object_kind - channel_names = get_channel_field(event_type).presence || channel + channel_names = get_channel_field(event_type).presence || channel.presence + channels = channel_names&.split(',')&.map(&:strip) opts = {} - opts[:channel] = channel_names.split(',').map(&:strip) if channel_names + opts[:channel] = channels if channels.present? opts[:username] = username if username return false unless notify(message, opts) diff --git a/app/models/project_services/prometheus_service.rb b/app/models/project_services/prometheus_service.rb index 17bc29ebdcf..30dfcc11417 100644 --- a/app/models/project_services/prometheus_service.rb +++ b/app/models/project_services/prometheus_service.rb @@ -24,6 +24,8 @@ class PrometheusService < MonitoringService after_commit :track_events + after_create_commit :create_default_alerts + def initialize_properties if properties.nil? self.properties = {} @@ -147,4 +149,10 @@ class PrometheusService < MonitoringService def disabled_manual_prometheus? manual_configuration_changed? && !manual_configuration? end + + def create_default_alerts + return unless project_id + + Prometheus::CreateDefaultAlertsWorker.perform_async(project_id: project_id) + end end 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 diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index 3ee8901b23b..5e3ea162d20 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -1256,6 +1256,13 @@ :resource_boundary: :unknown :weight: 1 :idempotent: +- :name: prometheus_create_default_alerts + :feature_category: :incident_management + :has_external_dependencies: + :urgency: :high + :resource_boundary: :unknown + :weight: 1 + :idempotent: true - :name: propagate_service_template :feature_category: :source_code_management :has_external_dependencies: diff --git a/app/workers/prometheus/create_default_alerts_worker.rb b/app/workers/prometheus/create_default_alerts_worker.rb new file mode 100644 index 00000000000..2c4fefa9ece --- /dev/null +++ b/app/workers/prometheus/create_default_alerts_worker.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Prometheus + class CreateDefaultAlertsWorker + include ApplicationWorker + + feature_category :incident_management + urgency :high + idempotent! + + def perform(project_id) + project = Project.find_by_id(project_id) + + return unless project + + result = Prometheus::CreateDefaultAlertsService.new(project: project).execute + + log_info(result.message) if result.error? + end + + private + + def log_info(message) + logger.info(structured_payload(message: message)) + end + end +end |