blob: abc41a1c4766eac4aadcdf3998899325fb0e5bcf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# frozen_string_literal: true
module PrometheusAdapter
extend ActiveSupport::Concern
included do
include ReactiveCaching
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
# This is a light-weight check if a prometheus client is properly configured.
def configured?
raise NotImplemented
end
# This is a heavy-weight check if a prometheus is properly configured and accessible from GitLab.
# This actually sends a request to an external service and often it could take a long time,
# Please consider using `configured?` instead if the process is running on unicorn/puma threads.
def can_query?
prometheus_client.present?
end
def query(query_name, *args)
return unless can_query?
query_class = query_klass_for(query_name)
query_args = build_query_args(*args)
with_reactive_cache(query_class.name, *query_args, &query_class.method(:transform_reactive_result))
end
# Cache metrics for specific environment
def calculate_reactive_cache(query_class_name, *args)
return unless prometheus_client
data = Object.const_get(query_class_name, false).new(prometheus_client).query(*args)
{
success: true,
data: data,
last_update: Time.now.utc
}
rescue Gitlab::PrometheusClient::Error => err
{ success: false, result: err.message }
end
def query_klass_for(query_name)
Gitlab::Prometheus::Queries.const_get("#{query_name.to_s.classify}Query", false)
end
def build_query_args(*args)
args.map { |arg| arg.respond_to?(:id) ? arg.id : arg }
end
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
end
end
|