2019-04-02 02:26:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Projects::Environments::PrometheusApiController < Projects::ApplicationController
|
|
|
|
before_action :authorize_read_prometheus!
|
|
|
|
before_action :environment
|
|
|
|
|
|
|
|
def proxy
|
|
|
|
result = Prometheus::ProxyService.new(
|
|
|
|
environment,
|
2019-04-15 11:31:59 -04:00
|
|
|
proxy_method,
|
|
|
|
proxy_path,
|
|
|
|
proxy_params
|
2019-04-02 02:26:09 -04:00
|
|
|
).execute
|
|
|
|
|
|
|
|
if result.nil?
|
2019-06-05 03:12:10 -04:00
|
|
|
return render status: :no_content, json: {
|
2019-04-04 07:40:06 -04:00
|
|
|
status: _('processing'),
|
|
|
|
message: _('Not ready yet. Try again later.')
|
2019-04-02 02:26:09 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
render status: result[:http_status], json: result[:body]
|
|
|
|
else
|
2019-04-03 05:31:44 -04:00
|
|
|
render(
|
|
|
|
status: result[:http_status] || :bad_request,
|
2019-04-02 12:38:49 -04:00
|
|
|
json: { status: result[:status], message: result[:message] }
|
2019-04-03 05:31:44 -04:00
|
|
|
)
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-04-15 11:31:59 -04:00
|
|
|
def query_context
|
|
|
|
Gitlab::Prometheus::QueryVariables.call(environment)
|
|
|
|
end
|
|
|
|
|
2019-04-02 02:26:09 -04:00
|
|
|
def environment
|
|
|
|
@environment ||= project.environments.find(params[:id])
|
|
|
|
end
|
2019-04-15 11:31:59 -04:00
|
|
|
|
|
|
|
def proxy_method
|
|
|
|
request.method
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy_path
|
|
|
|
params[:proxy_path]
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy_params
|
|
|
|
substitute_query_variables(params).permit!
|
|
|
|
end
|
|
|
|
|
|
|
|
def substitute_query_variables(params)
|
|
|
|
query = params[:query]
|
|
|
|
return params unless query
|
|
|
|
|
|
|
|
params.merge(query: query % query_context)
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|