summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPawel Chojnacki <pawel@chojnacki.ws>2018-03-05 19:34:59 +0100
committerPawel Chojnacki <pawel@chojnacki.ws>2018-03-05 19:34:59 +0100
commit5ceec83a0154e513480a9bb08e252371bf7886c6 (patch)
tree9a193e295c56feb2c29ac042b59b14b1808715d1
parent273e4142566a053e0628183fbf86ef7b94cb5a66 (diff)
downloadgitlab-ce-5ceec83a0154e513480a9bb08e252371bf7886c6.tar.gz
Implemente Prometheus:AdapterService
-rw-r--r--app/controllers/projects/prometheus/metrics_controller.rb6
-rw-r--r--app/models/concerns/prometheus_adapter_locator.rb26
-rw-r--r--app/models/environment.rb6
-rw-r--r--app/services/prometheus/adapter_service.rb36
-rw-r--r--spec/services/prometheus/adapter_service_spec.rb40
5 files changed, 84 insertions, 30 deletions
diff --git a/app/controllers/projects/prometheus/metrics_controller.rb b/app/controllers/projects/prometheus/metrics_controller.rb
index ec3c49059e1..523abce35fb 100644
--- a/app/controllers/projects/prometheus/metrics_controller.rb
+++ b/app/controllers/projects/prometheus/metrics_controller.rb
@@ -1,8 +1,6 @@
module Projects
module Prometheus
class MetricsController < Projects::ApplicationController
- include PrometheusAdapterLocator
-
before_action :authorize_admin_project!
before_action :require_prometheus_metrics!
@@ -22,6 +20,10 @@ module Projects
private
+ def prometheus_adapter
+ @prometheus_adapter ||= Prometheus::AdapterService.new(project).prometheus_adapter
+ end
+
def require_prometheus_metrics!
render_404 unless prometheus_adapter.can_query?
end
diff --git a/app/models/concerns/prometheus_adapter_locator.rb b/app/models/concerns/prometheus_adapter_locator.rb
deleted file mode 100644
index d8a3ed0de2d..00000000000
--- a/app/models/concerns/prometheus_adapter_locator.rb
+++ /dev/null
@@ -1,26 +0,0 @@
-module PrometheusAdapterLocator
- def deployment_platform
- project.deployment_platform
- end
-
- def prometheus_adapter
- @prometheus_adapter ||= if service_prometheus_adapter.can_query?
- service_prometheus_adapter
- else
- cluster_prometheus_adapter
- end
- end
-
- def service_prometheus_adapter
- project.find_or_initialize_service('prometheus')
- end
-
- def cluster_prometheus_adapter
- return unless deployment_platform.respond_to?(:cluster)
-
- cluster = deployment_platform.cluster
- return unless cluster.application_prometheus&.installed?
-
- cluster.application_prometheus
- end
-end
diff --git a/app/models/environment.rb b/app/models/environment.rb
index baba023c92d..24d4f1d8761 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -1,6 +1,4 @@
class Environment < ActiveRecord::Base
- include PrometheusAdapterLocator
-
# Used to generate random suffixes for the slug
LETTERS = 'a'..'z'
NUMBERS = '0'..'9'
@@ -159,6 +157,10 @@ class Environment < ActiveRecord::Base
prometheus_adapter.query(:additional_metrics_environment, self) if has_metrics?
end
+ def prometheus_adapter
+ @prometheus_adapter ||= Prometheus::AdapterService.new(project, deployment_platform).prometheus_adapter
+ end
+
def slug
super.presence || generate_slug
end
diff --git a/app/services/prometheus/adapter_service.rb b/app/services/prometheus/adapter_service.rb
new file mode 100644
index 00000000000..4504d2ccfe6
--- /dev/null
+++ b/app/services/prometheus/adapter_service.rb
@@ -0,0 +1,36 @@
+module Prometheus
+ class AdapterService
+ def initialize(project, deployment_platform = nil)
+ @project = project
+
+ @deployment_platform = if deployment_platform
+ deployment_platform
+ else
+ project.deployment_platform
+ end
+ end
+
+ attr_reader :deployment_platform, :project
+
+ def prometheus_adapter
+ @prometheus_adapter ||= if service_prometheus_adapter.can_query?
+ service_prometheus_adapter
+ else
+ cluster_prometheus_adapter
+ end
+ end
+
+ def service_prometheus_adapter
+ project.find_or_initialize_service('prometheus')
+ end
+
+ def cluster_prometheus_adapter
+ return unless deployment_platform.respond_to?(:cluster)
+
+ cluster = deployment_platform.cluster
+ return unless cluster.application_prometheus&.installed?
+
+ cluster.application_prometheus
+ end
+ end
+end
diff --git a/spec/services/prometheus/adapter_service_spec.rb b/spec/services/prometheus/adapter_service_spec.rb
new file mode 100644
index 00000000000..335fc5844aa
--- /dev/null
+++ b/spec/services/prometheus/adapter_service_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe Prometheus::AdapterService do
+ let(:project) { create(:project) }
+ subject { described_class.new(project) }
+
+ describe '#prometheus_adapter' do
+ let(:cluster) { create(:cluster, :provided_by_user, environment_scope: '*', projects: [project]) }
+
+ context 'prometheus service can execute queries' do
+ let(:prometheus_service) { double(:prometheus_service, can_query?: true) }
+
+ before do
+ allow(project).to receive(:find_or_initialize_service).with('prometheus').and_return prometheus_service
+ end
+
+ it 'return prometheus service as prometheus adapter' do
+ expect(subject.prometheus_adapter).to eq(prometheus_service)
+ end
+ end
+
+ context "prometheus service can't execute queries" do
+ let(:prometheus_service) { double(:prometheus_service, can_query?: false) }
+
+ context 'with cluster with prometheus installed' do
+ let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
+
+ it 'returns application handling all environments' do
+ expect(subject.prometheus_adapter).to eq(prometheus)
+ end
+ end
+
+ context 'with cluster without prometheus installed' do
+ it 'returns nil' do
+ expect(subject.prometheus_adapter).to be_nil
+ end
+ end
+ end
+ end
+end