Commit ea6196d4 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Move prometheus metrics to module. Use class instance variables for metrics...

Move prometheus metrics to module. Use class instance variables for metrics and metrics cache in metrics concern
parent 27359dbf
module Gitlab module Gitlab
module Metrics module Metrics
extend Gitlab::Metrics::InfluxDb extend Gitlab::Metrics::InfluxDb
extend Gitlab::Metrics::Prometheus include Gitlab::Metrics::Prometheus
def self.enabled? def self.enabled?
influx_metrics_enabled? || prometheus_metrics_enabled? influx_metrics_enabled? || prometheus_metrics_enabled?
......
...@@ -6,12 +6,13 @@ module Gitlab ...@@ -6,12 +6,13 @@ module Gitlab
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
@@_metric_provider_mutex = Mutex.new @_metric_provider_mutex = Mutex.new
@_metrics_provider_cache = {}
end end
class_methods do class_methods do
def reload_metric!(name) def reload_metric!(name)
@_metrics_provider_cache&.delete(name) @_metrics_provider_cache.delete(name)
end end
private private
...@@ -22,12 +23,12 @@ module Gitlab ...@@ -22,12 +23,12 @@ module Gitlab
end end
define_singleton_method(name) do define_singleton_method(name) do
@_metrics_provider_cache&.[](name) || init_metric(type, name, opts, &block) @_metrics_provider_cache[name] || init_metric(type, name, opts, &block)
end end
end end
def fetch_metric(type, name, opts = {}, &block) def fetch_metric(type, name, opts = {}, &block)
@_metrics_provider_cache&.[](name) || init_metric(type, name, opts, &block) @_metrics_provider_cache[name] || init_metric(type, name, opts, &block)
end end
def init_metric(type, name, opts = {}, &block) def init_metric(type, name, opts = {}, &block)
...@@ -42,8 +43,7 @@ module Gitlab ...@@ -42,8 +43,7 @@ module Gitlab
end end
def synchronized_cache_fill(key) def synchronized_cache_fill(key)
@@_metric_provider_mutex.synchronize do @_metric_provider_mutex.synchronize do
@_metrics_provider_cache ||= {}
@_metrics_provider_cache[key] ||= yield @_metrics_provider_cache[key] ||= yield
end end
end end
......
module Gitlab module Gitlab
module Metrics module Metrics
module InfluxDb module InfluxDb
include Gitlab::Metrics::Concern::ClassMethods include Gitlab::Metrics::Concern
include Gitlab::CurrentSettings include Gitlab::CurrentSettings
extend self extend self
......
...@@ -3,12 +3,16 @@ require 'prometheus/client' ...@@ -3,12 +3,16 @@ require 'prometheus/client'
module Gitlab module Gitlab
module Metrics module Metrics
module Prometheus module Prometheus
extend ActiveSupport::Concern
include Gitlab::Metrics::Concern
include Gitlab::CurrentSettings include Gitlab::CurrentSettings
include Gitlab::Utils::StrongMemoize
REGISTRY_MUTEX = Mutex.new REGISTRY_MUTEX = Mutex.new
PROVIDER_MUTEX = Mutex.new PROVIDER_MUTEX = Mutex.new
class_methods do
include Gitlab::Utils::StrongMemoize
def metrics_folder_present? def metrics_folder_present?
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
...@@ -73,4 +77,5 @@ module Gitlab ...@@ -73,4 +77,5 @@ module Gitlab
end end
end end
end end
end
end 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