Commit c7a1da80 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Explicitly require format.json in prometheus_controller

+ add missing prometheus_controller tests!
parent c0a66dbd
...@@ -24,10 +24,11 @@ class Projects::DeploymentsController < Projects::ApplicationController ...@@ -24,10 +24,11 @@ class Projects::DeploymentsController < Projects::ApplicationController
def additional_metrics def additional_metrics
return render_404 unless deployment.has_additional_metrics? return render_404 unless deployment.has_additional_metrics?
metrics = deployment.additional_metrics metrics = deployment.additional_metrics
if metrics&.any? if metrics.any?
render json: metrics, status: :ok render json: metrics
else else
head :no_content head :no_content
end end
......
...@@ -3,17 +3,25 @@ class Projects::PrometheusController < Projects::ApplicationController ...@@ -3,17 +3,25 @@ class Projects::PrometheusController < Projects::ApplicationController
before_action :require_prometheus_metrics! before_action :require_prometheus_metrics!
def active_metrics def active_metrics
matched_metrics = prometheus_service.reactive_query(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself) matched_metrics = prometheus_service.reactive_query(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself) || {}
if matched_metrics respond_to do |format|
render json: matched_metrics, status: :ok format.json do
else if matched_metrics.any?
head :no_content render json: matched_metrics
else
head :no_content
end
end
end end
end end
private private
rescue_from(ActionController::UnknownFormat) do |e|
render_404
end
def prometheus_service def prometheus_service
project.monitoring_service project.monitoring_service
end end
......
require('spec_helper')
describe Projects::PrometheusController do
let(:user) { create(:user) }
let!(:project) { create(:empty_project) }
let(:prometheus_service) { double('prometheus_service') }
before do
allow(controller).to receive(:project).and_return(project)
allow(project).to receive(:monitoring_service).and_return(prometheus_service)
project.add_master(user)
sign_in(user)
end
describe 'GET #active_metrics' do
context 'when prometheus metrics are enabled' do
before do
allow(prometheus_service).to receive(:reactive_query)
end
context 'when data is not present' do
it 'returns no content response' do
get :active_metrics, project_params(format: :json)
expect(response).to have_http_status(204)
end
end
context 'when data is available' do
let(:sample_response) { { some_data: 1 } }
before do
allow(prometheus_service).to receive(:reactive_query).with(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name)
.and_return(sample_response)
end
it 'returns no content response' do
get :active_metrics, project_params(format: :json)
expect(response).to have_http_status(200)
expect(json_response).to eq(sample_response.deep_stringify_keys)
end
end
context 'when requesting non json response' do
it 'returns not found response' do
get :active_metrics, project_params
expect(response).to have_http_status(404)
end
end
end
end
def project_params(opts = {})
opts.reverse_merge(namespace_id: project.namespace, project_id: project)
end
end
\ No newline at end of file
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