Commit 19812e3e authored by Pawel Chojnacki's avatar Pawel Chojnacki

Make queries not die when underlying data cannot be found

parent 15b7b9ec
...@@ -5,15 +5,16 @@ module Gitlab ...@@ -5,15 +5,16 @@ module Gitlab
include QueryAdditionalMetrics include QueryAdditionalMetrics
def query(deployment_id) def query(deployment_id)
deployment = Deployment.find_by(id: deployment_id) Deployment.find_by(id: deployment_id).try do |deployment|
query_context = { query_context = {
environment_slug: deployment.environment.slug, environment_slug: deployment.environment.slug,
environment_filter: %{container_name!="POD",environment="#{deployment.environment.slug}"}, environment_filter: %{container_name!="POD",environment="#{deployment.environment.slug}"},
timeframe_start: (deployment.created_at - 30.minutes).to_f, timeframe_start: (deployment.created_at - 30.minutes).to_f,
timeframe_end: (deployment.created_at + 30.minutes).to_f timeframe_end: (deployment.created_at + 30.minutes).to_f
} }
query_metrics(query_context) query_metrics(query_context)
end
end end
end end
end end
......
...@@ -5,15 +5,16 @@ module Gitlab ...@@ -5,15 +5,16 @@ module Gitlab
include QueryAdditionalMetrics include QueryAdditionalMetrics
def query(environment_id) def query(environment_id)
environment = Environment.find_by(id: environment_id) Environment.find_by(id: environment_id).try do |environment|
query_context = { query_context = {
environment_slug: environment.slug, environment_slug: environment.slug,
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}, environment_filter: %{container_name!="POD",environment="#{environment.slug}"},
timeframe_start: 8.hours.ago.to_f, timeframe_start: 8.hours.ago.to_f,
timeframe_end: Time.now.to_f timeframe_end: Time.now.to_f
} }
query_metrics(query_context) query_metrics(query_context)
end
end end
end end
end end
......
...@@ -3,26 +3,27 @@ module Gitlab ...@@ -3,26 +3,27 @@ module Gitlab
module Queries module Queries
class DeploymentQuery < BaseQuery class DeploymentQuery < BaseQuery
def query(deployment_id) def query(deployment_id)
deployment = Deployment.find_by(id: deployment_id) Deployment.find_by(id: deployment_id).try do |deployment|
environment_slug = deployment.environment.slug environment_slug = deployment.environment.slug
memory_query = raw_memory_usage_query(environment_slug) memory_query = raw_memory_usage_query(environment_slug)
memory_avg_query = %{avg(avg_over_time(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}[30m]))} memory_avg_query = %{avg(avg_over_time(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}[30m]))}
cpu_query = raw_cpu_usage_query(environment_slug) cpu_query = raw_cpu_usage_query(environment_slug)
cpu_avg_query = %{avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}[30m])) * 100} cpu_avg_query = %{avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}[30m])) * 100}
timeframe_start = (deployment.created_at - 30.minutes).to_f timeframe_start = (deployment.created_at - 30.minutes).to_f
timeframe_end = (deployment.created_at + 30.minutes).to_f timeframe_end = (deployment.created_at + 30.minutes).to_f
{ {
memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end), memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end),
memory_before: client_query(memory_avg_query, time: deployment.created_at.to_f), memory_before: client_query(memory_avg_query, time: deployment.created_at.to_f),
memory_after: client_query(memory_avg_query, time: timeframe_end), memory_after: client_query(memory_avg_query, time: timeframe_end),
cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end), cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end),
cpu_before: client_query(cpu_avg_query, time: deployment.created_at.to_f), cpu_before: client_query(cpu_avg_query, time: deployment.created_at.to_f),
cpu_after: client_query(cpu_avg_query, time: timeframe_end) cpu_after: client_query(cpu_avg_query, time: timeframe_end)
} }
end
end end
end end
end end
......
...@@ -3,20 +3,21 @@ module Gitlab ...@@ -3,20 +3,21 @@ module Gitlab
module Queries module Queries
class EnvironmentQuery < BaseQuery class EnvironmentQuery < BaseQuery
def query(environment_id) def query(environment_id)
environment = Environment.find_by(id: environment_id) Environment.find_by(id: environment_id).try do |environment|
environment_slug = environment.slug environment_slug = environment.slug
timeframe_start = 8.hours.ago.to_f timeframe_start = 8.hours.ago.to_f
timeframe_end = Time.now.to_f timeframe_end = Time.now.to_f
memory_query = raw_memory_usage_query(environment_slug) memory_query = raw_memory_usage_query(environment_slug)
cpu_query = raw_cpu_usage_query(environment_slug) cpu_query = raw_cpu_usage_query(environment_slug)
{ {
memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end), memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end),
memory_current: client_query(memory_query, time: timeframe_end), memory_current: client_query(memory_query, time: timeframe_end),
cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end), cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end),
cpu_current: client_query(cpu_query, time: timeframe_end) cpu_current: client_query(cpu_query, time: timeframe_end)
} }
end
end end
end end
end end
......
...@@ -141,7 +141,7 @@ describe Projects::DeploymentsController do ...@@ -141,7 +141,7 @@ describe Projects::DeploymentsController do
end end
it 'returns a empty response 204 response' do it 'returns a empty response 204 response' do
get :additional_metrics, deployment_params(id: deployment.id) get :additional_metrics, deployment_params(id: deployment.id, format: :json)
expect(response).to have_http_status(204) expect(response).to have_http_status(204)
expect(response.body).to eq('') expect(response.body).to eq('')
end end
...@@ -161,7 +161,7 @@ describe Projects::DeploymentsController do ...@@ -161,7 +161,7 @@ describe Projects::DeploymentsController do
end end
it 'returns a metrics JSON document' do it 'returns a metrics JSON document' do
get :additional_metrics, deployment_params(id: deployment.id) get :additional_metrics, deployment_params(id: deployment.id, format: :json)
expect(response).to be_ok expect(response).to be_ok
expect(json_response['success']).to be(true) expect(json_response['success']).to be(true)
......
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