From 80d4c0675f5715d724be20d47cafa372524e3ed1 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Thu, 4 Jan 2018 02:45:57 +0100 Subject: [PATCH] Add test checking if prometheus integration is enabled after prometheus is installed --- .../clusters/applications/prometheus.rb | 9 +++++++ .../project_services/prometheus_service.rb | 20 +++++++++----- .../services/prometheus/_help.html.haml | 10 +++++++ .../prometheus/queries/environment_query.rb | 26 +++++++++---------- .../queries/matched_metrics_query.rb | 2 +- ...dditional_metrics_deployment_query_spec.rb | 2 +- .../queries/deployment_query_spec.rb | 2 +- .../clusters/applications/prometheus_spec.rb | 18 +++++++++++++ 8 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 app/views/projects/services/prometheus/_help.html.haml diff --git a/app/models/clusters/applications/prometheus.rb b/app/models/clusters/applications/prometheus.rb index 94cac9277a5..ac9ea707f62 100644 --- a/app/models/clusters/applications/prometheus.rb +++ b/app/models/clusters/applications/prometheus.rb @@ -10,6 +10,15 @@ module Clusters 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 'stable/prometheus' end diff --git a/app/models/project_services/prometheus_service.rb b/app/models/project_services/prometheus_service.rb index abd2be2e7fe..2fb94c1facb 100644 --- a/app/models/project_services/prometheus_service.rb +++ b/app/models/project_services/prometheus_service.rb @@ -1,3 +1,7 @@ +module Gitlab + PrometheusError = Class.new(StandardError) +end + class PrometheusService < MonitoringService include ReactiveService @@ -8,7 +12,6 @@ class PrometheusService < MonitoringService # Access to prometheus is directly through the API prop_accessor :api_url boolean_accessor :manual_configuration - boolean_accessor :prometheus_installed with_options presence: true, if: :manual_configuration? do validates :api_url, url: true @@ -18,10 +21,9 @@ class PrometheusService < MonitoringService after_save :clear_reactive_cache! - def initialize_properties if properties.nil? - self.properties = { prometheus_installed: false } + self.properties = { } end end @@ -54,7 +56,6 @@ class PrometheusService < MonitoringService } ] }, - { type: 'text', name: 'api_url', @@ -126,6 +127,10 @@ class PrometheusService < MonitoringService end end + def prometheus_installed? + cluster_with_prometheus.present? + end + private def cluster_with_prometheus(environment_id = nil) @@ -135,7 +140,7 @@ class PrometheusService < MonitoringService project.clusters.enabled.select { |c| c.environment_scope == '*' || c.environment_scope == '' } end - clusters.detect { |cluster| cluster.application_prometheus.installed? } + clusters.detect { |cluster| cluster.application_prometheus&.installed? } end def rename_data_to_metrics(metrics) @@ -144,7 +149,8 @@ class PrometheusService < MonitoringService end def synchronize_service_state! - self.active = prometheus_installed? || manual_configuration? || cluster_with_prometheus.present? - self.prometheus_installed = !manual_configuration? && cluster_with_prometheus.present? + self.active = prometheus_installed? || self.manual_configuration? + + true end end diff --git a/app/views/projects/services/prometheus/_help.html.haml b/app/views/projects/services/prometheus/_help.html.haml new file mode 100644 index 00000000000..96a194b212e --- /dev/null +++ b/app/views/projects/services/prometheus/_help.html.haml @@ -0,0 +1,10 @@ +.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 + diff --git a/lib/gitlab/prometheus/queries/environment_query.rb b/lib/gitlab/prometheus/queries/environment_query.rb index 1d17d3cfd56..8f1453f31bf 100644 --- a/lib/gitlab/prometheus/queries/environment_query.rb +++ b/lib/gitlab/prometheus/queries/environment_query.rb @@ -2,22 +2,20 @@ module Gitlab module Prometheus module Queries class EnvironmentQuery < BaseQuery - def query(environment_id) - ::Environment.find_by(id: environment_id).try do |environment| - environment_slug = environment.slug - timeframe_start = 8.hours.ago.to_f - timeframe_end = Time.now.to_f + def query + environment_slug = environment.slug + timeframe_start = 8.hours.ago.to_f + timeframe_end = Time.now.to_f - memory_query = raw_memory_usage_query(environment_slug) - cpu_query = raw_cpu_usage_query(environment_slug) + memory_query = raw_memory_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_current: client_query(memory_query, time: timeframe_end), - cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end), - cpu_current: client_query(cpu_query, time: timeframe_end) - } - end + { + memory_values: client_query_range(memory_query, start: timeframe_start, stop: 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_current: client_query(cpu_query, time: timeframe_end) + } end end end diff --git a/lib/gitlab/prometheus/queries/matched_metrics_query.rb b/lib/gitlab/prometheus/queries/matched_metrics_query.rb index 4c3edccc71a..d21f64a252b 100644 --- a/lib/gitlab/prometheus/queries/matched_metrics_query.rb +++ b/lib/gitlab/prometheus/queries/matched_metrics_query.rb @@ -4,7 +4,7 @@ module Gitlab class MatchedMetricsQuery < BaseQuery MAX_QUERY_ITEMS = 40.freeze - def query + def query(_ = nil) groups_data.map do |group, data| { group: group.name, diff --git a/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb b/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb index c7169717fc1..0697cb2def6 100644 --- a/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb +++ b/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb @@ -7,7 +7,7 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery do include_examples 'additional metrics query' do let(:deployment) { create(:deployment, environment: environment) } - let(:query_params) { [deployment.id] } + let(:query_params) { [environment.id, deployment.id] } it 'queries using specific time' do expect(client).to receive(:query_range).with(anything, diff --git a/spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb b/spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb index ffe3ad85baa..d166327b6d3 100644 --- a/spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb +++ b/spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb @@ -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', 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) end end diff --git a/spec/models/clusters/applications/prometheus_spec.rb b/spec/models/clusters/applications/prometheus_spec.rb index 696099f7cf7..b9ce597364e 100644 --- a/spec/models/clusters/applications/prometheus_spec.rb +++ b/spec/models/clusters/applications/prometheus_spec.rb @@ -6,6 +6,24 @@ describe Clusters::Applications::Prometheus do 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 subject { create(:clusters_applications_prometheus).chart_values_file }