debian-mirror-gitlab/app/models/concerns/prometheus_adapter.rb

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

85 lines
2.5 KiB
Ruby
Raw Normal View History

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
2020-11-24 15:15:51 +05:30
# 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-03-27 19:54:05 +05:30
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-05-24 23:13:21 +05:30
self.reactive_cache_work_type = :external_dependency
2018-03-27 19:54:05 +05:30
def prometheus_client
raise NotImplementedError
end
2020-11-24 15:15:51 +05:30
def prometheus_client_default_options
{
timeout: DEFAULT_PROMETHEUS_REQUEST_TIMEOUT_SEC
}
end
2021-06-08 01:23:25 +05:30
# Overridden in app/models/clusters/applications/prometheus.rb
def managed_prometheus?
false
end
2020-01-01 13:55:28 +05:30
# This is a light-weight check if a prometheus client is properly configured.
def configured?
2021-06-08 01:23:25 +05:30
raise NotImplementedError
2020-01-01 13:55:28 +05:30
end
2020-03-13 15:44:24 +05:30
# This is a heavy-weight check if a prometheus is properly configured and accessible from GitLab.
2020-01-01 13:55:28 +05:30
# This actually sends a request to an external service and often it could take a long time,
2021-09-04 01:27:46 +05:30
# Please consider using `configured?` instead if the process is running on Puma threads.
2018-03-27 19:54:05 +05:30
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,
2020-06-23 00:09:42 +05:30
last_update: Time.current.utc
2018-03-27 19:54:05 +05:30
}
2022-08-27 11:52:29 +05:30
rescue Gitlab::PrometheusClient::Error => e
{ success: false, result: e.message }
2018-03-27 19:54:05 +05:30
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
2020-03-13 15:44:24 +05:30
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-03-27 19:54:05 +05:30
end
end