Commit a2920682 authored by syasonik's avatar syasonik

Add inital dashboard endpoint support

parent 6735f4c7
......@@ -12,6 +12,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController
before_action :expire_etag_cache, only: [:index]
before_action only: [:metrics, :additional_metrics] do
push_frontend_feature_flag(:metrics_time_window)
push_frontend_feature_flag(:environment_metrics_use_prometheus_endpoint)
push_frontend_feature_flag(:environment_metrics_show_multiple_dashboards)
end
def index
......@@ -156,6 +158,18 @@ class Projects::EnvironmentsController < Projects::ApplicationController
end
end
def metrics_dashboard
access_denied! unless Feature.enabled?(:environment_metrics_use_prometheus_endpoint, project)
respond_to do |format|
format.json do
dashboard = MetricsDashboardService.new(@project).find(params[:dashboard])
render json: dashboard, status: :ok
end
end
end
def search
respond_to do |format|
format.json do
......
# frozen_string_literal: true
# Searches a projects repository for a metrics dashboard and formats the output.
# Expects any custom dashboards will be located in `.gitlab/dashboards`
class MetricsDashboardService
DASHBOARD_ROOT = ".gitlab/dashboards"
DASHBOARD_EXTENSION = '.yml'
SYSTEM_DASHBOARD_NAME = 'system_dashboard'
SYSTEM_DASHBOARD_ROOT = "config/prometheus"
SYSTEM_DASHBOARD_PATH = Rails.root.join(SYSTEM_DASHBOARD_ROOT, "#{SYSTEM_DASHBOARD_NAME}#{DASHBOARD_EXTENSION}")
def initialize(project)
@project = project
end
# Returns a DB-supplemented json representation of a dashboard config file.
#
# param: dashboard_name [String] Filename of dashboard w/o an extension.
# If not provided, the system dashboard will be returned.
def find(dashboard_name = nil)
unless Feature.enabled?(:environment_metrics_show_multiple_dashboards, @project)
return process_dashboard(system_dashboard)
end
dashboard = Rails.cache.fetch(cache_key(dashboard_name)) do
dashboard_name ? project_dashboard(dashboard) : system_dashboard
end
process_dashboard(dashboard)
end
private
# Returns the base metrics shipped with every GitLab service.
def system_dashboard
YAML.load_file(SYSTEM_DASHBOARD_PATH)
end
# Searches the project repo for a custom-defined dashboard.
def project_dashboard(dashboard_name)
Gitlab::Template::Finders::RepoTemplateFinder.new(
project,
DASHBOARD_ROOT,
DASHBOARD_EXTENSION
).find(dashboard_name).read
end
def cache_key(dashboard_name)
return "metrics_dashboard_#{SYSTEM_DASHBOARD_NAME}" unless dashboard_name
"project_#{@project.id}_metrics_dashboard_#{dashboard_name}"
end
# TODO: "Processing" the dashboard needs to include several steps such as
# inserting metric ids and alert information.
def process_dashboard(dashboard)
dashboard.to_json
end
end
This diff is collapsed.
......@@ -218,6 +218,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
get :terminal
get :metrics
get :additional_metrics
get :metrics_dashboard
get '/terminal.ws/authorize', to: 'environments#terminal_websocket_authorize', constraints: { format: nil }
get '/prometheus/api/v1/*proxy_path', to: 'environments/prometheus_api#proxy'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment