Commit 8ad9cd55 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Implement PrometheusDeploymentLocator + fix metrics controller

parent 2c090539
module Projects module Projects
module Prometheus module Prometheus
class MetricsController < Projects::ApplicationController class MetricsController < Projects::ApplicationController
include PrometheusAdapterLocator
before_action :authorize_admin_project! before_action :authorize_admin_project!
before_action :require_prometheus_metrics! before_action :require_prometheus_metrics!
...@@ -20,10 +22,6 @@ module Projects ...@@ -20,10 +22,6 @@ module Projects
private private
def prometheus_adapter
project.prometheus_service
end
def require_prometheus_metrics! def require_prometheus_metrics!
render_404 unless prometheus_adapter.can_query? render_404 unless prometheus_adapter.can_query?
end end
......
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
\ No newline at end of file
class Environment < ActiveRecord::Base class Environment < ActiveRecord::Base
include PrometheusAdapterLocator
# Used to generate random suffixes for the slug # Used to generate random suffixes for the slug
LETTERS = 'a'..'z' LETTERS = 'a'..'z'
NUMBERS = '0'..'9' NUMBERS = '0'..'9'
...@@ -224,27 +226,6 @@ class Environment < ActiveRecord::Base ...@@ -224,27 +226,6 @@ class Environment < ActiveRecord::Base
project.deployment_platform project.deployment_platform
end 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
private private
# Slugifying a name may remove the uniqueness guarantee afforded by it being # Slugifying a name may remove the uniqueness guarantee afforded by it being
......
...@@ -7,8 +7,7 @@ describe Projects::Prometheus::MetricsController do ...@@ -7,8 +7,7 @@ describe Projects::Prometheus::MetricsController do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) } let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
before do before do
allow(controller).to receive(:project).and_return(project) allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(project).to receive(:prometheus_service).and_return(prometheus_adapter)
project.add_master(user) project.add_master(user)
sign_in(user) sign_in(user)
......
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