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

122 lines
3.8 KiB
Ruby
Raw Normal View History

module Gitlab
module Metrics
module Concern
extend ActiveSupport::Concern
MUTEX = Mutex.new
2018-01-16 13:06:56 +00:00
class_methods do
2018-01-16 14:47:07 +00:00
def reload_metric!(name)
@_metrics_provider_cache&.delete(name)
2018-01-16 14:47:07 +00:00
end
private
2018-01-16 13:06:56 +00:00
def define_metric(type, name, opts = {}, &block)
if instance_methods(false).include?(name)
2018-01-12 23:20:27 +00:00
raise ArgumentError, "metrics method #{name} already exists"
end
2018-01-16 14:47:07 +00:00
define_singleton_method(name) do
2018-01-16 13:06:56 +00:00
# avoid unnecessary method call to speed up metric access
metric = @_metrics_provider_cache&.[](name)
return metric if metric
2018-01-16 13:06:56 +00:00
fetch_metric(type, name, opts, &block)
end
end
def fetch_metric(type, name, opts = {}, &block)
# avoid synchronization to speed up metrics access
metric = @_metrics_provider_cache&.[](name)
return metric if metric
2018-01-16 13:06:56 +00:00
options = MetricOptions.new(opts)
options.evaluate(&block)
MUTEX.synchronize do
@_metrics_provider_cache ||= {}
2018-01-17 01:55:52 +00:00
@_metrics_provider_cache[name] ||= build_metric!(type, name, options)
2018-01-16 13:06:56 +00:00
end
2018-01-17 01:55:52 +00:00
@_metrics_provider_cache[name]
2018-01-16 13:06:56 +00:00
end
def build_metric!(type, name, options)
unless options.with_feature.nil? || Feature.get(options.with_feature).enabled?
return NullMetric.new
end
case type
2018-01-17 01:55:52 +00:00
when :gauge
Gitlab::Metrics.gauge(name, options.docstring, options.base_labels, options.multiprocess_mode)
when :counter
Gitlab::Metrics.counter(name, options.docstring, options.base_labels)
when :histogram
Gitlab::Metrics.histogram(name, options.docstring, options.base_labels, options.buckets)
when :summary
raise NotImplementedError, "summary metrics are not currently supported"
else
raise ArgumentError, "uknown metric type #{type}"
end
2018-01-16 13:06:56 +00:00
end
2018-01-16 13:06:56 +00:00
# Fetch and/or initialize counter metric
# @param [Symbol] name
# @param [Hash] opts
def fetch_counter(name, opts = {}, &block)
fetch_metric(:counter, name, opts, &block)
2018-01-16 13:06:56 +00:00
end
2018-01-16 13:06:56 +00:00
# DFetch and/or initialize gauge metric
# @param [Symbol] name
# @param [Hash] opts
def fetch_gauge(name, opts = {}, &block)
fetch_metric(:counter, name, opts, &block)
2018-01-16 13:06:56 +00:00
end
2018-01-15 21:06:40 +00:00
2018-01-16 13:06:56 +00:00
# Fetch and/or initialize histogram metric
# @param [Symbol] name
# @param [Hash] opts
def fetch_histogram(name, opts = {}, &block)
fetch_metric(:histogram, name, opts, &block)
2018-01-16 13:06:56 +00:00
end
2018-01-16 13:06:56 +00:00
# Fetch and/or initialize summary metric
# @param [Symbol] name
# @param [Hash] opts
def fetch_summary(name, opts = {}, &block)
fetch_metric(:summary, name, opts, &block)
end
2018-01-16 13:06:56 +00:00
# Define metric accessor method for a Counter
# @param [Symbol] name
2018-01-16 13:06:56 +00:00
# @param [Hash] opts
def define_counter(name, opts = {}, &block)
define_metric(:counter, name, opts, &block)
end
2018-01-16 13:06:56 +00:00
# Define metric accessor method for a Gauge
# @param [Symbol] name
2018-01-16 13:06:56 +00:00
# @param [Hash] opts
def define_gauge(name, opts = {}, &block)
define_metric(:counter, name, opts, &block)
end
2018-01-16 13:06:56 +00:00
# Define metric accessor method for a Histogram
# @param [Symbol] name
2018-01-16 13:06:56 +00:00
# @param [Hash] opts
def define_histogram(name, opts = {}, &block)
define_metric(:histogram, name, opts, &block)
end
2018-01-16 13:06:56 +00:00
# Define metric accessor method for a Summary
# @param [Symbol] name
# @param [Hash] opts
def define_summary(name, opts = {}, &block)
define_metric(:summary, name, opts, &block)
end
end
end
end
end