prometheus_exporter-ext 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -0
- data/LICENSE.txt +21 -0
- data/README.md +496 -0
- data/Rakefile +12 -0
- data/lib/prometheus_exporter/ext/instrumentation/active_job.rb +236 -0
- data/lib/prometheus_exporter/ext/instrumentation/base_stats.rb +68 -0
- data/lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb +48 -0
- data/lib/prometheus_exporter/ext/instrumentation/proc_cpu.rb +35 -0
- data/lib/prometheus_exporter/ext/instrumentation/solid_queue.rb +32 -0
- data/lib/prometheus_exporter/ext/metric/counter_with_expire.rb +73 -0
- data/lib/prometheus_exporter/ext/metric/expiring.rb +37 -0
- data/lib/prometheus_exporter/ext/metric/gauge_with_expire.rb +108 -0
- data/lib/prometheus_exporter/ext/proc_self_stat.rb +74 -0
- data/lib/prometheus_exporter/ext/request_middleware.rb +24 -0
- data/lib/prometheus_exporter/ext/rspec/matchers.rb +43 -0
- data/lib/prometheus_exporter/ext/rspec/metric_matcher.rb +52 -0
- data/lib/prometheus_exporter/ext/rspec/send_metrics_matcher.rb +89 -0
- data/lib/prometheus_exporter/ext/rspec/test_client.rb +30 -0
- data/lib/prometheus_exporter/ext/rspec.rb +17 -0
- data/lib/prometheus_exporter/ext/server/active_job_collector.rb +23 -0
- data/lib/prometheus_exporter/ext/server/base_collector_methods.rb +103 -0
- data/lib/prometheus_exporter/ext/server/expired_stats_collector.rb +57 -0
- data/lib/prometheus_exporter/ext/server/proc_cpu_collector.rb +14 -0
- data/lib/prometheus_exporter/ext/server/singleton_web_server.rb +33 -0
- data/lib/prometheus_exporter/ext/server/solid_queue_collector.rb +17 -0
- data/lib/prometheus_exporter/ext/server/stats_collector.rb +71 -0
- data/lib/prometheus_exporter/ext/version.rb +7 -0
- data/lib/prometheus_exporter/ext.rb +3 -0
- metadata +33 -8
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
require 'active_support'
|
|
5
|
+
require 'active_support/notifications'
|
|
6
|
+
require 'active_support/core_ext/object/blank'
|
|
7
|
+
require 'active_support/core_ext/enumerable'
|
|
8
|
+
require 'active_support/core_ext/time'
|
|
9
|
+
require_relative 'base_stats'
|
|
10
|
+
|
|
11
|
+
module PrometheusExporter::Ext::Instrumentation
|
|
12
|
+
class ActiveJob < BaseStats
|
|
13
|
+
self.type = 'active_job'
|
|
14
|
+
|
|
15
|
+
QUEUE_IVAR = :@_prometheus_exporter_queue
|
|
16
|
+
|
|
17
|
+
ERROR_EVENTS = {
|
|
18
|
+
'enqueue_retry.active_job' => :collect_enqueue_retried, # job retried due to error
|
|
19
|
+
'discard.active_job' => :collect_discarded, # job discarded due to error
|
|
20
|
+
'retry_stopped.active_job' => :collect_retry_stopped # retries stopped after max attempts reached
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
# @see https://guides.rubyonrails.org/active_support_instrumentation.html#active-job
|
|
25
|
+
def subscribe(**)
|
|
26
|
+
unsubscribe # ensures we don't double subscribe
|
|
27
|
+
instance = new(**)
|
|
28
|
+
@subscribers = []
|
|
29
|
+
|
|
30
|
+
subscribe_enqueue_events(instance)
|
|
31
|
+
subscribe_perform_events(instance)
|
|
32
|
+
subscribe_error_events(instance)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def unsubscribe
|
|
36
|
+
return unless defined?(@subscribers)
|
|
37
|
+
|
|
38
|
+
@subscribers.each do |sub_id|
|
|
39
|
+
::ActiveSupport::Notifications.unsubscribe(sub_id)
|
|
40
|
+
end
|
|
41
|
+
@subscribers = []
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def subscribe_enqueue_events(instance)
|
|
47
|
+
# job enqueued, with or without wait
|
|
48
|
+
['enqueue.active_job', 'enqueue_at.active_job'].each do |name|
|
|
49
|
+
subscribe_event(name) do |event|
|
|
50
|
+
payload = event.payload
|
|
51
|
+
instance.collect_enqueued(
|
|
52
|
+
jobs: [payload[:job]], adapter: payload[:adapter], exception_object: payload[:exception_object]
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# multiple jobs enqueued at once
|
|
58
|
+
subscribe_event 'enqueue_all.active_job' do |event|
|
|
59
|
+
payload = event.payload
|
|
60
|
+
instance.collect_enqueued(
|
|
61
|
+
jobs: payload[:jobs],
|
|
62
|
+
adapter: payload[:adapter],
|
|
63
|
+
exception_object: payload[:exception_object],
|
|
64
|
+
enqueued_count: payload[:enqueued_count]
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def subscribe_perform_events(instance)
|
|
70
|
+
# job perform started
|
|
71
|
+
subscribe_event 'perform_start.active_job' do |event|
|
|
72
|
+
instance.collect_started(job: event.payload[:job], adapter: event.payload[:adapter])
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# job perform completed (both success and failure)
|
|
76
|
+
monotonic_subscribe_event 'perform.active_job' do |event|
|
|
77
|
+
payload = event.payload
|
|
78
|
+
instance.collect_performed(
|
|
79
|
+
job: payload[:job],
|
|
80
|
+
adapter: payload[:adapter],
|
|
81
|
+
exception: payload[:exception],
|
|
82
|
+
duration_ms: event.duration,
|
|
83
|
+
aborted: payload[:aborted]
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def subscribe_error_events(instance)
|
|
89
|
+
ERROR_EVENTS.each do |name, handler|
|
|
90
|
+
subscribe_event(name) do |event|
|
|
91
|
+
payload = event.payload
|
|
92
|
+
instance.public_send(handler, job: payload[:job], adapter: payload[:adapter], error: payload[:error])
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def subscribe_event(name, &)
|
|
98
|
+
sub_id = ::ActiveSupport::Notifications.subscribe(name, &)
|
|
99
|
+
@subscribers << sub_id
|
|
100
|
+
sub_id
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def monotonic_subscribe_event(name, &)
|
|
104
|
+
sub_id = ::ActiveSupport::Notifications.monotonic_subscribe(name, &)
|
|
105
|
+
@subscribers << sub_id
|
|
106
|
+
sub_id
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def collect_enqueued(jobs:, adapter:, exception_object: nil, enqueued_count: nil)
|
|
111
|
+
safe_collect do
|
|
112
|
+
jobs.zip(enqueued_flags(jobs, enqueued_count)).each do |job, enqueued|
|
|
113
|
+
if enqueued
|
|
114
|
+
# A retry re-enqueues a job that already ran; retried_total counts it.
|
|
115
|
+
next if job.executions.to_i.positive?
|
|
116
|
+
|
|
117
|
+
collect_data(
|
|
118
|
+
{
|
|
119
|
+
labels: job_labels(job, adapter, scheduled: job.scheduled_at.present? ? 'true' : nil),
|
|
120
|
+
enqueued_total: 1
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
else
|
|
124
|
+
enqueue_error = exception_object || job.enqueue_error
|
|
125
|
+
collect_data(
|
|
126
|
+
{
|
|
127
|
+
labels: job_labels(job, adapter, error: enqueue_error&.class&.name),
|
|
128
|
+
fail_enqueued_total: 1
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def collect_started(job:, adapter:, now: Time.current)
|
|
137
|
+
safe_collect do
|
|
138
|
+
job.instance_variable_set(QUEUE_IVAR, job.queue_name.to_s)
|
|
139
|
+
latency_seconds = calculate_latency(job.enqueued_at, job.scheduled_at, now)
|
|
140
|
+
if latency_seconds.present?
|
|
141
|
+
collect_data(
|
|
142
|
+
{
|
|
143
|
+
labels: job_labels(job, adapter, executions: job.executions.to_s),
|
|
144
|
+
latency_seconds:
|
|
145
|
+
}
|
|
146
|
+
)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def collect_performed(job:, adapter:, exception:, duration_ms:, aborted: false)
|
|
152
|
+
safe_collect do
|
|
153
|
+
# Rails < 8.1 retry_on(queue:) changes job.queue_name before perform.active_job is published.
|
|
154
|
+
queue = job.instance_variable_get(QUEUE_IVAR) || job.queue_name.to_s
|
|
155
|
+
success = exception.blank? && !aborted
|
|
156
|
+
collect_data(
|
|
157
|
+
{
|
|
158
|
+
labels: job_labels(job, adapter, queue:, executions: job.executions.to_s),
|
|
159
|
+
executed_total: 1,
|
|
160
|
+
success_total: success ? 1 : nil,
|
|
161
|
+
duration_seconds: ms2s(duration_ms)
|
|
162
|
+
}.compact
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
if exception.present?
|
|
166
|
+
collect_data(
|
|
167
|
+
{
|
|
168
|
+
labels: job_labels(job, adapter, queue:, executions: job.executions.to_s, error: exception.first.to_s),
|
|
169
|
+
failed_total: 1
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def collect_enqueue_retried(job:, adapter:, error:)
|
|
177
|
+
safe_collect do
|
|
178
|
+
# Rails publishes enqueue_retry.active_job even when the re-enqueue failed.
|
|
179
|
+
collect_data({ labels: error_labels(job, adapter, error), retried_total: 1 }) if job.successfully_enqueued?
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def collect_discarded(job:, adapter:, error:)
|
|
184
|
+
safe_collect { collect_data({ labels: error_labels(job, adapter, error), discarded_total: 1 }) }
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def collect_retry_stopped(job:, adapter:, error:)
|
|
188
|
+
safe_collect { collect_data({ labels: error_labels(job, adapter, error), retry_stopped_total: 1 }) }
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
private
|
|
192
|
+
|
|
193
|
+
# Adapters implementing enqueue_all (e.g. Sidekiq) may report only enqueued_count without setting
|
|
194
|
+
# successfully_enqueued on each job. Then the totals follow enqueued_count, but which jobs failed is unknown,
|
|
195
|
+
# so the first enqueued_count jobs are counted as enqueued.
|
|
196
|
+
def enqueued_flags(jobs, enqueued_count)
|
|
197
|
+
flags = jobs.map(&:successfully_enqueued?)
|
|
198
|
+
return flags if enqueued_count.nil? || flags.any?
|
|
199
|
+
|
|
200
|
+
count = enqueued_count.to_i
|
|
201
|
+
jobs.each_index.map { |index| index < count }
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def error_labels(job, adapter, error)
|
|
205
|
+
job_labels(job, adapter, executions: job.executions.to_s, error: error&.class&.name)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def job_labels(job, adapter, queue: job.queue_name.to_s, **extra)
|
|
209
|
+
{
|
|
210
|
+
job_class: job.class.to_s,
|
|
211
|
+
adapter: adapter.class.name,
|
|
212
|
+
queue:,
|
|
213
|
+
**extra
|
|
214
|
+
}.compact_blank
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def ms2s(milliseconds)
|
|
218
|
+
milliseconds = milliseconds.to_f
|
|
219
|
+
return 0 if milliseconds.zero?
|
|
220
|
+
|
|
221
|
+
(milliseconds / 1000.0).round(3)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# A scheduled job becomes runnable at scheduled_at, so the delay it was asked to wait is not latency.
|
|
225
|
+
def calculate_latency(enqueued_at, scheduled_at, now)
|
|
226
|
+
runnable_at = [enqueued_at, scheduled_at].compact_blank.map { |time| parse_time(time) }.max
|
|
227
|
+
return nil if runnable_at.nil?
|
|
228
|
+
|
|
229
|
+
[now - runnable_at, 0].max
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def parse_time(time)
|
|
233
|
+
time.is_a?(String) ? Time.parse(time).utc : time
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'prometheus_exporter/client'
|
|
4
|
+
require_relative '../../ext'
|
|
5
|
+
|
|
6
|
+
module PrometheusExporter::Ext::Instrumentation
|
|
7
|
+
class BaseStats
|
|
8
|
+
class << self
|
|
9
|
+
attr_accessor :type
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# @param on_error [#call, nil] receives the exception raised inside #safe_collect.
|
|
13
|
+
# Defaults to printing a warning. If the handler raises, both errors are printed as warnings.
|
|
14
|
+
def initialize(client: PrometheusExporter::Client.default, labels: {}, on_error: nil)
|
|
15
|
+
@labels = labels.transform_keys(&:to_sym)
|
|
16
|
+
@client = client
|
|
17
|
+
@on_error = on_error || method(:warn_error)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def type
|
|
21
|
+
self.class.type
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def collect
|
|
25
|
+
raise NotImplementedError
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
# Runs the block and passes any StandardError to the on_error handler instead of raising it.
|
|
31
|
+
def safe_collect
|
|
32
|
+
yield
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
report_error(e)
|
|
35
|
+
nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# ActiveSupport::Notifications re-raises subscriber errors into the app, so a failing handler must not raise.
|
|
39
|
+
def report_error(error)
|
|
40
|
+
@on_error.call(error)
|
|
41
|
+
rescue StandardError => e
|
|
42
|
+
warn_error(error)
|
|
43
|
+
warn_error(e)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def warn_error(error)
|
|
47
|
+
warn "Failed to send metrics Prometheus #{self.class.name}: #{error.class}: #{error.message}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @param data [Array,Hash]
|
|
51
|
+
def collect_data(data)
|
|
52
|
+
data_list = data.is_a?(Array) ? data : [data]
|
|
53
|
+
metrics = data_list.map { |data_item| build_metric(data_item) }
|
|
54
|
+
metrics.map { |metric| @client.send_json(metric) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @param data [Hash]
|
|
58
|
+
# @return [Hash]
|
|
59
|
+
def build_metric(data)
|
|
60
|
+
metric = data.transform_keys(&:to_sym)
|
|
61
|
+
metric[:type] = type
|
|
62
|
+
metric[:labels] ||= {}
|
|
63
|
+
metric[:labels].transform_keys!(&:to_sym)
|
|
64
|
+
metric[:labels].merge!(@labels)
|
|
65
|
+
metric
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base_stats'
|
|
4
|
+
|
|
5
|
+
module PrometheusExporter::Ext::Instrumentation
|
|
6
|
+
class PeriodicStats < BaseStats
|
|
7
|
+
class << self
|
|
8
|
+
def start(frequency: 30, client: PrometheusExporter::Client.default, **)
|
|
9
|
+
raise ArgumentError, 'Expected frequency to be a number' unless frequency.is_a?(Numeric)
|
|
10
|
+
raise ArgumentError, 'Expected frequency to be a positive number' if frequency.negative?
|
|
11
|
+
|
|
12
|
+
klass = self
|
|
13
|
+
stop
|
|
14
|
+
instrumentation = new(client:, **)
|
|
15
|
+
@stop_thread = false
|
|
16
|
+
|
|
17
|
+
@thread = Thread.new do
|
|
18
|
+
until @stop_thread
|
|
19
|
+
begin
|
|
20
|
+
instrumentation.collect
|
|
21
|
+
rescue StandardError => e
|
|
22
|
+
client.logger.error("#{klass} Prometheus Exporter Failed To Collect Stats")
|
|
23
|
+
client.logger.error("#{e.class} #{e.backtrace&.join("\n")}")
|
|
24
|
+
ensure
|
|
25
|
+
sleep frequency
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def started?
|
|
32
|
+
!!@thread&.alive?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def stop
|
|
36
|
+
# to avoid a warning
|
|
37
|
+
@thread = nil unless defined?(@thread)
|
|
38
|
+
|
|
39
|
+
if @thread&.alive?
|
|
40
|
+
@stop_thread = true
|
|
41
|
+
@thread.wakeup
|
|
42
|
+
@thread.join
|
|
43
|
+
end
|
|
44
|
+
@thread = nil
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'periodic_stats'
|
|
4
|
+
require_relative '../proc_self_stat'
|
|
5
|
+
|
|
6
|
+
module PrometheusExporter::Ext::Instrumentation
|
|
7
|
+
class ProcCpu < PeriodicStats
|
|
8
|
+
self.type = 'proc_cpu'
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
def start(type:, labels: {}, **)
|
|
12
|
+
labels = labels.merge(type:)
|
|
13
|
+
|
|
14
|
+
super(labels:, **)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(...)
|
|
19
|
+
@last_cpu_time = 0.0
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def collect
|
|
24
|
+
stat = PrometheusExporter::Ext::ProcSelfStat.get
|
|
25
|
+
collect_data(
|
|
26
|
+
labels: {
|
|
27
|
+
pid: stat.pid,
|
|
28
|
+
hostname: ::PrometheusExporter.hostname
|
|
29
|
+
},
|
|
30
|
+
usage_seconds_total: stat.cpu_time - @last_cpu_time
|
|
31
|
+
)
|
|
32
|
+
@last_cpu_time = stat.cpu_time
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'periodic_stats'
|
|
4
|
+
|
|
5
|
+
module PrometheusExporter::Ext::Instrumentation
|
|
6
|
+
# Reports the number of Solid Queue jobs in each execution table.
|
|
7
|
+
# Requires the solid_queue gem to be loaded by the host application.
|
|
8
|
+
# Processed by PrometheusExporter::Ext::Server::SolidQueueCollector.
|
|
9
|
+
class SolidQueue < PeriodicStats
|
|
10
|
+
self.type = 'solid_queue'
|
|
11
|
+
|
|
12
|
+
def collect
|
|
13
|
+
safe_collect do
|
|
14
|
+
collect_data(queue_stats)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def queue_stats
|
|
21
|
+
::SolidQueue::Record.connection_pool.with_connection do
|
|
22
|
+
{
|
|
23
|
+
ready_count: ::SolidQueue::ReadyExecution.count,
|
|
24
|
+
scheduled_count: ::SolidQueue::ScheduledExecution.count,
|
|
25
|
+
claimed_count: ::SolidQueue::ClaimedExecution.count,
|
|
26
|
+
blocked_count: ::SolidQueue::BlockedExecution.count,
|
|
27
|
+
failed_count: ::SolidQueue::FailedExecution.count
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'prometheus_exporter/metric'
|
|
4
|
+
require_relative '../../ext'
|
|
5
|
+
require_relative 'expiring'
|
|
6
|
+
|
|
7
|
+
module PrometheusExporter::Ext::Metric
|
|
8
|
+
# Counter that drops a label set not updated for `ttl` seconds. Same removing strategy as GaugeWithExpire.
|
|
9
|
+
class CounterWithExpire < PrometheusExporter::Metric::Counter
|
|
10
|
+
include Expiring
|
|
11
|
+
|
|
12
|
+
def initialize(name, help, opts = {})
|
|
13
|
+
super(name, help)
|
|
14
|
+
init_ttl(opts)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def reset!
|
|
18
|
+
@expiration_times = {}
|
|
19
|
+
super
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def metric_text
|
|
23
|
+
expire
|
|
24
|
+
super
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_h
|
|
28
|
+
expire
|
|
29
|
+
super
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def remove(labels)
|
|
33
|
+
expiration_times.delete(labels)
|
|
34
|
+
super
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def observe(increment = 1, labels = {}) = with_expiry(labels) { super }
|
|
38
|
+
|
|
39
|
+
def increment(labels = {}, value = 1) = with_expiry(labels) { super }
|
|
40
|
+
|
|
41
|
+
def decrement(labels = {}, value = 1) = with_expiry(labels) { super }
|
|
42
|
+
|
|
43
|
+
def reset(labels = {}, value = 0) = with_expiry(labels) { super }
|
|
44
|
+
|
|
45
|
+
def expire
|
|
46
|
+
now = now_time
|
|
47
|
+
expiration_times.delete_if do |labels, expired_at|
|
|
48
|
+
next false if expired_at >= now
|
|
49
|
+
|
|
50
|
+
data.delete(labels)
|
|
51
|
+
true
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
# A series that expired but was not read yet must restart from zero.
|
|
58
|
+
def with_expiry(labels)
|
|
59
|
+
drop_if_stale(labels)
|
|
60
|
+
result = yield
|
|
61
|
+
expiration_times[labels] = now_time + ttl
|
|
62
|
+
result
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def drop_if_stale(labels)
|
|
66
|
+
expired_at = expiration_times[labels]
|
|
67
|
+
return if expired_at.nil? || expired_at >= now_time
|
|
68
|
+
|
|
69
|
+
data.delete(labels)
|
|
70
|
+
expiration_times.delete(labels)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../ext'
|
|
4
|
+
|
|
5
|
+
module PrometheusExporter::Ext::Metric
|
|
6
|
+
# Shared TTL handling for metrics that drop label sets not updated for `ttl` seconds.
|
|
7
|
+
module Expiring
|
|
8
|
+
class << self
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
def included(klass)
|
|
12
|
+
super
|
|
13
|
+
klass.extend ClassMethods
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
def default_ttl
|
|
19
|
+
60
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :ttl, :expiration_times
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def init_ttl(opts)
|
|
28
|
+
@ttl = opts[:ttl] || self.class.default_ttl
|
|
29
|
+
raise ArgumentError, ':ttl must be numeric' unless ttl.is_a?(Numeric)
|
|
30
|
+
raise ArgumentError, ":ttl must be greater than zero: #{ttl.inspect}" unless ttl.positive?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def now_time
|
|
34
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'prometheus_exporter/metric'
|
|
4
|
+
require_relative '../../ext'
|
|
5
|
+
require_relative 'expiring'
|
|
6
|
+
|
|
7
|
+
module PrometheusExporter::Ext::Metric
|
|
8
|
+
class GaugeWithExpire < PrometheusExporter::Metric::Gauge
|
|
9
|
+
include Expiring
|
|
10
|
+
|
|
11
|
+
NULLIFY_STRATEGY_UPDATE = ->(labels) { expiration_times[labels] = now_time + ttl }.freeze
|
|
12
|
+
NULLIFY_STRATEGY_EXPIRE = ->(labels) { data.delete(labels) }.freeze
|
|
13
|
+
ZEROING_STRATEGY_UPDATE = ->(labels) do
|
|
14
|
+
if data[labels].zero?
|
|
15
|
+
expiration_times.delete(labels)
|
|
16
|
+
else
|
|
17
|
+
expiration_times[labels] = now_time + ttl
|
|
18
|
+
end
|
|
19
|
+
end.freeze
|
|
20
|
+
ZEROING_STRATEGY_EXPIRE = ->(labels) { data[labels] = 0 }.freeze
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
# @return [Hash]
|
|
24
|
+
# key - strategy name
|
|
25
|
+
# value [Hash] with keys: :on_update, :on_expire
|
|
26
|
+
# :on_update [Proc] yieldparam labels [Hash] - updates expiration_times after data was updated (instance exec)
|
|
27
|
+
# :on_expire [Proc] yieldparam labels [Hash] - updates data after expiration_times was expired (instance exec)
|
|
28
|
+
def strategies
|
|
29
|
+
{
|
|
30
|
+
removing: { on_update: NULLIFY_STRATEGY_UPDATE, on_expire: NULLIFY_STRATEGY_EXPIRE },
|
|
31
|
+
zeroing: { on_update: ZEROING_STRATEGY_UPDATE, on_expire: ZEROING_STRATEGY_EXPIRE }
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def initialize(name, help, opts = {})
|
|
37
|
+
super(name, help)
|
|
38
|
+
init_ttl(opts)
|
|
39
|
+
|
|
40
|
+
@strategy = self.class.strategies.fetch(opts[:strategy] || :removing) do
|
|
41
|
+
raise ArgumentError, "Unknown strategy: #{opts[:strategy].inspect}"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def reset!
|
|
46
|
+
@expiration_times = {}
|
|
47
|
+
super
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def metric_text
|
|
51
|
+
expire
|
|
52
|
+
super
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def remove(labels)
|
|
56
|
+
result = super
|
|
57
|
+
remove_expired_at(labels)
|
|
58
|
+
result
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def observe(value, labels = {})
|
|
62
|
+
result = super
|
|
63
|
+
value.nil? ? remove_expired_at(labels) : update_expired_at(labels)
|
|
64
|
+
result
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def increment(labels = {}, value = 1)
|
|
68
|
+
result = super
|
|
69
|
+
update_expired_at(labels)
|
|
70
|
+
result
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def decrement(labels = {}, value = 1)
|
|
74
|
+
result = super
|
|
75
|
+
update_expired_at(labels)
|
|
76
|
+
result
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def expire
|
|
80
|
+
now = now_time
|
|
81
|
+
expiration_times.each do |labels, expired_at|
|
|
82
|
+
if expired_at < now
|
|
83
|
+
remove_data_when_expired(labels)
|
|
84
|
+
remove_expired_at(labels)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def to_h
|
|
90
|
+
expire
|
|
91
|
+
super
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def remove_data_when_expired(labels)
|
|
97
|
+
instance_exec(labels, &@strategy[:on_expire])
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def update_expired_at(labels)
|
|
101
|
+
instance_exec(labels, &@strategy[:on_update])
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def remove_expired_at(labels)
|
|
105
|
+
expiration_times.delete(labels)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|