debian-mirror-gitlab/app/controllers/projects/prometheus/alerts_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

152 lines
3.7 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# 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]
2021-02-22 17:27:13 +05:30
feature_category :incident_management
2021-01-03 14:25:43 +05:30
2020-04-22 19:07:51 +05:30
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)
2021-12-11 22:18:48 +05:30
if result.success?
render json: AlertManagement::AlertSerializer.new.represent(result.payload[:alerts]), code: result.http_status
else
head result.http_status
end
2020-04-22 19:07:51 +05:30
end
def create
@alert = create_service.execute
if @alert.persisted?
schedule_prometheus_update!
render json: serialize_as_json(@alert)
else
2020-10-24 23:57:45 +05:30
head :bad_request
2020-04-22 19:07:51 +05:30
end
end
def update
if update_service.execute(alert)
schedule_prometheus_update!
render json: serialize_as_json(alert)
else
2020-10-24 23:57:45 +05:30
head :bad_request
2020-04-22 19:07:51 +05:30
end
end
def destroy
if destroy_service.execute(alert)
schedule_prometheus_update!
head :ok
else
2020-10-24 23:57:45 +05:30
head :bad_request
2020-04-22 19:07:51 +05:30
end
end
private
def alerts_params
2020-10-24 23:57:45 +05:30
params.permit(:operator, :threshold, :environment_id, :prometheus_metric_id, :runbook_url)
2020-04-22 19:07:51 +05:30
end
def notify_service
Projects::Prometheus::Alerts::NotifyService
2021-02-22 17:27:13 +05:30
.new(project, params.permit!)
2020-04-22 19:07:51 +05:30
end
def create_service
Projects::Prometheus::Alerts::CreateService
2022-01-26 12:08:38 +05:30
.new(project: project, current_user: current_user, params: alerts_params)
2020-04-22 19:07:51 +05:30
end
def update_service
Projects::Prometheus::Alerts::UpdateService
2022-01-26 12:08:38 +05:30
.new(project: project, current_user: current_user, params: alerts_params)
2020-04-22 19:07:51 +05:30
end
def destroy_service
Projects::Prometheus::Alerts::DestroyService
2022-01-26 12:08:38 +05:30
.new(project: project, current_user: current_user, params: nil)
2020-04-22 19:07:51 +05:30
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