gitlab-org--gitlab-foss/lib/gitlab/metrics/methods.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# rubocop:disable Style/ClassVars
module Gitlab
module Metrics
2018-01-29 06:33:08 -05:00
module Methods
extend ActiveSupport::Concern
included do
2018-01-22 10:43:04 -05:00
@@_metric_provider_mutex ||= Mutex.new
@@_metrics_provider_cache = {}
end
2018-01-16 08:06:56 -05:00
class_methods do
def reload_metric!(name = nil)
if name.nil?
@@_metrics_provider_cache = {}
else
@@_metrics_provider_cache.delete(name)
end
2018-01-16 09:47:07 -05:00
end
private
2018-01-16 08:06:56 -05:00
def define_metric(type, name, opts = {}, &block)
2018-01-17 16:16:25 -05:00
if respond_to?(name)
2018-01-19 08:54:40 -05:00
raise ArgumentError, "method #{name} already exists"
end
2018-01-16 09:47:07 -05:00
define_singleton_method(name) do
2018-01-22 14:55:48 -05:00
# inlining fetch_metric method to avoid method call overhead when instrumenting hot spots
@@_metrics_provider_cache[name] || init_metric(type, name, opts, &block)
2018-01-16 08:06:56 -05:00
end
end
def fetch_metric(type, name, opts = {}, &block)
@@_metrics_provider_cache[name] || init_metric(type, name, opts, &block)
2018-01-19 08:54:40 -05:00
end
2018-01-16 08:06:56 -05:00
2018-01-19 08:54:40 -05:00
def init_metric(type, name, opts = {}, &block)
options = ::Gitlab::Metrics::Methods::MetricOptions.new(opts)
2018-01-16 08:06:56 -05:00
options.evaluate(&block)
2018-01-18 14:22:49 -05:00
if disabled_by_feature(options)
synchronized_cache_fill(name) { ::Gitlab::Metrics::NullMetric.instance }
2018-01-18 14:22:49 -05:00
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
2018-01-16 08:06:56 -05:00
end
2018-01-18 14:22:49 -05:00
end
2018-01-16 08:06:56 -05:00
2018-01-18 14:22:49 -05:00
def disabled_by_feature(options)
options.with_feature && !::Feature.enabled?(options.with_feature,
type: :undefined, default_enabled_if_undefined: false)
2018-01-16 08:06:56 -05:00
end
def build_metric!(type, name, options)
case type
2018-01-16 20:55:52 -05:00
when :gauge
::Gitlab::Metrics.gauge(name, options.docstring, options.base_labels, options.multiprocess_mode)
2018-01-16 20:55:52 -05:00
when :counter
::Gitlab::Metrics.counter(name, options.docstring, options.base_labels)
2018-01-16 20:55:52 -05:00
when :histogram
::Gitlab::Metrics.histogram(name, options.docstring, options.base_labels, options.buckets)
2018-01-16 20:55:52 -05:00
when :summary
raise NotImplementedError, "summary metrics are not currently supported"
else
raise ArgumentError, "uknown metric type #{type}"
end
2018-01-16 08:06:56 -05:00
end
end
end
end
end