2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
module PrometheusAdapter
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
include ReactiveCaching
|
2019-12-04 20:38:33 +05:30
|
|
|
# We can't prepend outside of this model due to the use of `included`, so this must stay here.
|
|
|
|
prepend_if_ee('EE::PrometheusAdapter') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
self.reactive_cache_lease_timeout = 30.seconds
|
|
|
|
self.reactive_cache_refresh_interval = 30.seconds
|
|
|
|
self.reactive_cache_lifetime = 1.minute
|
|
|
|
|
|
|
|
def prometheus_client
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_query?
|
|
|
|
prometheus_client.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def query(query_name, *args)
|
|
|
|
return unless can_query?
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
query_class = query_klass_for(query_name)
|
|
|
|
query_args = build_query_args(*args)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
with_reactive_cache(query_class.name, *query_args, &query_class.method(:transform_reactive_result))
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Cache metrics for specific environment
|
|
|
|
def calculate_reactive_cache(query_class_name, *args)
|
|
|
|
return unless prometheus_client
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
data = Object.const_get(query_class_name, false).new(prometheus_client).query(*args)
|
2018-03-27 19:54:05 +05:30
|
|
|
{
|
|
|
|
success: true,
|
|
|
|
data: data,
|
|
|
|
last_update: Time.now.utc
|
|
|
|
}
|
|
|
|
rescue Gitlab::PrometheusClient::Error => err
|
|
|
|
{ success: false, result: err.message }
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
def query_klass_for(query_name)
|
2019-12-21 20:55:43 +05:30
|
|
|
Gitlab::Prometheus::Queries.const_get("#{query_name.to_s.classify}Query", false)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def build_query_args(*args)
|
2019-07-07 11:18:12 +05:30
|
|
|
args.map { |arg| arg.respond_to?(:id) ? arg.id : arg }
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|