prometheus_exporter-ext 0.3.2 → 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 +10 -0
- data/README.md +314 -91
- data/lib/prometheus_exporter/ext/instrumentation/active_job.rb +236 -0
- data/lib/prometheus_exporter/ext/instrumentation/base_stats.rb +24 -1
- 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 +4 -13
- data/lib/prometheus_exporter/ext/request_middleware.rb +24 -0
- data/lib/prometheus_exporter/ext/rspec/matchers.rb +5 -0
- data/lib/prometheus_exporter/ext/server/active_job_collector.rb +23 -0
- data/lib/prometheus_exporter/ext/server/proc_cpu_collector.rb +3 -1
- data/lib/prometheus_exporter/ext/server/solid_queue_collector.rb +17 -0
- data/lib/prometheus_exporter/ext/server/stats_collector.rb +27 -1
- data/lib/prometheus_exporter/ext/version.rb +1 -1
- metadata +11 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f4890381a295ba6754ffb2cfe19221e5e6b28ad3c088b0b63f0705a01b74d3f8
|
|
4
|
+
data.tar.gz: 2f264310ebd39c4776b916d948164c350cbf9763dd4d229f6ad8a8a225f7cc48
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 547da7feaea9b6efc543a08aba6e7db492c5fb31d128e8450eb9fc9f3375ba7ac6be4aa835f8a6ffb009d48b3e1fbd5e080b0c0a1e695c999d567dd78a5e467a
|
|
7
|
+
data.tar.gz: '004518e397c14f41cde2427a5b1c485b8de768d7b5b3f69247e28ea7f74daede452ccc4b3e73a8a25970ac9f999fab5e0527353cd542977fa182ac998a75dc0a'
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.4.0] - 2026-11-25
|
|
4
|
+
- drop EOL Ruby 3.2, test on Ruby 3.3 and 3.4, raise `required_ruby_version` to `>= 3.3.0`
|
|
5
|
+
- add `PrometheusExporter::Ext::Metric::CounterWithExpire`, `register_counter_with_expire` and the `a_counter_with_expire_metric` RSpec matcher
|
|
6
|
+
- `ProcCpuCollector` drops series not updated for 300 seconds (set `ProcCpuCollector.ttl` to change it); specs that matched it with `a_counter_metric` need `a_counter_with_expire_metric`
|
|
7
|
+
- add `on_error:` option and `safe_collect` helper to `BaseStats`; an `on_error` handler that raises falls back to a warning
|
|
8
|
+
- add a collector-wide `ttl` setting to `StatsCollector` for expiring metrics registered without `ttl:`
|
|
9
|
+
- add `Instrumentation::ActiveJob` and `Server::ActiveJobCollector`; `enqueued_total` leaves out retry re-enqueues, which `retried_total` counts
|
|
10
|
+
- add `Instrumentation::SolidQueue` and `Server::SolidQueueCollector`
|
|
11
|
+
- add `PrometheusExporter::Ext::RequestMiddleware` that adds static labels to request metrics
|
|
12
|
+
|
|
3
13
|
## [0.3.2] - 2026-06-11
|
|
4
14
|
- fix gemspec missing `spec.files` so the published gem actually ships its source files (0.3.1 and earlier shipped empty)
|
|
5
15
|
|
data/README.md
CHANGED
|
@@ -1,14 +1,48 @@
|
|
|
1
1
|
# PrometheusExporter::Ext
|
|
2
2
|
|
|
3
|
-
](https://rubygems.org/gems/prometheus_exporter-ext)
|
|
4
|
+
[](https://github.com/didww/prometheus_exporter-ext/actions/workflows/tests.yml)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Extension for the [Ruby Prometheus Exporter](https://github.com/discourse/prometheus_exporter).
|
|
8
|
+
|
|
9
|
+
It adds a small, opinionated DSL on top of `prometheus_exporter` so you can build
|
|
10
|
+
custom instrumentations and server-side collectors with far less boilerplate, plus a
|
|
11
|
+
few features the base gem doesn't provide:
|
|
12
|
+
|
|
13
|
+
- **Instrumentation DSL** for both event-driven (`BaseStats`) and periodic (`PeriodicStats`) metrics.
|
|
14
|
+
- **Collector DSL** (`register_gauge`, `register_counter`, `register_metric`, …) to declare metrics declaratively.
|
|
15
|
+
- **Expiring gauges and counters** — automatically remove or zero stale metrics via `gauge_with_expire`,
|
|
16
|
+
`counter_with_expire` / `ExpiredStatsCollector`, so you don't keep exporting values for things that stopped reporting.
|
|
17
|
+
- **Built-in process CPU instrumentation** (`ProcCpu`) reading from `/proc/self/stat`.
|
|
18
|
+
- **Built-in ActiveJob and Solid Queue instrumentations** with matching collectors.
|
|
19
|
+
- **A request middleware** that adds your own labels to `prometheus_exporter` HTTP request metrics.
|
|
20
|
+
- **A singleton web server** helper for embedding the exporter web server in your app.
|
|
21
|
+
- **RSpec matchers** to test your instrumentations and collectors without standing up a real server.
|
|
22
|
+
|
|
23
|
+
## Table of Contents
|
|
24
|
+
|
|
25
|
+
- [Installation](#installation)
|
|
26
|
+
- [How it works](#how-it-works)
|
|
27
|
+
- [Usage](#usage)
|
|
28
|
+
- [Event-driven metrics](#event-driven-metrics)
|
|
29
|
+
- [Periodic metrics](#periodic-metrics)
|
|
30
|
+
- [Collectors and expiring metrics](#collectors-and-expiring-metrics)
|
|
31
|
+
- [Built-in process CPU metrics](#built-in-process-cpu-metrics)
|
|
32
|
+
- [ActiveJob metrics](#activejob-metrics)
|
|
33
|
+
- [Solid Queue metrics](#solid-queue-metrics)
|
|
34
|
+
- [Request middleware labels](#request-middleware-labels)
|
|
35
|
+
- [Singleton web server](#singleton-web-server)
|
|
36
|
+
- [Testing](#testing)
|
|
37
|
+
- [Development](#development)
|
|
38
|
+
- [Contributing](#contributing)
|
|
39
|
+
- [License](#license)
|
|
8
40
|
|
|
9
41
|
## Installation
|
|
10
42
|
|
|
11
|
-
|
|
43
|
+
Requires Ruby `>= 3.3.0` and `prometheus_exporter ~> 2.0`.
|
|
44
|
+
|
|
45
|
+
Add the gem to your application's Gemfile by executing:
|
|
12
46
|
|
|
13
47
|
$ bundle add prometheus_exporter-ext
|
|
14
48
|
|
|
@@ -16,10 +50,29 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
|
16
50
|
|
|
17
51
|
$ gem install prometheus_exporter-ext
|
|
18
52
|
|
|
53
|
+
## How it works
|
|
54
|
+
|
|
55
|
+
The flow mirrors `prometheus_exporter`: an **instrumentation** runs inside your
|
|
56
|
+
application process and ships data points to the exporter server; a **collector**
|
|
57
|
+
runs inside the exporter server, receives those data points, and exposes them as
|
|
58
|
+
Prometheus metrics on the `/metrics` endpoint.
|
|
59
|
+
|
|
60
|
+
This gem gives you base classes for both sides, paired by a shared `self.type`:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
your app code ──▶ Instrumentation#collect_data ──▶ exporter server ──▶ Collector ──▶ /metrics
|
|
64
|
+
(self.type = 'my') (self.type = 'my')
|
|
65
|
+
```
|
|
66
|
+
|
|
19
67
|
## Usage
|
|
20
68
|
|
|
21
|
-
###
|
|
22
|
-
|
|
69
|
+
### Event-driven metrics
|
|
70
|
+
|
|
71
|
+
Use `BaseStats` when metrics should be sent in response to a particular event
|
|
72
|
+
(e.g. after running an operation).
|
|
73
|
+
|
|
74
|
+
Create the instrumentation:
|
|
75
|
+
|
|
23
76
|
```ruby
|
|
24
77
|
# lib/prometheus/my_instrumentation.rb
|
|
25
78
|
require 'prometheus_exporter/ext'
|
|
@@ -44,71 +97,46 @@ module Prometheus
|
|
|
44
97
|
end
|
|
45
98
|
```
|
|
46
99
|
|
|
47
|
-
|
|
100
|
+
Instead of the `rescue` block, you can wrap the work in the private `safe_collect` helper.
|
|
101
|
+
It passes any `StandardError` to the `on_error:` callable given to `new` (or `start` for periodic stats)
|
|
102
|
+
and prints a warning when no handler is set:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
def collect(duration, operation)
|
|
106
|
+
safe_collect do
|
|
107
|
+
collect_data(labels: { operation_name: operation }, last_duration_seconds: duration)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
Prometheus::MyInstrumentation.new(on_error: ->(e) { Rails.error.report(e, handled: true) })
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Then send metrics from your code:
|
|
115
|
+
|
|
48
116
|
```ruby
|
|
49
|
-
|
|
117
|
+
time_start = Time.current.to_i
|
|
50
118
|
begin
|
|
51
119
|
MyOperation.run
|
|
52
120
|
ensure
|
|
53
121
|
duration = Time.current.to_i - time_start
|
|
54
122
|
Prometheus::MyInstrumentation.new.collect(duration, 'my_operation')
|
|
55
|
-
|
|
123
|
+
|
|
124
|
+
# you can add additional labels or override the client
|
|
56
125
|
Prometheus::MyInstrumentation.new(
|
|
57
126
|
client: PrometheusExporter::Client.new(...),
|
|
58
127
|
labels: { foo: 'bar' }
|
|
59
|
-
).collect(duration)
|
|
128
|
+
).collect(duration, 'my_operation')
|
|
60
129
|
end
|
|
61
130
|
```
|
|
62
131
|
|
|
63
|
-
|
|
64
|
-
```ruby
|
|
65
|
-
require 'prometheus_exporter/ext'
|
|
66
|
-
require 'prometheus_exporter/ext/server/stats_collector'
|
|
67
|
-
|
|
68
|
-
module Prometheus
|
|
69
|
-
class MyCollector < ::PrometheusExporter::Server::TypeCollector
|
|
70
|
-
include ::PrometheusExporter::Ext::Server::StatsCollector
|
|
71
|
-
self.type = 'my'
|
|
132
|
+
### Periodic metrics
|
|
72
133
|
|
|
73
|
-
|
|
74
|
-
# when no :strategy option passed, default is `:removing`, available options are `:removing, :zeroing`.
|
|
75
|
-
# when no :ttl option passed, default is 60, any numeric greater than 0 can be used.
|
|
76
|
-
register_gauge_with_expire :last_duration_seconds, 'duration of last operation execution', ttl: 300
|
|
77
|
-
|
|
78
|
-
register_counter :task_duration_seconds_sum, 'sum of operation execution durations'
|
|
79
|
-
register_counter :task_duration_seconds_count, 'sum of operation execution runs'
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
as alternative you can use `ExpiredStatsCollector` if you want all metric data to be removed after expiration
|
|
85
|
-
```ruby
|
|
86
|
-
require 'prometheus_exporter/ext'
|
|
87
|
-
require 'prometheus_exporter/ext/server/stats_collector'
|
|
88
|
-
|
|
89
|
-
module Prometheus
|
|
90
|
-
class MyCollector < ::PrometheusExporter::Server::TypeCollector
|
|
91
|
-
include ::PrometheusExporter::Ext::Server::ExpiredStatsCollector
|
|
92
|
-
self.type = 'my'
|
|
93
|
-
self.ttl = 300 # default 60
|
|
94
|
-
|
|
95
|
-
# Optionally you can expire old_metric when specific new metric is collected.
|
|
96
|
-
# If this block returns true then old_metric will be removed.
|
|
97
|
-
unique_metric_by do |new_metric, old_metric|
|
|
98
|
-
new_metric['labels'] == old_metric['labels']
|
|
99
|
-
end
|
|
134
|
+
Use `PeriodicStats` when metrics should be sent periodically at a given frequency.
|
|
100
135
|
|
|
101
|
-
|
|
102
|
-
register_counter :task_duration_seconds_sum, 'sum of operation execution durations'
|
|
103
|
-
register_counter :task_duration_seconds_count, 'sum of operation execution runs'
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
````
|
|
136
|
+
Create the instrumentation:
|
|
107
137
|
|
|
108
|
-
### When metrics should be send periodically with given frequency
|
|
109
|
-
create instrumentation
|
|
110
138
|
```ruby
|
|
111
|
-
# lib/prometheus/
|
|
139
|
+
# lib/prometheus/my_periodic_instrumentation.rb
|
|
112
140
|
require 'prometheus_exporter/ext'
|
|
113
141
|
require 'prometheus_exporter/ext/instrumentation/periodic_stats'
|
|
114
142
|
|
|
@@ -132,20 +160,33 @@ module Prometheus
|
|
|
132
160
|
end
|
|
133
161
|
```
|
|
134
162
|
|
|
135
|
-
|
|
163
|
+
Then start it once (it runs on a background thread):
|
|
164
|
+
|
|
136
165
|
```ruby
|
|
137
|
-
Prometheus::
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
166
|
+
Prometheus::MyPeriodicInstrumentation.start
|
|
167
|
+
|
|
168
|
+
# you can override the frequency in seconds
|
|
169
|
+
Prometheus::MyPeriodicInstrumentation.start(frequency: 60)
|
|
170
|
+
|
|
171
|
+
# you can also add additional labels or override the client
|
|
172
|
+
Prometheus::MyPeriodicInstrumentation.start(
|
|
142
173
|
client: PrometheusExporter::Client.new(...),
|
|
143
174
|
labels: { foo: 'bar' }
|
|
144
175
|
)
|
|
145
|
-
|
|
176
|
+
|
|
177
|
+
# to stop the instrumentation:
|
|
178
|
+
Prometheus::MyPeriodicInstrumentation.stop
|
|
146
179
|
```
|
|
147
180
|
|
|
148
|
-
|
|
181
|
+
### Collectors and expiring metrics
|
|
182
|
+
|
|
183
|
+
On the server side, declare a collector with the same `type`. There are two flavors.
|
|
184
|
+
|
|
185
|
+
#### `StatsCollector` with per-metric expiration
|
|
186
|
+
|
|
187
|
+
`register_gauge_with_expire` removes or zeroes a single metric once it expires,
|
|
188
|
+
while leaving other metrics (e.g. counters) untouched.
|
|
189
|
+
|
|
149
190
|
```ruby
|
|
150
191
|
require 'prometheus_exporter/ext'
|
|
151
192
|
require 'prometheus_exporter/ext/server/stats_collector'
|
|
@@ -154,41 +195,215 @@ module Prometheus
|
|
|
154
195
|
class MyCollector < ::PrometheusExporter::Server::TypeCollector
|
|
155
196
|
include ::PrometheusExporter::Ext::Server::StatsCollector
|
|
156
197
|
self.type = 'my'
|
|
157
|
-
|
|
158
|
-
#
|
|
159
|
-
|
|
160
|
-
|
|
198
|
+
|
|
199
|
+
# `register_gauge_with_expire` removes or zeroes the metric once it expires.
|
|
200
|
+
# :strategy defaults to `:removing`; available options are `:removing` and `:zeroing`.
|
|
201
|
+
# :ttl defaults to 60 (seconds); any numeric greater than 0 can be used.
|
|
202
|
+
register_gauge_with_expire :last_duration_seconds, 'duration of last operation execution', ttl: 300
|
|
203
|
+
|
|
204
|
+
register_counter :duration_seconds_sum, 'sum of operation execution durations'
|
|
205
|
+
register_counter :duration_seconds_count, 'count of operation execution runs'
|
|
161
206
|
end
|
|
162
207
|
end
|
|
163
208
|
```
|
|
164
209
|
|
|
165
|
-
|
|
210
|
+
`register_counter_with_expire` works the same way for counters: a label set that is not updated
|
|
211
|
+
for `ttl` seconds (default 60) is removed, and if it reports again it starts from zero.
|
|
212
|
+
|
|
213
|
+
```ruby
|
|
214
|
+
register_counter_with_expire :processed_total, 'count of processed items', ttl: 300
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
To set one TTL for every expiring metric of a collector that doesn't pass `ttl:` itself, set the
|
|
218
|
+
collector's `ttl` before you create it. A `ttl:` given at registration still wins.
|
|
219
|
+
|
|
220
|
+
```ruby
|
|
221
|
+
MyCollector.ttl = 300
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
#### `ExpiredStatsCollector` for whole-collector expiration
|
|
225
|
+
|
|
226
|
+
Use `ExpiredStatsCollector` when you want **all** metric data removed after expiration.
|
|
227
|
+
|
|
166
228
|
```ruby
|
|
167
229
|
require 'prometheus_exporter/ext'
|
|
168
|
-
require 'prometheus_exporter/ext/server/
|
|
230
|
+
require 'prometheus_exporter/ext/server/expired_stats_collector'
|
|
169
231
|
|
|
170
232
|
module Prometheus
|
|
171
233
|
class MyCollector < ::PrometheusExporter::Server::TypeCollector
|
|
172
234
|
include ::PrometheusExporter::Ext::Server::ExpiredStatsCollector
|
|
173
235
|
self.type = 'my'
|
|
174
|
-
|
|
175
|
-
|
|
236
|
+
self.ttl = 300 # default 60
|
|
237
|
+
|
|
238
|
+
# Optionally expire an old metric when a specific new metric is collected.
|
|
239
|
+
# If this block returns true, the old metric is removed.
|
|
240
|
+
unique_metric_by do |new_metric, old_metric|
|
|
241
|
+
new_metric['labels'] == old_metric['labels']
|
|
242
|
+
end
|
|
176
243
|
|
|
177
|
-
register_gauge :
|
|
178
|
-
|
|
244
|
+
register_gauge :last_duration_seconds, 'duration of last operation execution'
|
|
245
|
+
register_counter :duration_seconds_sum, 'sum of operation execution durations'
|
|
246
|
+
register_counter :duration_seconds_count, 'count of operation execution runs'
|
|
179
247
|
end
|
|
180
248
|
end
|
|
181
|
-
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Built-in process CPU metrics
|
|
252
|
+
|
|
253
|
+
The gem ships a ready-to-use instrumentation/collector pair that reports cumulative
|
|
254
|
+
CPU time of the current process, read from `/proc/self/stat` (Linux only).
|
|
255
|
+
|
|
256
|
+
In your application process:
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
require 'prometheus_exporter/ext/instrumentation/proc_cpu'
|
|
260
|
+
|
|
261
|
+
# `type` is required and is added as a label, so you can distinguish
|
|
262
|
+
# CPU usage of different process kinds (web, sidekiq, etc).
|
|
263
|
+
PrometheusExporter::Ext::Instrumentation::ProcCpu.start(type: 'web')
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
In the exporter server:
|
|
267
|
+
|
|
268
|
+
```ruby
|
|
269
|
+
require 'prometheus_exporter/ext/server/proc_cpu_collector'
|
|
270
|
+
|
|
271
|
+
# register the collector with the exporter server, e.g.:
|
|
272
|
+
server.collector.register_collector(PrometheusExporter::Ext::Server::ProcCpuCollector.new)
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
This exposes `proc_cpu_usage_seconds_total` (a counter, in core-seconds) labeled with
|
|
276
|
+
`type`, `pid`, and `hostname`. A series that receives no data for 300 seconds is removed,
|
|
277
|
+
so processes that exited don't stay in the output. To change the TTL, set it before you create the collector:
|
|
278
|
+
|
|
279
|
+
```ruby
|
|
280
|
+
PrometheusExporter::Ext::Server::ProcCpuCollector.ttl = 600
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### ActiveJob metrics
|
|
284
|
+
|
|
285
|
+
The ActiveJob instrumentation subscribes to `ActiveSupport::Notifications` events of ActiveJob.
|
|
286
|
+
It needs ActiveSupport and ActiveJob, which this gem doesn't depend on, so load it explicitly
|
|
287
|
+
from a Rails app, for example in an initializer:
|
|
288
|
+
|
|
289
|
+
```ruby
|
|
290
|
+
require 'prometheus_exporter/ext/instrumentation/active_job'
|
|
291
|
+
|
|
292
|
+
PrometheusExporter::Ext::Instrumentation::ActiveJob.subscribe(
|
|
293
|
+
on_error: ->(e) { Rails.error.report(e, handled: true) } # optional, defaults to a warning
|
|
294
|
+
)
|
|
295
|
+
# to stop: PrometheusExporter::Ext::Instrumentation::ActiveJob.unsubscribe
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
In the exporter server:
|
|
299
|
+
|
|
300
|
+
```ruby
|
|
301
|
+
require 'prometheus_exporter/ext/server/active_job_collector'
|
|
302
|
+
|
|
303
|
+
server.collector.register_collector(PrometheusExporter::Ext::Server::ActiveJobCollector.new)
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
This exposes these counters, labeled with `job_class`, `adapter`, `queue`, and, where they apply,
|
|
307
|
+
`executions`, `scheduled`, and `error` (an exception class name):
|
|
308
|
+
`active_job_enqueued_total`, `active_job_fail_enqueued_total`, `active_job_executed_total`,
|
|
309
|
+
`active_job_success_total`, `active_job_failed_total`, `active_job_retried_total`,
|
|
310
|
+
`active_job_retry_stopped_total`, `active_job_discarded_total`, `active_job_duration_seconds`, and
|
|
311
|
+
`active_job_latency_seconds`. Latency of a scheduled job counts from its `scheduled_at`, not from enqueue time.
|
|
312
|
+
`active_job_enqueued_total` counts first enqueues only; a retry's re-enqueue counts in `active_job_retried_total`.
|
|
313
|
+
|
|
314
|
+
### Solid Queue metrics
|
|
315
|
+
|
|
316
|
+
A periodic instrumentation that counts Solid Queue executions. Load it explicitly in an app
|
|
317
|
+
that uses Solid Queue:
|
|
318
|
+
|
|
319
|
+
```ruby
|
|
320
|
+
require 'prometheus_exporter/ext/instrumentation/solid_queue'
|
|
321
|
+
|
|
322
|
+
PrometheusExporter::Ext::Instrumentation::SolidQueue.start(
|
|
323
|
+
frequency: 30,
|
|
324
|
+
on_error: ->(e) { Rails.error.report(e, handled: true) } # optional, defaults to a warning
|
|
325
|
+
)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
In the exporter server:
|
|
329
|
+
|
|
330
|
+
```ruby
|
|
331
|
+
require 'prometheus_exporter/ext/server/solid_queue_collector'
|
|
332
|
+
|
|
333
|
+
server.collector.register_collector(PrometheusExporter::Ext::Server::SolidQueueCollector.new)
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
This exposes the expiring gauges `solid_queue_ready_count`, `solid_queue_scheduled_count`,
|
|
337
|
+
`solid_queue_claimed_count`, `solid_queue_blocked_count`, and `solid_queue_failed_count`.
|
|
338
|
+
The gauges expire after 60 seconds without data. If `frequency` is 60 seconds or more, raise the TTL
|
|
339
|
+
before you create the collector:
|
|
340
|
+
|
|
341
|
+
```ruby
|
|
342
|
+
PrometheusExporter::Ext::Server::SolidQueueCollector.ttl = 180
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### Request middleware labels
|
|
346
|
+
|
|
347
|
+
`PrometheusExporter::Ext::RequestMiddleware` is a `PrometheusExporter::Middleware` that adds
|
|
348
|
+
fixed labels to every request metric, for example to tell apart processes that serve different traffic:
|
|
349
|
+
|
|
350
|
+
```ruby
|
|
351
|
+
require 'prometheus_exporter/ext/request_middleware'
|
|
352
|
+
|
|
353
|
+
Rails.application.middleware.unshift PrometheusExporter::Ext::RequestMiddleware, labels: { type: 'web' }
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
It accepts the same options as `PrometheusExporter::Middleware` (`instrument:`, `client:`).
|
|
357
|
+
When you omit `instrument:`, it defaults to `:alias_method` even if you pass other keys.
|
|
358
|
+
The parent class applies that default only when you pass no config at all, so
|
|
359
|
+
`client:` alone turns instrumentation off there but not here. To turn it off, pass `instrument: nil`.
|
|
360
|
+
|
|
361
|
+
### Singleton web server
|
|
362
|
+
|
|
363
|
+
`SingletonWebServer` wraps `PrometheusExporter::Server::WebServer` with class-level
|
|
364
|
+
start/stop helpers, making it easy to embed and configure the exporter web server
|
|
365
|
+
from a single place in your app.
|
|
366
|
+
|
|
367
|
+
```ruby
|
|
368
|
+
require 'prometheus_exporter/ext/server/singleton_web_server'
|
|
369
|
+
|
|
370
|
+
PrometheusExporter::Ext::Server::SingletonWebServer.start(
|
|
371
|
+
bind: '0.0.0.0',
|
|
372
|
+
port: 9394
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
# register collectors against the running server
|
|
376
|
+
PrometheusExporter::Ext::Server::SingletonWebServer.configure_collector do |collector|
|
|
377
|
+
collector.register_collector(Prometheus::MyCollector.new)
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
# optionally protect /metrics with basic auth
|
|
381
|
+
PrometheusExporter::Ext::Server::SingletonWebServer.build_htpasswd(
|
|
382
|
+
'/path/to/.htpasswd',
|
|
383
|
+
username: 'prometheus',
|
|
384
|
+
password: 'secret'
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
# shut it down
|
|
388
|
+
PrometheusExporter::Ext::Server::SingletonWebServer.stop
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
## Testing
|
|
182
392
|
|
|
183
|
-
|
|
393
|
+
The gem provides RSpec matchers so you can test instrumentations and collectors
|
|
394
|
+
without running a real exporter server. Require them in your spec helper:
|
|
184
395
|
|
|
185
|
-
instrumentation test
|
|
186
396
|
```ruby
|
|
187
397
|
require 'prometheus_exporter/ext/rspec'
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
Test an instrumentation with the `send_metrics` matcher:
|
|
188
401
|
|
|
402
|
+
```ruby
|
|
189
403
|
RSpec.describe Prometheus::MyInstrumentation do
|
|
190
404
|
describe '#collect' do
|
|
191
405
|
subject { described_class.new.collect(duration, operation) }
|
|
406
|
+
|
|
192
407
|
let(:duration) { 1.23 }
|
|
193
408
|
let(:operation) { 'test' }
|
|
194
409
|
|
|
@@ -207,13 +422,13 @@ RSpec.describe Prometheus::MyInstrumentation do
|
|
|
207
422
|
end
|
|
208
423
|
```
|
|
209
424
|
|
|
210
|
-
collector
|
|
425
|
+
Test a collector with the metric matchers
|
|
426
|
+
(`a_gauge_metric`, `a_counter_metric`, `a_gauge_with_expire_metric`, `a_counter_with_expire_metric`, …):
|
|
427
|
+
|
|
211
428
|
```ruby
|
|
212
429
|
RSpec.describe Prometheus::MyCollector do
|
|
213
430
|
describe '#collect' do
|
|
214
|
-
subject
|
|
215
|
-
collector.metrics
|
|
216
|
-
end
|
|
431
|
+
subject { collector.metrics }
|
|
217
432
|
|
|
218
433
|
let(:collector) { described_class.new }
|
|
219
434
|
let(:metric) do
|
|
@@ -225,28 +440,28 @@ RSpec.describe Prometheus::MyCollector do
|
|
|
225
440
|
duration_seconds_count: 1
|
|
226
441
|
}
|
|
227
442
|
end
|
|
228
|
-
|
|
443
|
+
|
|
229
444
|
let(:collect_data) do
|
|
230
445
|
collector.collect(metric.deep_stringify_keys)
|
|
231
446
|
end
|
|
232
447
|
|
|
448
|
+
before { collect_data }
|
|
449
|
+
|
|
233
450
|
it 'observes prometheus metrics' do
|
|
234
|
-
subject
|
|
235
451
|
expect(collector.metrics).to contain_exactly(
|
|
236
452
|
a_gauge_with_expire_metric('my_last_duration_seconds').with(1.2, metric[:labels]),
|
|
237
453
|
a_counter_metric('my_duration_seconds_sum').with(3.4, metric[:labels]),
|
|
238
454
|
a_counter_metric('my_duration_seconds_count').with(1, metric[:labels])
|
|
239
455
|
)
|
|
240
456
|
end
|
|
241
|
-
|
|
457
|
+
|
|
242
458
|
context 'when collected data is expired' do
|
|
243
459
|
let(:collect_data) do
|
|
244
460
|
super()
|
|
245
461
|
sleep 60.1 # when gauge_with_expire ttl is 60
|
|
246
462
|
end
|
|
247
463
|
|
|
248
|
-
it 'observes empty prometheus metrics' do
|
|
249
|
-
subject
|
|
464
|
+
it 'observes empty prometheus metrics for the expired gauge' do
|
|
250
465
|
expect(collector.metrics).to contain_exactly(
|
|
251
466
|
a_gauge_with_expire_metric('my_last_duration_seconds').empty,
|
|
252
467
|
a_counter_metric('my_duration_seconds_sum').with(3.4, metric[:labels]),
|
|
@@ -260,14 +475,22 @@ end
|
|
|
260
475
|
|
|
261
476
|
## Development
|
|
262
477
|
|
|
263
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then
|
|
478
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then run
|
|
479
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
|
480
|
+
prompt to experiment.
|
|
264
481
|
|
|
265
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
482
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
483
|
+
To release a new version, update the version number in `version.rb`, and then run
|
|
484
|
+
`bundle exec rake release`, which will create a git tag for the version, push git
|
|
485
|
+
commits and the created tag, and push the `.gem` file to
|
|
486
|
+
[rubygems.org](https://rubygems.org).
|
|
266
487
|
|
|
267
488
|
## Contributing
|
|
268
489
|
|
|
269
|
-
Bug reports and pull requests are welcome on GitHub at
|
|
490
|
+
Bug reports and pull requests are welcome on GitHub at
|
|
491
|
+
https://github.com/didww/prometheus_exporter-ext.
|
|
270
492
|
|
|
271
493
|
## License
|
|
272
494
|
|
|
273
|
-
The gem is available as open source under the terms of the
|
|
495
|
+
The gem is available as open source under the terms of the
|
|
496
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -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
|
|
@@ -9,9 +9,12 @@ module PrometheusExporter::Ext::Instrumentation
|
|
|
9
9
|
attr_accessor :type
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
|
|
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)
|
|
13
15
|
@labels = labels.transform_keys(&:to_sym)
|
|
14
16
|
@client = client
|
|
17
|
+
@on_error = on_error || method(:warn_error)
|
|
15
18
|
end
|
|
16
19
|
|
|
17
20
|
def type
|
|
@@ -24,6 +27,26 @@ module PrometheusExporter::Ext::Instrumentation
|
|
|
24
27
|
|
|
25
28
|
private
|
|
26
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
|
+
|
|
27
50
|
# @param data [Array,Hash]
|
|
28
51
|
def collect_data(data)
|
|
29
52
|
data_list = data.is_a?(Array) ? data : [data]
|
|
@@ -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
|
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
require 'prometheus_exporter/metric'
|
|
4
4
|
require_relative '../../ext'
|
|
5
|
+
require_relative 'expiring'
|
|
5
6
|
|
|
6
7
|
module PrometheusExporter::Ext::Metric
|
|
7
8
|
class GaugeWithExpire < PrometheusExporter::Metric::Gauge
|
|
9
|
+
include Expiring
|
|
10
|
+
|
|
8
11
|
NULLIFY_STRATEGY_UPDATE = ->(labels) { expiration_times[labels] = now_time + ttl }.freeze
|
|
9
12
|
NULLIFY_STRATEGY_EXPIRE = ->(labels) { data.delete(labels) }.freeze
|
|
10
13
|
ZEROING_STRATEGY_UPDATE = ->(labels) do
|
|
@@ -28,19 +31,11 @@ module PrometheusExporter::Ext::Metric
|
|
|
28
31
|
zeroing: { on_update: ZEROING_STRATEGY_UPDATE, on_expire: ZEROING_STRATEGY_EXPIRE }
|
|
29
32
|
}
|
|
30
33
|
end
|
|
31
|
-
|
|
32
|
-
def default_ttl
|
|
33
|
-
60
|
|
34
|
-
end
|
|
35
34
|
end
|
|
36
35
|
|
|
37
|
-
attr_reader :ttl, :expiration_times
|
|
38
|
-
|
|
39
36
|
def initialize(name, help, opts = {})
|
|
40
37
|
super(name, help)
|
|
41
|
-
|
|
42
|
-
raise ArgumentError, ':ttl must be numeric' unless ttl.is_a?(Numeric)
|
|
43
|
-
raise ArgumentError, ":ttl must be greater than zero: #{ttl.inspect}" unless ttl.positive?
|
|
38
|
+
init_ttl(opts)
|
|
44
39
|
|
|
45
40
|
@strategy = self.class.strategies.fetch(opts[:strategy] || :removing) do
|
|
46
41
|
raise ArgumentError, "Unknown strategy: #{opts[:strategy].inspect}"
|
|
@@ -109,9 +104,5 @@ module PrometheusExporter::Ext::Metric
|
|
|
109
104
|
def remove_expired_at(labels)
|
|
110
105
|
expiration_times.delete(labels)
|
|
111
106
|
end
|
|
112
|
-
|
|
113
|
-
def now_time
|
|
114
|
-
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
115
|
-
end
|
|
116
107
|
end
|
|
117
108
|
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'prometheus_exporter/middleware'
|
|
4
|
+
require_relative '../ext'
|
|
5
|
+
|
|
6
|
+
# Adds a fixed set of custom labels to every HTTP request metric.
|
|
7
|
+
#
|
|
8
|
+
# Rails.application.middleware.unshift PrometheusExporter::Ext::RequestMiddleware, labels: { type: 'web' }
|
|
9
|
+
class PrometheusExporter::Ext::RequestMiddleware < PrometheusExporter::Middleware
|
|
10
|
+
# @param app [#call] Rack application.
|
|
11
|
+
# @param config [Hash] PrometheusExporter::Middleware config (`instrument`, `client`) plus `labels`.
|
|
12
|
+
def initialize(app, config = { instrument: :alias_method, client: nil })
|
|
13
|
+
@labels = (config[:labels] || {}).transform_keys(&:to_sym).freeze
|
|
14
|
+
super(app, { instrument: :alias_method }.merge(config.except(:labels)))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @return [Hash, nil]
|
|
18
|
+
def custom_labels(env)
|
|
19
|
+
parent = super
|
|
20
|
+
return parent.merge(@labels) if parent.is_a?(Hash)
|
|
21
|
+
|
|
22
|
+
@labels.empty? ? parent : @labels
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'date'
|
|
4
|
+
require_relative '../metric/counter_with_expire'
|
|
4
5
|
require_relative 'metric_matcher'
|
|
5
6
|
require_relative 'send_metrics_matcher'
|
|
6
7
|
|
|
@@ -27,6 +28,10 @@ module PrometheusExporter::Ext::RSpec
|
|
|
27
28
|
a_prometheus_metric(PrometheusExporter::Metric::Counter, name)
|
|
28
29
|
end
|
|
29
30
|
|
|
31
|
+
def a_counter_with_expire_metric(name)
|
|
32
|
+
a_prometheus_metric(PrometheusExporter::Ext::Metric::CounterWithExpire, name)
|
|
33
|
+
end
|
|
34
|
+
|
|
30
35
|
def a_histogram_metric(name)
|
|
31
36
|
a_prometheus_metric(PrometheusExporter::Metric::Histogram, name)
|
|
32
37
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'stats_collector'
|
|
4
|
+
|
|
5
|
+
module PrometheusExporter::Ext::Server
|
|
6
|
+
class ActiveJobCollector < PrometheusExporter::Server::TypeCollector
|
|
7
|
+
include StatsCollector
|
|
8
|
+
|
|
9
|
+
self.type = 'active_job'
|
|
10
|
+
|
|
11
|
+
register_counter :enqueued_total, 'total enqueued jobs qty'
|
|
12
|
+
register_counter :fail_enqueued_total, 'total jobs qty that were failed to enqueue'
|
|
13
|
+
register_counter :executed_total, 'total executed jobs qty'
|
|
14
|
+
register_counter :failed_total, 'total jobs qty that failed, without successful retry, rescue, or discard'
|
|
15
|
+
register_counter :success_total,
|
|
16
|
+
'total successfully executed jobs qty, including with successful retry, rescue, or discard'
|
|
17
|
+
register_counter :retried_total, 'total successfully retried jobs qty'
|
|
18
|
+
register_counter :retry_stopped_total, 'total failed retry jobs qty, due to max attempts reached'
|
|
19
|
+
register_counter :discarded_total, 'total discarded jobs qty'
|
|
20
|
+
register_counter :duration_seconds, 'seconds qty of job execution'
|
|
21
|
+
register_counter :latency_seconds, 'seconds qty from enqueuing to start performing a job'
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -7,6 +7,8 @@ module PrometheusExporter::Ext::Server
|
|
|
7
7
|
include ::PrometheusExporter::Ext::Server::StatsCollector
|
|
8
8
|
self.type = 'proc_cpu'
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
self.ttl = 300
|
|
11
|
+
|
|
12
|
+
register_counter_with_expire :usage_seconds_total, 'Cumulative CPU time consumed by the process in core-seconds'
|
|
11
13
|
end
|
|
12
14
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'stats_collector'
|
|
4
|
+
|
|
5
|
+
module PrometheusExporter::Ext::Server
|
|
6
|
+
class SolidQueueCollector < PrometheusExporter::Server::TypeCollector
|
|
7
|
+
include StatsCollector
|
|
8
|
+
|
|
9
|
+
self.type = 'solid_queue'
|
|
10
|
+
|
|
11
|
+
register_gauge_with_expire :ready_count, 'number of jobs ready to be picked up'
|
|
12
|
+
register_gauge_with_expire :scheduled_count, 'number of jobs scheduled for future execution'
|
|
13
|
+
register_gauge_with_expire :claimed_count, 'number of jobs currently being executed'
|
|
14
|
+
register_gauge_with_expire :blocked_count, 'number of jobs blocked by concurrency controls'
|
|
15
|
+
register_gauge_with_expire :failed_count, 'number of jobs that have failed'
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'base_collector_methods'
|
|
4
|
+
require_relative '../metric/counter_with_expire'
|
|
4
5
|
|
|
5
6
|
module PrometheusExporter::Ext::Server
|
|
6
7
|
module StatsCollector
|
|
@@ -15,6 +16,10 @@ module PrometheusExporter::Ext::Server
|
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
module ClassMethods
|
|
19
|
+
# Seconds an expiring metric keeps a label set without updates, for metrics registered without `ttl:`.
|
|
20
|
+
# nil keeps each metric's default. Set it before the collector is instantiated.
|
|
21
|
+
attr_accessor :ttl
|
|
22
|
+
|
|
18
23
|
# Registers PrometheusExporter::Metric::GaugeWithExpire observer.
|
|
19
24
|
# @param name [Symbol] metric name.
|
|
20
25
|
# @param help [String] metric description.
|
|
@@ -22,11 +27,19 @@ module PrometheusExporter::Ext::Server
|
|
|
22
27
|
def register_gauge_with_expire(name, help, opts = {})
|
|
23
28
|
register_metric(name, help, PrometheusExporter::Ext::Metric::GaugeWithExpire, opts)
|
|
24
29
|
end
|
|
30
|
+
|
|
31
|
+
# Registers PrometheusExporter::Ext::Metric::CounterWithExpire observer.
|
|
32
|
+
# @param name [Symbol] metric name.
|
|
33
|
+
# @param help [String] metric description.
|
|
34
|
+
# @param opts [Hash] additional options, supports `ttl` key.
|
|
35
|
+
def register_counter_with_expire(name, help, opts = {})
|
|
36
|
+
register_metric(name, help, PrometheusExporter::Ext::Metric::CounterWithExpire, opts)
|
|
37
|
+
end
|
|
25
38
|
end
|
|
26
39
|
|
|
27
40
|
def initialize
|
|
28
41
|
super
|
|
29
|
-
@observers =
|
|
42
|
+
@observers = build_stats_observers
|
|
30
43
|
end
|
|
31
44
|
|
|
32
45
|
# Returns all metrics collected by this collector.
|
|
@@ -41,5 +54,18 @@ module PrometheusExporter::Ext::Server
|
|
|
41
54
|
obj = normalize_labels(obj)
|
|
42
55
|
fill_observers(@observers, obj)
|
|
43
56
|
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# BaseCollectorMethods#build_observers ignores the collector ttl.
|
|
61
|
+
def build_stats_observers
|
|
62
|
+
self.class.registered_metrics.to_h do |name, metric|
|
|
63
|
+
args = metric[:args]
|
|
64
|
+
if self.class.ttl && metric[:metric_class] < PrometheusExporter::Ext::Metric::Expiring
|
|
65
|
+
args = [{ ttl: self.class.ttl }.merge(args.first || {}), *args.drop(1)]
|
|
66
|
+
end
|
|
67
|
+
[name, metric[:metric_class].new("#{type}_#{name}", metric[:help], *args)]
|
|
68
|
+
end
|
|
69
|
+
end
|
|
44
70
|
end
|
|
45
71
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: prometheus_exporter-ext
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Denis Talakevich
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: prometheus_exporter
|
|
@@ -36,20 +35,27 @@ files:
|
|
|
36
35
|
- README.md
|
|
37
36
|
- Rakefile
|
|
38
37
|
- lib/prometheus_exporter/ext.rb
|
|
38
|
+
- lib/prometheus_exporter/ext/instrumentation/active_job.rb
|
|
39
39
|
- lib/prometheus_exporter/ext/instrumentation/base_stats.rb
|
|
40
40
|
- lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb
|
|
41
41
|
- lib/prometheus_exporter/ext/instrumentation/proc_cpu.rb
|
|
42
|
+
- lib/prometheus_exporter/ext/instrumentation/solid_queue.rb
|
|
43
|
+
- lib/prometheus_exporter/ext/metric/counter_with_expire.rb
|
|
44
|
+
- lib/prometheus_exporter/ext/metric/expiring.rb
|
|
42
45
|
- lib/prometheus_exporter/ext/metric/gauge_with_expire.rb
|
|
43
46
|
- lib/prometheus_exporter/ext/proc_self_stat.rb
|
|
47
|
+
- lib/prometheus_exporter/ext/request_middleware.rb
|
|
44
48
|
- lib/prometheus_exporter/ext/rspec.rb
|
|
45
49
|
- lib/prometheus_exporter/ext/rspec/matchers.rb
|
|
46
50
|
- lib/prometheus_exporter/ext/rspec/metric_matcher.rb
|
|
47
51
|
- lib/prometheus_exporter/ext/rspec/send_metrics_matcher.rb
|
|
48
52
|
- lib/prometheus_exporter/ext/rspec/test_client.rb
|
|
53
|
+
- lib/prometheus_exporter/ext/server/active_job_collector.rb
|
|
49
54
|
- lib/prometheus_exporter/ext/server/base_collector_methods.rb
|
|
50
55
|
- lib/prometheus_exporter/ext/server/expired_stats_collector.rb
|
|
51
56
|
- lib/prometheus_exporter/ext/server/proc_cpu_collector.rb
|
|
52
57
|
- lib/prometheus_exporter/ext/server/singleton_web_server.rb
|
|
58
|
+
- lib/prometheus_exporter/ext/server/solid_queue_collector.rb
|
|
53
59
|
- lib/prometheus_exporter/ext/server/stats_collector.rb
|
|
54
60
|
- lib/prometheus_exporter/ext/version.rb
|
|
55
61
|
homepage: https://github.com/senid231/prometheus_exporter-ext
|
|
@@ -60,7 +66,6 @@ metadata:
|
|
|
60
66
|
source_code_uri: https://github.com/senid231/prometheus_exporter-ext
|
|
61
67
|
changelog_uri: https://github.com/senid231/prometheus_exporter-ext/blob/main/CHANGELOG.md
|
|
62
68
|
rubygems_mfa_required: 'true'
|
|
63
|
-
post_install_message:
|
|
64
69
|
rdoc_options: []
|
|
65
70
|
require_paths:
|
|
66
71
|
- lib
|
|
@@ -68,15 +73,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
68
73
|
requirements:
|
|
69
74
|
- - ">="
|
|
70
75
|
- !ruby/object:Gem::Version
|
|
71
|
-
version: 3.
|
|
76
|
+
version: 3.3.0
|
|
72
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
78
|
requirements:
|
|
74
79
|
- - ">="
|
|
75
80
|
- !ruby/object:Gem::Version
|
|
76
81
|
version: '0'
|
|
77
82
|
requirements: []
|
|
78
|
-
rubygems_version: 3.
|
|
79
|
-
signing_key:
|
|
83
|
+
rubygems_version: 3.6.9
|
|
80
84
|
specification_version: 4
|
|
81
85
|
summary: Extended Prometheus Exporter for Ruby
|
|
82
86
|
test_files: []
|