2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
module PrometheusAdapter
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2020-08-24 20:10:16 -04:00
|
|
|
# We should choose more conservative timeouts, but some queries we run are now busting our
|
|
|
|
# default timeouts, which are stricter. We should make those queries faster instead.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/232786
|
|
|
|
DEFAULT_PROMETHEUS_REQUEST_TIMEOUT_SEC = 60.seconds
|
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
included do
|
|
|
|
include ReactiveCaching
|
|
|
|
|
|
|
|
self.reactive_cache_lease_timeout = 30.seconds
|
|
|
|
self.reactive_cache_refresh_interval = 30.seconds
|
|
|
|
self.reactive_cache_lifetime = 1.minute
|
2020-04-24 17:09:48 -04:00
|
|
|
self.reactive_cache_work_type = :external_dependency
|
2018-02-23 15:33:33 -05:00
|
|
|
|
|
|
|
def prometheus_client
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2020-08-24 20:10:16 -04:00
|
|
|
def prometheus_client_default_options
|
|
|
|
{
|
|
|
|
timeout: DEFAULT_PROMETHEUS_REQUEST_TIMEOUT_SEC
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
# Overridden in app/models/clusters/applications/prometheus.rb
|
|
|
|
def managed_prometheus?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-12-05 22:08:02 -05:00
|
|
|
# This is a light-weight check if a prometheus client is properly configured.
|
|
|
|
def configured?
|
2021-05-04 20:10:41 -04:00
|
|
|
raise NotImplementedError
|
2019-12-05 22:08:02 -05:00
|
|
|
end
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
# This is a heavy-weight check if a prometheus is properly configured and accessible from GitLab.
|
2019-12-05 22:08:02 -05:00
|
|
|
# This actually sends a request to an external service and often it could take a long time,
|
2021-05-19 08:10:33 -04:00
|
|
|
# Please consider using `configured?` instead if the process is running on Puma threads.
|
2018-02-23 15:33:33 -05:00
|
|
|
def can_query?
|
|
|
|
prometheus_client.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def query(query_name, *args)
|
|
|
|
return unless can_query?
|
|
|
|
|
2018-06-25 05:57:29 -04:00
|
|
|
query_class = query_klass_for(query_name)
|
|
|
|
query_args = build_query_args(*args)
|
2018-02-23 15:33:33 -05:00
|
|
|
|
2018-06-25 05:57:29 -04:00
|
|
|
with_reactive_cache(query_class.name, *query_args, &query_class.method(:transform_reactive_result))
|
2018-02-23 15:33:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Cache metrics for specific environment
|
|
|
|
def calculate_reactive_cache(query_class_name, *args)
|
|
|
|
return unless prometheus_client
|
|
|
|
|
2019-08-06 22:42:20 -04:00
|
|
|
data = Object.const_get(query_class_name, false).new(prometheus_client).query(*args)
|
2018-02-23 15:33:33 -05:00
|
|
|
{
|
|
|
|
success: true,
|
|
|
|
data: data,
|
2020-05-22 05:08:09 -04:00
|
|
|
last_update: Time.current.utc
|
2018-02-23 15:33:33 -05:00
|
|
|
}
|
|
|
|
rescue Gitlab::PrometheusClient::Error => err
|
|
|
|
{ success: false, result: err.message }
|
|
|
|
end
|
2018-06-25 05:57:29 -04:00
|
|
|
|
|
|
|
def query_klass_for(query_name)
|
2019-10-15 23:06:12 -04:00
|
|
|
Gitlab::Prometheus::Queries.const_get("#{query_name.to_s.classify}Query", false)
|
2018-06-25 05:57:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_query_args(*args)
|
2019-03-15 07:20:59 -04:00
|
|
|
args.map { |arg| arg.respond_to?(:id) ? arg.id : arg }
|
2018-06-25 05:57:29 -04:00
|
|
|
end
|
2020-02-06 13:08:54 -05:00
|
|
|
|
|
|
|
def clear_prometheus_reactive_cache!(query_name, *args)
|
|
|
|
query_class = query_klass_for(query_name)
|
|
|
|
query_args = build_query_args(*args)
|
|
|
|
|
|
|
|
clear_reactive_cache!(query_class.name, *query_args)
|
|
|
|
end
|
2018-02-23 15:33:33 -05:00
|
|
|
end
|
|
|
|
end
|