Commit 83d8af24 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'prom-api-1' into 'master'

Update server API to enable switch prometheus endpoint

See merge request gitlab-org/gitlab-ce!29186
parents df000f03 ecdc50b1
......@@ -13,7 +13,7 @@ class Projects::Environments::PrometheusApiController < Projects::ApplicationCon
).execute
if result.nil?
return render status: :accepted, json: {
return render status: :no_content, json: {
status: _('processing'),
message: _('Not ready yet. Try again later.')
}
......
......@@ -330,7 +330,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
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'
get '/prometheus/api/v1/*proxy_path', to: 'environments/prometheus_api#proxy', as: :prometheus_api
end
collection do
......
......@@ -6,13 +6,13 @@ module Gitlab
module Metrics
module Dashboard
class BaseService < ::BaseService
DASHBOARD_LAYOUT_ERROR = Gitlab::Metrics::Dashboard::Stages::BaseStage::DashboardLayoutError
PROCESSING_ERROR = Gitlab::Metrics::Dashboard::Stages::BaseStage::DashboardProcessingError
def get_dashboard
return error("#{dashboard_path} could not be found.", :not_found) unless path_available?
success(dashboard: process_dashboard)
rescue DASHBOARD_LAYOUT_ERROR => e
rescue PROCESSING_ERROR => e
error(e.message, :unprocessable_entity)
end
......
......@@ -11,11 +11,13 @@ module Gitlab
SYSTEM_SEQUENCE = [
Stages::CommonMetricsInserter,
Stages::ProjectMetricsInserter,
Stages::EndpointInserter,
Stages::Sorter
].freeze
PROJECT_SEQUENCE = [
Stages::CommonMetricsInserter,
Stages::EndpointInserter,
Stages::Sorter
].freeze
......
......@@ -5,7 +5,8 @@ module Gitlab
module Dashboard
module Stages
class BaseStage
DashboardLayoutError = Class.new(StandardError)
DashboardProcessingError = Class.new(StandardError)
LayoutError = Class.new(DashboardProcessingError)
DEFAULT_PANEL_TYPE = 'area-chart'
......@@ -25,15 +26,15 @@ module Gitlab
protected
def missing_panel_groups!
raise DashboardLayoutError.new('Top-level key :panel_groups must be an array')
raise LayoutError.new('Top-level key :panel_groups must be an array')
end
def missing_panels!
raise DashboardLayoutError.new('Each "panel_group" must define an array :panels')
raise LayoutError.new('Each "panel_group" must define an array :panels')
end
def missing_metrics!
raise DashboardLayoutError.new('Each "panel" must define an array :metrics')
raise LayoutError.new('Each "panel" must define an array :metrics')
end
def for_metrics
......
# frozen_string_literal: true
module Gitlab
module Metrics
module Dashboard
module Stages
class EndpointInserter < BaseStage
MissingQueryError = Class.new(DashboardProcessingError)
def transform!
for_metrics do |metric|
metric[:prometheus_endpoint_path] = endpoint_for_metric(metric)
end
end
private
def endpoint_for_metric(metric)
Gitlab::Routing.url_helpers.prometheus_api_project_environment_path(
project,
environment,
proxy_path: query_type(metric),
query: query_for_metric(metric)
)
end
def query_type(metric)
metric[:query] ? :query : :query_range
end
def query_for_metric(metric)
query = metric[query_type(metric)]
raise MissingQueryError.new('Each "metric" must define one of :query or :query_range') unless query
query
end
end
end
end
end
end
......@@ -85,12 +85,12 @@ describe Projects::Environments::PrometheusApiController do
context 'with nil result' do
let(:service_result) { nil }
it 'returns 202 accepted' do
it 'returns 204 no_content' do
get :proxy, params: environment_params
expect(json_response['status']).to eq('processing')
expect(json_response['message']).to eq('Not ready yet. Try again later.')
expect(response).to have_gitlab_http_status(:accepted)
expect(response).to have_gitlab_http_status(:no_content)
end
end
......
......@@ -2,7 +2,8 @@
"type": "object",
"required": [
"unit",
"label"
"label",
"prometheus_endpoint_path"
],
"oneOf": [
{ "required": ["query"] },
......@@ -14,7 +15,8 @@
"query": { "type": "string" },
"unit": { "type": "string" },
"label": { "type": "string" },
"track": { "type": "string" }
"track": { "type": "string" },
"prometheus_endpoint_path": { "type": "string" }
},
"additionalProperties": false
}
......@@ -6,7 +6,7 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi
include MetricsDashboardHelpers
set(:project) { build(:project) }
set(:environment) { build(:environment, project: project) }
set(:environment) { create(:environment, project: project) }
let(:system_dashboard_path) { Gitlab::Metrics::Dashboard::SystemDashboardService::SYSTEM_DASHBOARD_PATH}
describe '.find' do
......@@ -27,6 +27,13 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi
it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity
end
context 'when the dashboard contains a metric without a query' do
let(:dashboard) { { 'panel_groups' => [{ 'panels' => [{ 'metrics' => [{ 'id' => 'mock' }] }] }] } }
let(:project) { project_with_dashboard(dashboard_path, dashboard.to_yaml) }
it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity
end
context 'when the system dashboard is specified' do
let(:dashboard_path) { system_dashboard_path }
......
......@@ -4,13 +4,19 @@ require 'spec_helper'
describe Gitlab::Metrics::Dashboard::Processor do
let(:project) { build(:project) }
let(:environment) { build(:environment, project: project) }
let(:environment) { create(:environment, project: project) }
let(:dashboard_yml) { YAML.load_file('spec/fixtures/lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
describe 'process' do
let(:process_params) { [project, environment, dashboard_yml] }
let(:dashboard) { described_class.new(*process_params).process(insert_project_metrics: true) }
it 'includes a path for the prometheus endpoint with each metric' do
expect(all_metrics).to satisfy_all do |metric|
metric[:prometheus_endpoint_path] == prometheus_path(metric[:query_range])
end
end
context 'when dashboard config corresponds to common metrics' do
let!(:common_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
......@@ -61,7 +67,7 @@ describe Gitlab::Metrics::Dashboard::Processor do
shared_examples_for 'errors with message' do |expected_message|
it 'raises a DashboardLayoutError' do
error_class = Gitlab::Metrics::Dashboard::Stages::BaseStage::DashboardLayoutError
error_class = Gitlab::Metrics::Dashboard::Stages::BaseStage::DashboardProcessingError
expect { dashboard }.to raise_error(error_class, expected_message)
end
......@@ -84,6 +90,12 @@ describe Gitlab::Metrics::Dashboard::Processor do
it_behaves_like 'errors with message', 'Each "panel" must define an array :metrics'
end
context 'when the dashboard contains a metric which is missing a query' do
let(:dashboard_yml) { { panel_groups: [{ panels: [{ metrics: [{}] }] }] } }
it_behaves_like 'errors with message', 'Each "metric" must define one of :query or :query_range'
end
end
private
......@@ -99,7 +111,17 @@ describe Gitlab::Metrics::Dashboard::Processor do
query_range: metric.query,
unit: metric.unit,
label: metric.legend,
metric_id: metric.id
metric_id: metric.id,
prometheus_endpoint_path: prometheus_path(metric.query)
}
end
def prometheus_path(query)
Gitlab::Routing.url_helpers.prometheus_api_project_environment_path(
project,
environment,
proxy_path: :query_range,
query: query
)
end
end
......@@ -7,7 +7,7 @@ describe Gitlab::Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_m
set(:user) { build(:user) }
set(:project) { build(:project) }
set(:environment) { build(:environment, project: project) }
set(:environment) { create(:environment, project: project) }
before do
project.add_maintainer(user)
......
......@@ -6,7 +6,7 @@ describe Gitlab::Metrics::Dashboard::SystemDashboardService, :use_clean_rails_me
include MetricsDashboardHelpers
set(:project) { build(:project) }
set(:environment) { build(:environment, project: project) }
set(:environment) { create(:environment, project: project) }
describe 'get_dashboard' do
let(:dashboard_path) { described_class::SYSTEM_DASHBOARD_PATH }
......
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