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,73 +3,78 @@ require 'prometheus/client' ...@@ -3,73 +3,78 @@ 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
def metrics_folder_present? class_methods do
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir include Gitlab::Utils::StrongMemoize
multiprocess_files_dir && def metrics_folder_present?
::Dir.exist?(multiprocess_files_dir) && multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
::File.writable?(multiprocess_files_dir)
end
def prometheus_metrics_enabled? multiprocess_files_dir &&
strong_memoize(:prometheus_metrics_enabled) do ::Dir.exist?(multiprocess_files_dir) &&
prometheus_metrics_enabled_unmemoized ::File.writable?(multiprocess_files_dir)
end
def prometheus_metrics_enabled?
strong_memoize(:prometheus_metrics_enabled) do
prometheus_metrics_enabled_unmemoized
end
end end
end
def registry def registry
strong_memoize(:registry) do strong_memoize(:registry) do
REGISTRY_MUTEX.synchronize do REGISTRY_MUTEX.synchronize do
strong_memoize(:registry) do strong_memoize(:registry) do
::Prometheus::Client.registry ::Prometheus::Client.registry
end
end end
end end
end end
end
def counter(name, docstring, base_labels = {}) def counter(name, docstring, base_labels = {})
safe_provide_metric(:counter, name, docstring, base_labels) safe_provide_metric(:counter, name, docstring, base_labels)
end end
def summary(name, docstring, base_labels = {}) def summary(name, docstring, base_labels = {})
safe_provide_metric(:summary, name, docstring, base_labels) safe_provide_metric(:summary, name, docstring, base_labels)
end end
def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all) def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
safe_provide_metric(:gauge, name, docstring, base_labels, multiprocess_mode) safe_provide_metric(:gauge, name, docstring, base_labels, multiprocess_mode)
end end
def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS) def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
safe_provide_metric(:histogram, name, docstring, base_labels, buckets) safe_provide_metric(:histogram, name, docstring, base_labels, buckets)
end end
private private
def safe_provide_metric(method, name, *args) def safe_provide_metric(method, name, *args)
metric = provide_metric(name) metric = provide_metric(name)
return metric if metric return metric if metric
PROVIDER_MUTEX.synchronize do PROVIDER_MUTEX.synchronize do
provide_metric(name) || registry.method(method).call(name, *args) provide_metric(name) || registry.method(method).call(name, *args)
end
end end
end
def provide_metric(name) def provide_metric(name)
if prometheus_metrics_enabled? if prometheus_metrics_enabled?
registry.get(name) registry.get(name)
else else
NullMetric.instance NullMetric.instance
end
end end
end
def prometheus_metrics_enabled_unmemoized def prometheus_metrics_enabled_unmemoized
metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false
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