Commit dd78dc0a authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'refactor_deployment_metrics' into 'master'

Refactor DeploymentMetric to reuse code

Closes #119297

See merge request gitlab-org/gitlab!22458
parents 5a9a8d21 0252ca40
......@@ -23,7 +23,7 @@ module Projects
private
def prometheus_adapter
@prometheus_adapter ||= ::Prometheus::AdapterService.new(project, project.deployment_platform&.cluster).prometheus_adapter
@prometheus_adapter ||= ::Gitlab::Prometheus::Adapter.new(project, project.deployment_platform&.cluster).prometheus_adapter
end
def require_prometheus_metrics!
......
......@@ -34,20 +34,10 @@ class DeploymentMetrics
def prometheus_adapter
strong_memoize(:prometheus_adapter) do
service = project.find_or_initialize_service('prometheus')
if service.can_query?
service
else
cluster_prometheus
end
Gitlab::Prometheus::Adapter.new(project, cluster).prometheus_adapter
end
end
def cluster_prometheus
cluster.application_prometheus if cluster&.application_prometheus_available?
end
def has_metrics_and_can_query?
has_metrics? && prometheus_adapter.can_query?
end
......
......@@ -225,11 +225,9 @@ class Environment < ApplicationRecord
prometheus_adapter.query(:additional_metrics_environment, self, *args.map(&:to_f))
end
# rubocop: disable CodeReuse/ServiceClass
def prometheus_adapter
@prometheus_adapter ||= Prometheus::AdapterService.new(project, deployment_platform&.cluster).prometheus_adapter
@prometheus_adapter ||= Gitlab::Prometheus::Adapter.new(project, deployment_platform&.cluster).prometheus_adapter
end
# rubocop: enable CodeReuse/ServiceClass
def slug
super.presence || generate_slug
......
# frozen_string_literal: true
module Prometheus
class AdapterService
attr_reader :project, :cluster
def initialize(project, cluster)
@project = project
@cluster = cluster
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
application = cluster&.application_prometheus
application if application&.available?
end
end
end
......@@ -81,7 +81,7 @@ module EE
end
def cluster_prometheus_adapter
@cluster_prometheus_adapter ||= Prometheus::AdapterService.new(project, deployment_platform&.cluster).cluster_prometheus_adapter
@cluster_prometheus_adapter ||= ::Gitlab::Prometheus::Adapter.new(project, deployment_platform&.cluster).cluster_prometheus_adapter
end
def protected?
......
# frozen_string_literal: true
module Gitlab
module Prometheus
class Adapter
attr_reader :project, :cluster
def initialize(project, cluster)
@project = project
@cluster = cluster
end
def prometheus_adapter
@prometheus_adapter ||= if service_prometheus_adapter.can_query?
service_prometheus_adapter
else
cluster_prometheus_adapter
end
end
def cluster_prometheus_adapter
application = cluster&.application_prometheus
application if application&.available?
end
private
def service_prometheus_adapter
project.find_or_initialize_service('prometheus')
end
end
end
end
......@@ -85,7 +85,7 @@ describe Projects::Prometheus::MetricsController do
end
it 'calls prometheus adapter service' do
expect_next_instance_of(::Prometheus::AdapterService) do |instance|
expect_next_instance_of(::Gitlab::Prometheus::Adapter) do |instance|
expect(instance).to receive(:prometheus_adapter)
end
......
......@@ -108,7 +108,7 @@ describe Projects::Serverless::FunctionsFinder do
let(:finder) { described_class.new(project) }
before do
allow(Prometheus::AdapterService).to receive(:new).and_return(double(prometheus_adapter: prometheus_adapter))
allow(Gitlab::Prometheus::Adapter).to receive(:new).and_return(double(prometheus_adapter: prometheus_adapter))
allow(prometheus_adapter).to receive(:query).and_return(prometheus_empty_body('matrix'))
end
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe Prometheus::AdapterService do
describe Gitlab::Prometheus::Adapter do
let_it_be(:project) { create(:project) }
let_it_be(:cluster, reload: true) { create(:cluster, :provided_by_user, environment_scope: '*', projects: [project]) }
......
......@@ -1112,7 +1112,7 @@ describe Environment, :use_clean_rails_memory_store_caching do
describe '#prometheus_adapter' do
it 'calls prometheus adapter service' do
expect_next_instance_of(Prometheus::AdapterService) do |instance|
expect_next_instance_of(Gitlab::Prometheus::Adapter) do |instance|
expect(instance).to receive(:prometheus_adapter)
end
......
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