2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
module Projects
|
|
|
|
module Prometheus
|
|
|
|
class MetricsController < Projects::ApplicationController
|
|
|
|
before_action :authorize_admin_project!
|
|
|
|
before_action :require_prometheus_metrics!
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
feature_category :metrics
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def active_common
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
matched_metrics = prometheus_adapter.query(:matched_metrics) || {}
|
|
|
|
|
|
|
|
if matched_metrics.any?
|
|
|
|
render json: matched_metrics
|
|
|
|
else
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def validate_query
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
result = prometheus_adapter.query(:validate, params[:query])
|
|
|
|
|
|
|
|
if result
|
|
|
|
render json: result
|
|
|
|
else
|
|
|
|
head :accepted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@metric = project.prometheus_metrics.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
metrics = ::PrometheusMetricsFinder.new(
|
|
|
|
project: project,
|
|
|
|
ordered: true
|
|
|
|
).execute.to_a
|
|
|
|
|
|
|
|
response = {}
|
|
|
|
if metrics.any?
|
|
|
|
response[:metrics] = ::PrometheusMetricSerializer
|
|
|
|
.new(project: project)
|
|
|
|
.represent(metrics)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@metric = project.prometheus_metrics.create(
|
|
|
|
metrics_params.to_h.symbolize_keys
|
|
|
|
)
|
|
|
|
|
|
|
|
if @metric.persisted?
|
2021-09-30 23:02:18 +05:30
|
|
|
redirect_to edit_project_service_path(project, ::Integrations::Prometheus),
|
2020-04-22 19:07:51 +05:30
|
|
|
notice: _('Metric was successfully added.')
|
|
|
|
else
|
|
|
|
render 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@metric = update_metrics_service(prometheus_metric).execute
|
|
|
|
|
|
|
|
if @metric.persisted?
|
2021-09-30 23:02:18 +05:30
|
|
|
redirect_to edit_project_service_path(project, ::Integrations::Prometheus),
|
2020-04-22 19:07:51 +05:30
|
|
|
notice: _('Metric was successfully updated.')
|
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@metric = prometheus_metric
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
destroy_metrics_service(prometheus_metric).execute
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2021-09-30 23:02:18 +05:30
|
|
|
redirect_to edit_project_service_path(project, ::Integrations::Prometheus), status: :see_other
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def prometheus_adapter
|
2020-03-13 15:44:24 +05:30
|
|
|
@prometheus_adapter ||= ::Gitlab::Prometheus::Adapter.new(project, project.deployment_platform&.cluster).prometheus_adapter
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def require_prometheus_metrics!
|
2018-10-15 14:42:47 +05:30
|
|
|
render_404 unless prometheus_adapter&.can_query?
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
def prometheus_metric
|
|
|
|
@prometheus_metric ||= ::PrometheusMetricsFinder.new(id: params[:id]).execute.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_metrics_service(metric)
|
|
|
|
::Projects::Prometheus::Metrics::UpdateService.new(metric, metrics_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_metrics_service(metric)
|
|
|
|
::Projects::Prometheus::Metrics::DestroyService.new(metric)
|
|
|
|
end
|
|
|
|
|
|
|
|
def metrics_params
|
|
|
|
params.require(:prometheus_metric).permit(:title, :query, :y_label, :unit, :legend, :group)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|