diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-12-02 18:09:19 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-12-02 18:09:19 +0300 |
commit | 20ddcb963c756f5c7df26046adb01a8e325a26cd (patch) | |
tree | 6bd96e9d61c206cfe4a912270edbe797d51dc6c9 /app/services/clusters | |
parent | f62c9f693fbe0d9bfac43cbe24b86af3c35f6a17 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/clusters')
-rw-r--r-- | app/services/clusters/applications/prometheus_config_service.rb | 155 |
1 files changed, 0 insertions, 155 deletions
diff --git a/app/services/clusters/applications/prometheus_config_service.rb b/app/services/clusters/applications/prometheus_config_service.rb deleted file mode 100644 index d39d63c874f..00000000000 --- a/app/services/clusters/applications/prometheus_config_service.rb +++ /dev/null @@ -1,155 +0,0 @@ -# frozen_string_literal: true - -module Clusters - module Applications - class PrometheusConfigService - def initialize(project, cluster, app) - @project = project - @cluster = cluster - @app = app - end - - def execute(config = {}) - if has_alerts? - generate_alert_manager(config) - else - reset_alert_manager(config) - end - end - - private - - attr_reader :project, :cluster, :app - - def reset_alert_manager(config) - config = set_alert_manager_enabled(config, false) - config.delete('alertmanagerFiles') - config['serverFiles'] ||= {} - config['serverFiles']['alerts'] = {} - - config - end - - def generate_alert_manager(config) - config = set_alert_manager_enabled(config, true) - config = set_alert_manager_files(config) - - set_alert_manager_groups(config) - end - - def set_alert_manager_enabled(config, enabled) - config['alertmanager'] ||= {} - config['alertmanager']['enabled'] = enabled - - config - end - - def set_alert_manager_files(config) - config['alertmanagerFiles'] = { - 'alertmanager.yml' => { - 'receivers' => alert_manager_receivers_params, - 'route' => alert_manager_route_params - } - } - - config - end - - def set_alert_manager_groups(config) - config['serverFiles'] ||= {} - config['serverFiles']['alerts'] ||= {} - config['serverFiles']['alerts']['groups'] ||= [] - - environments_with_alerts.each do |env_name, alerts| - index = config['serverFiles']['alerts']['groups'].find_index do |group| - group['name'] == env_name - end - - if index - config['serverFiles']['alerts']['groups'][index]['rules'] = alerts - else - config['serverFiles']['alerts']['groups'] << { - 'name' => env_name, - 'rules' => alerts - } - end - end - - config - end - - def alert_manager_receivers_params - [ - { - 'name' => 'gitlab', - 'webhook_configs' => [ - { - 'url' => notify_url, - 'send_resolved' => true, - 'http_config' => { - 'bearer_token' => alert_manager_token - } - } - ] - } - ] - end - - def alert_manager_token - app.alert_manager_token - end - - def alert_manager_route_params - { - 'receiver' => 'gitlab', - 'group_wait' => '30s', - 'group_interval' => '5m', - 'repeat_interval' => '4h' - } - end - - def notify_url - ::Gitlab::Routing.url_helpers - .notify_project_prometheus_alerts_url(project, format: :json) - end - - def has_alerts? - environments_with_alerts.values.flatten(1).any? - end - - def environments_with_alerts - @environments_with_alerts ||= - environments.each_with_object({}) do |environment, hash| - name = rule_name(environment) - hash[name] = alerts(environment) - end - end - - def rule_name(environment) - "#{environment.name}.rules" - end - - def alerts(environment) - alerts = Projects::Prometheus::AlertsFinder - .new(environment: environment) - .execute - - alerts.map do |alert| - hash = alert.to_param - hash['expr'] = substitute_query_variables(hash['expr'], environment) - hash - end - end - - def substitute_query_variables(query, environment) - result = ::Prometheus::ProxyVariableSubstitutionService.new(environment, query: query).execute - - result[:params][:query] - end - - def environments - project.environments_for_scope(cluster.environment_scope) - end - end - end -end |