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
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
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
## [Unreleased]
|
|
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
|
+
|
|
13
|
+
## [0.3.2] - 2026-06-11
|
|
14
|
+
- fix gemspec missing `spec.files` so the published gem actually ships its source files (0.3.1 and earlier shipped empty)
|
|
15
|
+
|
|
16
|
+
## [0.3.1] - 2024-05-04
|
|
17
|
+
- ProcCpu add hostname label
|
|
18
|
+
|
|
19
|
+
## [0.3.0] - 2024-05-04
|
|
20
|
+
- replace ProcStat with ProcCpu
|
|
21
|
+
|
|
22
|
+
## [0.2.4] - 2024-05-03
|
|
23
|
+
- add PrometheusExporter::Ext::Server::ProcStatCollector
|
|
24
|
+
|
|
25
|
+
## [0.2.3] - 2024-05-03
|
|
26
|
+
- add PrometheusExporter::Ext::Instrumentation::ProcStat
|
|
27
|
+
|
|
28
|
+
## [0.2.2] - 2023-12-08
|
|
29
|
+
- fix send_metrics RSpec matcher
|
|
30
|
+
|
|
31
|
+
## [0.2.1] - 2023-12-06
|
|
32
|
+
- add gauge_with_expire, add tests, update readme
|
|
33
|
+
|
|
34
|
+
## [0.2.0] - 2023-12-06
|
|
35
|
+
- remove gauge_with_time, add expired_stats_collector, add tests
|
|
36
|
+
|
|
37
|
+
## [0.1.0] - 2023-11-26
|
|
38
|
+
- Initial release
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Denis Talakevich
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
# PrometheusExporter::Ext
|
|
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)
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
Requires Ruby `>= 3.3.0` and `prometheus_exporter ~> 2.0`.
|
|
44
|
+
|
|
45
|
+
Add the gem to your application's Gemfile by executing:
|
|
46
|
+
|
|
47
|
+
$ bundle add prometheus_exporter-ext
|
|
48
|
+
|
|
49
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
50
|
+
|
|
51
|
+
$ gem install prometheus_exporter-ext
|
|
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
|
+
|
|
67
|
+
## Usage
|
|
68
|
+
|
|
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
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
# lib/prometheus/my_instrumentation.rb
|
|
78
|
+
require 'prometheus_exporter/ext'
|
|
79
|
+
require 'prometheus_exporter/ext/instrumentation/base_stats'
|
|
80
|
+
|
|
81
|
+
module Prometheus
|
|
82
|
+
class MyInstrumentation < ::PrometheusExporter::Ext::Instrumentation::BaseStats
|
|
83
|
+
self.type = 'my'
|
|
84
|
+
|
|
85
|
+
def collect(duration, operation)
|
|
86
|
+
collect_data(
|
|
87
|
+
labels: { operation_name: operation },
|
|
88
|
+
last_duration_seconds: duration,
|
|
89
|
+
duration_seconds_sum: duration,
|
|
90
|
+
duration_seconds_count: 1
|
|
91
|
+
)
|
|
92
|
+
rescue StandardError => e
|
|
93
|
+
Rails.logger.error("Failed to send metrics Prometheus #{self.class.name} #{e}")
|
|
94
|
+
Rails.error.report(e, handled: true, severity: :error, context: { prometheus: self.class.name })
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
```
|
|
99
|
+
|
|
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
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
time_start = Time.current.to_i
|
|
118
|
+
begin
|
|
119
|
+
MyOperation.run
|
|
120
|
+
ensure
|
|
121
|
+
duration = Time.current.to_i - time_start
|
|
122
|
+
Prometheus::MyInstrumentation.new.collect(duration, 'my_operation')
|
|
123
|
+
|
|
124
|
+
# you can add additional labels or override the client
|
|
125
|
+
Prometheus::MyInstrumentation.new(
|
|
126
|
+
client: PrometheusExporter::Client.new(...),
|
|
127
|
+
labels: { foo: 'bar' }
|
|
128
|
+
).collect(duration, 'my_operation')
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Periodic metrics
|
|
133
|
+
|
|
134
|
+
Use `PeriodicStats` when metrics should be sent periodically at a given frequency.
|
|
135
|
+
|
|
136
|
+
Create the instrumentation:
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
# lib/prometheus/my_periodic_instrumentation.rb
|
|
140
|
+
require 'prometheus_exporter/ext'
|
|
141
|
+
require 'prometheus_exporter/ext/instrumentation/periodic_stats'
|
|
142
|
+
|
|
143
|
+
module Prometheus
|
|
144
|
+
class MyPeriodicInstrumentation < ::PrometheusExporter::Ext::Instrumentation::PeriodicStats
|
|
145
|
+
self.type = 'my'
|
|
146
|
+
|
|
147
|
+
def collect
|
|
148
|
+
count = MyItem.processed.count
|
|
149
|
+
last_duration = MyItem.processed.last&.duration
|
|
150
|
+
collect_data(
|
|
151
|
+
labels: { some_label: 'some_value' },
|
|
152
|
+
last_processed_duration: last_duration || 0,
|
|
153
|
+
processed_count: count
|
|
154
|
+
)
|
|
155
|
+
rescue StandardError => e
|
|
156
|
+
Rails.logger.error("Failed to send metrics Prometheus #{self.class.name} #{e}")
|
|
157
|
+
Rails.error.report(e, handled: true, severity: :error, context: { prometheus: self.class.name })
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Then start it once (it runs on a background thread):
|
|
164
|
+
|
|
165
|
+
```ruby
|
|
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(
|
|
173
|
+
client: PrometheusExporter::Client.new(...),
|
|
174
|
+
labels: { foo: 'bar' }
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
# to stop the instrumentation:
|
|
178
|
+
Prometheus::MyPeriodicInstrumentation.stop
|
|
179
|
+
```
|
|
180
|
+
|
|
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
|
+
|
|
190
|
+
```ruby
|
|
191
|
+
require 'prometheus_exporter/ext'
|
|
192
|
+
require 'prometheus_exporter/ext/server/stats_collector'
|
|
193
|
+
|
|
194
|
+
module Prometheus
|
|
195
|
+
class MyCollector < ::PrometheusExporter::Server::TypeCollector
|
|
196
|
+
include ::PrometheusExporter::Ext::Server::StatsCollector
|
|
197
|
+
self.type = 'my'
|
|
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'
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
```
|
|
209
|
+
|
|
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
|
+
|
|
228
|
+
```ruby
|
|
229
|
+
require 'prometheus_exporter/ext'
|
|
230
|
+
require 'prometheus_exporter/ext/server/expired_stats_collector'
|
|
231
|
+
|
|
232
|
+
module Prometheus
|
|
233
|
+
class MyCollector < ::PrometheusExporter::Server::TypeCollector
|
|
234
|
+
include ::PrometheusExporter::Ext::Server::ExpiredStatsCollector
|
|
235
|
+
self.type = 'my'
|
|
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
|
|
243
|
+
|
|
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'
|
|
247
|
+
end
|
|
248
|
+
end
|
|
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
|
|
392
|
+
|
|
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:
|
|
395
|
+
|
|
396
|
+
```ruby
|
|
397
|
+
require 'prometheus_exporter/ext/rspec'
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
Test an instrumentation with the `send_metrics` matcher:
|
|
401
|
+
|
|
402
|
+
```ruby
|
|
403
|
+
RSpec.describe Prometheus::MyInstrumentation do
|
|
404
|
+
describe '#collect' do
|
|
405
|
+
subject { described_class.new.collect(duration, operation) }
|
|
406
|
+
|
|
407
|
+
let(:duration) { 1.23 }
|
|
408
|
+
let(:operation) { 'test' }
|
|
409
|
+
|
|
410
|
+
it 'sends prometheus metrics' do
|
|
411
|
+
expect { subject }.to send_metrics(
|
|
412
|
+
[
|
|
413
|
+
type: 'my',
|
|
414
|
+
labels: { operation_name: operation },
|
|
415
|
+
last_duration_seconds: duration,
|
|
416
|
+
duration_seconds_sum: duration,
|
|
417
|
+
duration_seconds_count: 1
|
|
418
|
+
]
|
|
419
|
+
)
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
end
|
|
423
|
+
```
|
|
424
|
+
|
|
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
|
+
|
|
428
|
+
```ruby
|
|
429
|
+
RSpec.describe Prometheus::MyCollector do
|
|
430
|
+
describe '#collect' do
|
|
431
|
+
subject { collector.metrics }
|
|
432
|
+
|
|
433
|
+
let(:collector) { described_class.new }
|
|
434
|
+
let(:metric) do
|
|
435
|
+
{
|
|
436
|
+
type: 'my',
|
|
437
|
+
labels: { operation_name: 'test' },
|
|
438
|
+
last_duration_seconds: 1.2,
|
|
439
|
+
duration_seconds_sum: 3.4,
|
|
440
|
+
duration_seconds_count: 1
|
|
441
|
+
}
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
let(:collect_data) do
|
|
445
|
+
collector.collect(metric.deep_stringify_keys)
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
before { collect_data }
|
|
449
|
+
|
|
450
|
+
it 'observes prometheus metrics' do
|
|
451
|
+
expect(collector.metrics).to contain_exactly(
|
|
452
|
+
a_gauge_with_expire_metric('my_last_duration_seconds').with(1.2, metric[:labels]),
|
|
453
|
+
a_counter_metric('my_duration_seconds_sum').with(3.4, metric[:labels]),
|
|
454
|
+
a_counter_metric('my_duration_seconds_count').with(1, metric[:labels])
|
|
455
|
+
)
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
context 'when collected data is expired' do
|
|
459
|
+
let(:collect_data) do
|
|
460
|
+
super()
|
|
461
|
+
sleep 60.1 # when gauge_with_expire ttl is 60
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
it 'observes empty prometheus metrics for the expired gauge' do
|
|
465
|
+
expect(collector.metrics).to contain_exactly(
|
|
466
|
+
a_gauge_with_expire_metric('my_last_duration_seconds').empty,
|
|
467
|
+
a_counter_metric('my_duration_seconds_sum').with(3.4, metric[:labels]),
|
|
468
|
+
a_counter_metric('my_duration_seconds_count').with(1, metric[:labels])
|
|
469
|
+
)
|
|
470
|
+
end
|
|
471
|
+
end
|
|
472
|
+
end
|
|
473
|
+
end
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
## Development
|
|
477
|
+
|
|
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.
|
|
481
|
+
|
|
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).
|
|
487
|
+
|
|
488
|
+
## Contributing
|
|
489
|
+
|
|
490
|
+
Bug reports and pull requests are welcome on GitHub at
|
|
491
|
+
https://github.com/didww/prometheus_exporter-ext.
|
|
492
|
+
|
|
493
|
+
## License
|
|
494
|
+
|
|
495
|
+
The gem is available as open source under the terms of the
|
|
496
|
+
[MIT License](https://opensource.org/licenses/MIT).
|