debian-mirror-gitlab/lib/gitlab/metrics/methods.rb

81 lines
2.5 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
# rubocop:disable Style/ClassVars
module Gitlab
module Metrics
module Methods
extend ActiveSupport::Concern
included do
@@_metric_provider_mutex ||= Mutex.new
@@_metrics_provider_cache = {}
end
class_methods do
2021-12-11 22:18:48 +05:30
def reload_metric!(name = nil)
if name.nil?
@@_metrics_provider_cache = {}
else
@@_metrics_provider_cache.delete(name)
end
2018-03-17 18:26:18 +05:30
end
private
def define_metric(type, name, opts = {}, &block)
if respond_to?(name)
raise ArgumentError, "method #{name} already exists"
end
define_singleton_method(name) do
# inlining fetch_metric method to avoid method call overhead when instrumenting hot spots
@@_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)
end
def init_metric(type, name, opts = {}, &block)
2020-07-28 23:09:34 +05:30
options = ::Gitlab::Metrics::Methods::MetricOptions.new(opts)
2018-03-17 18:26:18 +05:30
options.evaluate(&block)
if disabled_by_feature(options)
2021-03-11 19:13:27 +05:30
synchronized_cache_fill(name) { ::Gitlab::Metrics::NullMetric.instance }
2018-03-17 18:26:18 +05:30
else
synchronized_cache_fill(name) { build_metric!(type, name, options) }
end
end
def synchronized_cache_fill(key)
@@_metric_provider_mutex.synchronize do
@@_metrics_provider_cache[key] ||= yield
end
end
def disabled_by_feature(options)
2022-07-16 23:28:13 +05:30
options.with_feature && !::Feature.enabled?(options.with_feature,
type: :undefined, default_enabled_if_undefined: false)
2018-03-17 18:26:18 +05:30
end
def build_metric!(type, name, options)
case type
when :gauge
2019-07-07 11:18:12 +05:30
::Gitlab::Metrics.gauge(name, options.docstring, options.base_labels, options.multiprocess_mode)
2018-03-17 18:26:18 +05:30
when :counter
2019-07-07 11:18:12 +05:30
::Gitlab::Metrics.counter(name, options.docstring, options.base_labels)
2018-03-17 18:26:18 +05:30
when :histogram
2019-07-07 11:18:12 +05:30
::Gitlab::Metrics.histogram(name, options.docstring, options.base_labels, options.buckets)
2018-03-17 18:26:18 +05:30
when :summary
raise NotImplementedError, "summary metrics are not currently supported"
else
raise ArgumentError, "uknown metric type #{type}"
end
end
end
end
end
end