2018-01-12 16:55:53 +00:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Concern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
class_methods do
|
|
|
|
private
|
|
|
|
|
|
|
|
def metrics_provider(type, name, docstring, options = {})
|
|
|
|
@@_metrics_provider_mutex ||= Mutex.new
|
|
|
|
|
|
|
|
if instance_methods(false).include?(name)
|
2018-01-12 23:20:27 +00:00
|
|
|
raise ArgumentError, "metrics method #{name} already exists"
|
2018-01-12 16:55:53 +00:00
|
|
|
end
|
|
|
|
options[:base_labels] ||= {}
|
|
|
|
|
|
|
|
args = [name.inspect, %{"#{docstring}"}, options[:base_labels].inspect]
|
|
|
|
|
|
|
|
case type
|
|
|
|
when :gauge
|
|
|
|
options[:multiprocess_mode] ||= :all
|
|
|
|
args << options[:multiprocess_mode].inspect
|
|
|
|
when :histogram
|
|
|
|
options[:buckets] ||= ::Prometheus::Client::Histogram::DEFAULT_BUCKETS
|
|
|
|
args << options[:buckets].inspect
|
|
|
|
end
|
|
|
|
|
2018-01-12 23:20:27 +00:00
|
|
|
metric_fetching_code = %{Gitlab::Metrics.#{type}(#{args.join(', ')})}
|
2018-01-12 16:55:53 +00:00
|
|
|
|
|
|
|
# optionally wrap in feature
|
2018-01-12 23:20:27 +00:00
|
|
|
if options[:with_feature].is_a?(Symbol)
|
|
|
|
metric_fetching_code = <<-FETCH.strip_heredoc
|
|
|
|
if Feature.get(#{options[:with_feature].inspect}).enabled?
|
|
|
|
#{metric_fetching_code}
|
|
|
|
else
|
|
|
|
Gitlab::Metrics::NullMetric.new
|
|
|
|
end
|
|
|
|
FETCH
|
|
|
|
end
|
2018-01-12 16:55:53 +00:00
|
|
|
|
|
|
|
method_code, line = <<-METRIC, __LINE__ + 1
|
2018-01-12 23:20:27 +00:00
|
|
|
@@_metric_provider_cached_#{name} = nil
|
2018-01-12 16:55:53 +00:00
|
|
|
def #{name}
|
2018-01-12 23:20:27 +00:00
|
|
|
return @@_metric_provider_cached_#{name} if @@_metric_provider_cached_#{name}
|
2018-01-12 16:55:53 +00:00
|
|
|
|
|
|
|
@@_metrics_provider_mutex.synchronize do
|
2018-01-12 23:20:27 +00:00
|
|
|
@@_metric_provider_cached_#{name} ||= #{metric_fetching_code}
|
2018-01-12 16:55:53 +00:00
|
|
|
end
|
|
|
|
end
|
2018-01-15 21:06:40 +00:00
|
|
|
|
|
|
|
def reload_#{name}!
|
|
|
|
@@_metric_provider_cached_#{name} = nil
|
|
|
|
end
|
2018-01-12 16:55:53 +00:00
|
|
|
METRIC
|
|
|
|
|
2018-01-12 23:20:27 +00:00
|
|
|
instance_eval(method_code, __FILE__, line)
|
2018-01-12 16:55:53 +00:00
|
|
|
module_eval(method_code, __FILE__, line)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Declare a Counter
|
|
|
|
# @param [Symbol] name
|
|
|
|
# @param [String] docstring
|
2018-01-12 23:20:27 +00:00
|
|
|
# @param [Hash] options
|
|
|
|
def counter(name, docstring, options = {})
|
2018-01-12 16:55:53 +00:00
|
|
|
metrics_provider(:counter, name, docstring, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Declare a Gauge
|
|
|
|
# @param [Symbol] name
|
|
|
|
# @param [String] docstring
|
2018-01-12 23:20:27 +00:00
|
|
|
# @param [Hash] options
|
|
|
|
def gauge(name, docstring, options = {})
|
|
|
|
metrics_provider(:counter, name, docstring, options)
|
2018-01-12 16:55:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Declare a Histograam
|
|
|
|
# @param [Symbol] name
|
|
|
|
# @param [String] docstring
|
2018-01-12 23:20:27 +00:00
|
|
|
# @param [Hash] options
|
|
|
|
def histogram(name, docstring, options = {})
|
|
|
|
metrics_provider(:histogram, name, docstring, options)
|
2018-01-12 16:55:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def summary(*args)
|
|
|
|
raise NotImplementedError, "summary metrics are not currently supported"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|