Commit 80d4c067 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Add test checking if prometheus integration is enabled after prometheus is installed

parent 249c9a8c
...@@ -10,6 +10,15 @@ module Clusters ...@@ -10,6 +10,15 @@ module Clusters
default_value_for :version, VERSION default_value_for :version, VERSION
state_machine :status do
after_transition any => [:installed] do |application|
application.cluster.projects.each do |project|
# raise "exe"
project.prometheus_service&.update(active: true)
end
end
end
def chart def chart
'stable/prometheus' 'stable/prometheus'
end end
......
module Gitlab
PrometheusError = Class.new(StandardError)
end
class PrometheusService < MonitoringService class PrometheusService < MonitoringService
include ReactiveService include ReactiveService
...@@ -8,7 +12,6 @@ class PrometheusService < MonitoringService ...@@ -8,7 +12,6 @@ class PrometheusService < MonitoringService
# Access to prometheus is directly through the API # Access to prometheus is directly through the API
prop_accessor :api_url prop_accessor :api_url
boolean_accessor :manual_configuration boolean_accessor :manual_configuration
boolean_accessor :prometheus_installed
with_options presence: true, if: :manual_configuration? do with_options presence: true, if: :manual_configuration? do
validates :api_url, url: true validates :api_url, url: true
...@@ -18,10 +21,9 @@ class PrometheusService < MonitoringService ...@@ -18,10 +21,9 @@ class PrometheusService < MonitoringService
after_save :clear_reactive_cache! after_save :clear_reactive_cache!
def initialize_properties def initialize_properties
if properties.nil? if properties.nil?
self.properties = { prometheus_installed: false } self.properties = { }
end end
end end
...@@ -54,7 +56,6 @@ class PrometheusService < MonitoringService ...@@ -54,7 +56,6 @@ class PrometheusService < MonitoringService
} }
] ]
}, },
{ {
type: 'text', type: 'text',
name: 'api_url', name: 'api_url',
...@@ -126,6 +127,10 @@ class PrometheusService < MonitoringService ...@@ -126,6 +127,10 @@ class PrometheusService < MonitoringService
end end
end end
def prometheus_installed?
cluster_with_prometheus.present?
end
private private
def cluster_with_prometheus(environment_id = nil) def cluster_with_prometheus(environment_id = nil)
...@@ -135,7 +140,7 @@ class PrometheusService < MonitoringService ...@@ -135,7 +140,7 @@ class PrometheusService < MonitoringService
project.clusters.enabled.select { |c| c.environment_scope == '*' || c.environment_scope == '' } project.clusters.enabled.select { |c| c.environment_scope == '*' || c.environment_scope == '' }
end end
clusters.detect { |cluster| cluster.application_prometheus.installed? } clusters.detect { |cluster| cluster.application_prometheus&.installed? }
end end
def rename_data_to_metrics(metrics) def rename_data_to_metrics(metrics)
...@@ -144,7 +149,8 @@ class PrometheusService < MonitoringService ...@@ -144,7 +149,8 @@ class PrometheusService < MonitoringService
end end
def synchronize_service_state! def synchronize_service_state!
self.active = prometheus_installed? || manual_configuration? || cluster_with_prometheus.present? self.active = prometheus_installed? || self.manual_configuration?
self.prometheus_installed = !manual_configuration? && cluster_with_prometheus.present?
true
end end
end end
.row.prepend-top-default.append-bottom-default
%p
- unless @service.manual_configuration?
- if @service.prometheus_installed?
= link_to 'Manage installed Prometheus', project_clusters_path(@project), class: 'btn btn-cancel'
- else
= link_to 'Install Prometheus', project_clusters_path(@project), class: 'btn btn-cancel'
- else
To automatically install prometheus disable manual configuration
...@@ -2,22 +2,20 @@ module Gitlab ...@@ -2,22 +2,20 @@ module Gitlab
module Prometheus module Prometheus
module Queries module Queries
class EnvironmentQuery < BaseQuery class EnvironmentQuery < BaseQuery
def query(environment_id) def query
::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
......
...@@ -4,7 +4,7 @@ module Gitlab ...@@ -4,7 +4,7 @@ module Gitlab
class MatchedMetricsQuery < BaseQuery class MatchedMetricsQuery < BaseQuery
MAX_QUERY_ITEMS = 40.freeze MAX_QUERY_ITEMS = 40.freeze
def query def query(_ = nil)
groups_data.map do |group, data| groups_data.map do |group, data|
{ {
group: group.name, group: group.name,
......
...@@ -7,7 +7,7 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery do ...@@ -7,7 +7,7 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery do
include_examples 'additional metrics query' do include_examples 'additional metrics query' do
let(:deployment) { create(:deployment, environment: environment) } let(:deployment) { create(:deployment, environment: environment) }
let(:query_params) { [deployment.id] } let(:query_params) { [environment.id, deployment.id] }
it 'queries using specific time' do it 'queries using specific time' do
expect(client).to receive(:query_range).with(anything, expect(client).to receive(:query_range).with(anything,
......
...@@ -31,7 +31,7 @@ describe Gitlab::Prometheus::Queries::DeploymentQuery do ...@@ -31,7 +31,7 @@ describe Gitlab::Prometheus::Queries::DeploymentQuery do
expect(client).to receive(:query).with('avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="environment-slug"}[30m])) * 100', expect(client).to receive(:query).with('avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="environment-slug"}[30m])) * 100',
time: stop_time) time: stop_time)
expect(subject.query(deployment.id)).to eq(memory_values: nil, memory_before: nil, memory_after: nil, expect(subject.query(environment.id, deployment.id)).to eq(memory_values: nil, memory_before: nil, memory_after: nil,
cpu_values: nil, cpu_before: nil, cpu_after: nil) cpu_values: nil, cpu_before: nil, cpu_after: nil)
end end
end end
...@@ -6,6 +6,24 @@ describe Clusters::Applications::Prometheus do ...@@ -6,6 +6,24 @@ describe Clusters::Applications::Prometheus do
include_examples 'cluster application specs', described_class include_examples 'cluster application specs', described_class
describe 'transition to installed' do
let(:project) { create(:project) }
let(:cluster) { create(:cluster, projects: [project]) }
let(:prometheus_service) { double('prometheus_service') }
subject { create(:clusters_applications_prometheus, :installing, cluster: cluster) }
before do
allow(project).to receive(:prometheus_service).and_return prometheus_service
end
it 'ensures Prometheus service is activated' do
expect(prometheus_service).to receive(:update).with(active: true)
subject.make_installed
end
end
describe "#chart_values_file" do describe "#chart_values_file" do
subject { create(:clusters_applications_prometheus).chart_values_file } subject { create(:clusters_applications_prometheus).chart_values_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