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

alerts_controller.rb « prometheus « projects « controllers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f711417f0b045a5e37ececb126fa931566df416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true

module Projects
  module Prometheus
    class AlertsController < Projects::ApplicationController
      include MetricsDashboard

      respond_to :json

      protect_from_forgery except: [:notify]

      skip_before_action :project, only: [:notify]

      prepend_before_action :repository, :project_without_auth, only: [:notify]

      before_action :authorize_read_prometheus_alerts!, except: [:notify]
      before_action :alert, only: [:update, :show, :destroy, :metrics_dashboard]

      feature_category :incident_management

      def index
        render json: serialize_as_json(alerts)
      end

      def show
        render json: serialize_as_json(alert)
      end

      def notify
        token = extract_alert_manager_token(request)
        result = notify_service.execute(token)

        head result.http_status
      end

      def create
        @alert = create_service.execute

        if @alert.persisted?
          schedule_prometheus_update!

          render json: serialize_as_json(@alert)
        else
          head :bad_request
        end
      end

      def update
        if update_service.execute(alert)
          schedule_prometheus_update!

          render json: serialize_as_json(alert)
        else
          head :bad_request
        end
      end

      def destroy
        if destroy_service.execute(alert)
          schedule_prometheus_update!

          head :ok
        else
          head :bad_request
        end
      end

      private

      def alerts_params
        params.permit(:operator, :threshold, :environment_id, :prometheus_metric_id, :runbook_url)
      end

      def notify_service
        Projects::Prometheus::Alerts::NotifyService
          .new(project, current_user, params.permit!)
      end

      def create_service
        Projects::Prometheus::Alerts::CreateService
          .new(project, current_user, alerts_params)
      end

      def update_service
        Projects::Prometheus::Alerts::UpdateService
          .new(project, current_user, alerts_params)
      end

      def destroy_service
        Projects::Prometheus::Alerts::DestroyService
          .new(project, current_user, nil)
      end

      def schedule_prometheus_update!
        ::Clusters::Applications::ScheduleUpdateService.new(application, project).execute
      end

      def serialize_as_json(alert_obj)
        serializer.represent(alert_obj)
      end

      def serializer
        PrometheusAlertSerializer
          .new(project: project, current_user: current_user)
      end

      def alerts
        alerts_finder.execute
      end

      def alert
        @alert ||= alerts_finder(metric: params[:id]).execute.first || render_404
      end

      def alerts_finder(opts = {})
        Projects::Prometheus::AlertsFinder.new({
          project: project,
          environment: params[:environment_id]
        }.reverse_merge(opts))
      end

      def application
        @application ||= alert.environment.cluster_prometheus_adapter
      end

      def extract_alert_manager_token(request)
        Doorkeeper::OAuth::Token.from_bearer_authorization(request)
      end

      def project_without_auth
        @project ||= Project
          .find_by_full_path("#{params[:namespace_id]}/#{params[:project_id]}")
      end

      def prometheus_alerts
        project.prometheus_alerts.for_environment(params[:environment_id])
      end

      def metrics_dashboard_params
        {
          embedded: true,
          prometheus_alert_id: alert.id
        }
      end
    end
  end
end