diff options
author | rpereira2 <rpereira@gitlab.com> | 2019-04-02 09:26:09 +0300 |
---|---|---|
committer | rpereira2 <rpereira@gitlab.com> | 2019-04-04 23:42:33 +0300 |
commit | e1a167ee234fbb8886ac1a882002b76adeb67edf (patch) | |
tree | d7bd9ce87625fe792ea912641fe844b667459568 /app | |
parent | 5dd6e752c0c3d572c645ac56a79c6c5a3fbd5157 (diff) |
Add a Prometheus API per environment
The api will proxy requests to the environment's prometheus server.
The Prometheus::ProxyService class can be reused when we add support for
group prometheus servers.
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects/environments/prometheus_api_controller.rb | 47 | ||||
-rw-r--r-- | app/policies/project_policy.rb | 1 |
2 files changed, 48 insertions, 0 deletions
diff --git a/app/controllers/projects/environments/prometheus_api_controller.rb b/app/controllers/projects/environments/prometheus_api_controller.rb new file mode 100644 index 00000000000..337b7fdf65d --- /dev/null +++ b/app/controllers/projects/environments/prometheus_api_controller.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class Projects::Environments::PrometheusApiController < Projects::ApplicationController + before_action :authorize_read_prometheus! + before_action :environment + + def proxy + permitted = permit_params + + result = Prometheus::ProxyService.new( + environment, + request.method, + permitted[:proxy_path], + permitted.except(:proxy_path) # rubocop: disable CodeReuse/ActiveRecord + ).execute + + if result.nil? + render status: :accepted, json: { + status: 'processing', + message: 'Not ready yet. Try again later.' + } + return + end + + if result[:status] == :success + render status: result[:http_status], json: result[:body] + else + render status: result[:http_status] || :bad_request, json: { + status: result[:status], + message: result[:message] + } + end + end + + private + + def permit_params + params.permit([ + :proxy_path, :query, :time, :timeout, :start, :end, :step, { match: [] }, + :match_target, :metric, :limit + ]) + end + + def environment + @environment ||= project.environments.find(params[:id]) + end +end diff --git a/app/policies/project_policy.rb b/app/policies/project_policy.rb index 75825c8fac0..26d7d6e84c4 100644 --- a/app/policies/project_policy.rb +++ b/app/policies/project_policy.rb @@ -204,6 +204,7 @@ class ProjectPolicy < BasePolicy enable :read_merge_request enable :read_sentry_issue enable :read_release + enable :read_prometheus end # We define `:public_user_access` separately because there are cases in gitlab-ee |