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 Metrics
extend Gitlab::Metrics::InfluxDb
extend Gitlab::Metrics::Prometheus
include Gitlab::Metrics::Prometheus
def self.enabled?
influx_metrics_enabled? || prometheus_metrics_enabled?
......
......@@ -6,12 +6,13 @@ module Gitlab
extend ActiveSupport::Concern
included do
@@_metric_provider_mutex = Mutex.new
@_metric_provider_mutex = Mutex.new
@_metrics_provider_cache = {}
end
class_methods do
def reload_metric!(name)
@_metrics_provider_cache&.delete(name)
@_metrics_provider_cache.delete(name)
end
private
......@@ -22,12 +23,12 @@ module Gitlab
end
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
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
def init_metric(type, name, opts = {}, &block)
......@@ -42,8 +43,7 @@ module Gitlab
end
def synchronized_cache_fill(key)
@@_metric_provider_mutex.synchronize do
@_metrics_provider_cache ||= {}
@_metric_provider_mutex.synchronize do
@_metrics_provider_cache[key] ||= yield
end
end
......
module Gitlab
module Metrics
module InfluxDb
include Gitlab::Metrics::Concern::ClassMethods
include Gitlab::Metrics::Concern
include Gitlab::CurrentSettings
extend self
......
......@@ -3,73 +3,78 @@ require 'prometheus/client'
module Gitlab
module Metrics
module Prometheus
extend ActiveSupport::Concern
include Gitlab::Metrics::Concern
include Gitlab::CurrentSettings
include Gitlab::Utils::StrongMemoize
REGISTRY_MUTEX = Mutex.new
PROVIDER_MUTEX = Mutex.new
def metrics_folder_present?
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
class_methods do
include Gitlab::Utils::StrongMemoize
multiprocess_files_dir &&
::Dir.exist?(multiprocess_files_dir) &&
::File.writable?(multiprocess_files_dir)
end
def metrics_folder_present?
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
def prometheus_metrics_enabled?
strong_memoize(:prometheus_metrics_enabled) do
prometheus_metrics_enabled_unmemoized
multiprocess_files_dir &&
::Dir.exist?(multiprocess_files_dir) &&
::File.writable?(multiprocess_files_dir)
end
def prometheus_metrics_enabled?
strong_memoize(:prometheus_metrics_enabled) do
prometheus_metrics_enabled_unmemoized
end
end
end
def registry
strong_memoize(:registry) do
REGISTRY_MUTEX.synchronize do
strong_memoize(:registry) do
::Prometheus::Client.registry
def registry
strong_memoize(:registry) do
REGISTRY_MUTEX.synchronize do
strong_memoize(:registry) do
::Prometheus::Client.registry
end
end
end
end
end
def counter(name, docstring, base_labels = {})
safe_provide_metric(:counter, name, docstring, base_labels)
end
def counter(name, docstring, base_labels = {})
safe_provide_metric(:counter, name, docstring, base_labels)
end
def summary(name, docstring, base_labels = {})
safe_provide_metric(:summary, name, docstring, base_labels)
end
def summary(name, docstring, base_labels = {})
safe_provide_metric(:summary, name, docstring, base_labels)
end
def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
safe_provide_metric(:gauge, name, docstring, base_labels, multiprocess_mode)
end
def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
safe_provide_metric(:gauge, name, docstring, base_labels, multiprocess_mode)
end
def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
safe_provide_metric(:histogram, name, docstring, base_labels, buckets)
end
def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
safe_provide_metric(:histogram, name, docstring, base_labels, buckets)
end
private
private
def safe_provide_metric(method, name, *args)
metric = provide_metric(name)
return metric if metric
def safe_provide_metric(method, name, *args)
metric = provide_metric(name)
return metric if metric
PROVIDER_MUTEX.synchronize do
provide_metric(name) || registry.method(method).call(name, *args)
PROVIDER_MUTEX.synchronize do
provide_metric(name) || registry.method(method).call(name, *args)
end
end
end
def provide_metric(name)
if prometheus_metrics_enabled?
registry.get(name)
else
NullMetric.instance
def provide_metric(name)
if prometheus_metrics_enabled?
registry.get(name)
else
NullMetric.instance
end
end
end
def prometheus_metrics_enabled_unmemoized
metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false
def prometheus_metrics_enabled_unmemoized
metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false
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