summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@gitlab.com>2019-04-15 15:32:00 +0000
committerSean McGivern <sean@gitlab.com>2019-04-15 15:32:00 +0000
commit6e3c9bee8bf09b2dd842e4e017e6f58c23dad32b (patch)
tree126ef533dcd8488d14c408e46e67596c29573b43
parente7c9bcec5c44e3afd7e89dc5380d9d7aa5235150 (diff)
parent434359446a5436d6c11b66c7107ecaea133cd6e1 (diff)
downloadgitlab-ce-6e3c9bee8bf09b2dd842e4e017e6f58c23dad32b.tar.gz
Merge branch 'prometheus-api-var-subst' into 'master'
Add variable substitution in prometheus proxy API See merge request gitlab-org/gitlab-ce!27226
-rw-r--r--app/controllers/projects/environments/prometheus_api_controller.rb29
-rw-r--r--spec/controllers/projects/environments/prometheus_api_controller_spec.rb33
2 files changed, 59 insertions, 3 deletions
diff --git a/app/controllers/projects/environments/prometheus_api_controller.rb b/app/controllers/projects/environments/prometheus_api_controller.rb
index fd3320637b0..f8ef23cd83e 100644
--- a/app/controllers/projects/environments/prometheus_api_controller.rb
+++ b/app/controllers/projects/environments/prometheus_api_controller.rb
@@ -7,9 +7,9 @@ class Projects::Environments::PrometheusApiController < Projects::ApplicationCon
def proxy
result = Prometheus::ProxyService.new(
environment,
- request.method,
- params[:proxy_path],
- params.permit!
+ proxy_method,
+ proxy_path,
+ proxy_params
).execute
if result.nil?
@@ -31,7 +31,30 @@ class Projects::Environments::PrometheusApiController < Projects::ApplicationCon
private
+ def query_context
+ Gitlab::Prometheus::QueryVariables.call(environment)
+ end
+
def environment
@environment ||= project.environments.find(params[:id])
end
+
+ 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
end
diff --git a/spec/controllers/projects/environments/prometheus_api_controller_spec.rb b/spec/controllers/projects/environments/prometheus_api_controller_spec.rb
index 5a0b92c2514..d232408b775 100644
--- a/spec/controllers/projects/environments/prometheus_api_controller_spec.rb
+++ b/spec/controllers/projects/environments/prometheus_api_controller_spec.rb
@@ -14,6 +14,7 @@ describe Projects::Environments::PrometheusApiController do
describe 'GET #proxy' do
let(:prometheus_proxy_service) { instance_double(Prometheus::ProxyService) }
+
let(:expected_params) do
ActionController::Parameters.new(
environment_params(
@@ -47,6 +48,38 @@ describe Projects::Environments::PrometheusApiController do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq(prometheus_json_body)
end
+
+ context 'with format string' do
+ before do
+ expected_params[:query] = %{up{environment="#{environment.slug}"}}
+ end
+
+ it 'replaces variables with values' do
+ get :proxy, params: environment_params.merge(query: 'up{environment="%{ci_environment_slug}"}')
+
+ expect(Prometheus::ProxyService).to have_received(:new)
+ .with(environment, 'GET', 'query', expected_params)
+ end
+
+ context 'with nil query' do
+ let(:params_without_query) do
+ params = environment_params
+ params.delete(:query)
+ params
+ end
+
+ before do
+ expected_params.delete(:query)
+ end
+
+ it 'does not raise error' do
+ get :proxy, params: params_without_query
+
+ expect(Prometheus::ProxyService).to have_received(:new)
+ .with(environment, 'GET', 'query', expected_params)
+ end
+ end
+ end
end
context 'with nil result' do