sidekiq-influxdb 1.4.0 → 1.4.2
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 +9 -0
- data/CONTRIBUTING.md +6 -0
- data/README.md +68 -31
- data/lib/sidekiq/influxdb/version.rb +1 -1
- data/lib/sidekiq/middleware/server/influxdb.rb +32 -34
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 607a8f119a12166e7e1e9b266d5e8a6f15c665b2f4c3dd8d8f83dbf4485bf2ce
|
|
4
|
+
data.tar.gz: a7daceac5ae7b29a3d45b5cffaa0ee73e90bfe6b6a795659e0d465e28037a865
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7de72f2c1014b3eacdcfb03e15eebfe4d3521c4e2379f029ed3ab07fd97ccce6e9639977985c4a50a59dd786940f6b21452835513123847a6f7c474f1a1105e0
|
|
7
|
+
data.tar.gz: affc2415b5ed9a874e3a9804128bcfc320081d1a324bcde8bd583e6d054a72fd2f30f0ba3aeca18bfa5a00778ea80ceed48a820f33c2f9aabfbacd785ef280d1
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Sidekiq-InfluxDB Changelog
|
|
2
2
|
|
|
3
|
+
## 1.4.2 (2026-06-05)
|
|
4
|
+
|
|
5
|
+
* Sidekiq 8 support: convert millisecond `created_at` (Integer) to seconds for `waited`, `creation_time`, and `total`
|
|
6
|
+
* Added `measurement_key` option (`:values` by default, use `:fields` for InfluxDB 2 clients)
|
|
7
|
+
|
|
8
|
+
## 1.4.1 (2021-04-20)
|
|
9
|
+
|
|
10
|
+
* Ruby 3 support
|
|
11
|
+
|
|
3
12
|
## 1.4.0 (2021-03-01)
|
|
4
13
|
|
|
5
14
|
* Added queue sizes collection class
|
data/CONTRIBUTING.md
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
# Sidekiq-InfluxDB
|
|
2
2
|
|
|
3
|
+
## Reference documentation
|
|
4
|
+
|
|
5
|
+
* [Sidekiq middleware](https://github.com/mperham/sidekiq/wiki/Middleware)
|
|
6
|
+
* [InfluxDB client](https://github.com/influxdata/influxdb-ruby)
|
|
7
|
+
|
|
3
8
|
## Contributing Guidelines
|
|
4
9
|
|
|
10
|
+
1. We welcome addition of useful Sidekiq metrics and optimisations of the existing ones.
|
|
5
11
|
1. All code should be covered by tests. This library is used in high load Sidekiq deployments. Failures are unacceptable.
|
|
6
12
|
1. Code should be easy for an average Rubyist to understand.
|
|
7
13
|
1. Each pull request should contain changes for a single goal.
|
data/README.md
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
[](https://travis-ci.com/github/funbox/sidekiq-influxdb)
|
|
5
5
|
[](https://coveralls.io/github/funbox/sidekiq-influxdb)
|
|
6
6
|
|
|
7
|
-
[Sidekiq](https://github.com/mperham/sidekiq/wiki) middleware
|
|
7
|
+
[Sidekiq](https://github.com/mperham/sidekiq/wiki) server middleware
|
|
8
|
+
that writes job lifecycle events as points to an [InfluxDB](http://docs.influxdata.com/influxdb/v1.3/) database
|
|
9
|
+
(or any client with a compatible `write_point` API, including [InfluxDB 2](https://github.com/influxdata/influxdb-client-ruby)).
|
|
10
|
+
Also includes classes that write global Sidekiq metrics and queue metrics.
|
|
8
11
|
|
|
9
12
|
## Installation
|
|
10
13
|
|
|
@@ -16,7 +19,24 @@ bundle add sidekiq-influxdb
|
|
|
16
19
|
|
|
17
20
|
## Usage
|
|
18
21
|
|
|
19
|
-
Add included middleware to your application's Sidekiq middleware stack
|
|
22
|
+
Add included middleware to your application's Sidekiq middleware stack.
|
|
23
|
+
The following examples assume that you already have an InfluxDB client object
|
|
24
|
+
in the `influxdb` variable.
|
|
25
|
+
This will create a middleware with all defaults (suitable for most deployments):
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
# config/initializers/sidekiq.rb
|
|
29
|
+
|
|
30
|
+
require "sidekiq/middleware/server/influxdb"
|
|
31
|
+
|
|
32
|
+
Sidekiq.configure_server do |config|
|
|
33
|
+
config.server_middleware do |chain|
|
|
34
|
+
chain.add Sidekiq::Middleware::Server::InfluxDB, influxdb_client: influxdb
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
You can customize the middleware by passing more options:
|
|
20
40
|
|
|
21
41
|
```ruby
|
|
22
42
|
# config/initializers/sidekiq.rb
|
|
@@ -26,17 +46,41 @@ require "sidekiq/middleware/server/influxdb"
|
|
|
26
46
|
Sidekiq.configure_server do |config|
|
|
27
47
|
config.server_middleware do |chain|
|
|
28
48
|
chain.add Sidekiq::Middleware::Server::InfluxDB,
|
|
29
|
-
influxdb_client:
|
|
30
|
-
series_name: 'sidekiq_jobs',
|
|
31
|
-
retention_policy:
|
|
32
|
-
start_events: true,
|
|
33
|
-
tags: {
|
|
34
|
-
except: [
|
|
49
|
+
influxdb_client: influxdb,
|
|
50
|
+
series_name: 'sidekiq_jobs', # This is the default one.
|
|
51
|
+
retention_policy: 'rp_name', # In case you want to write metrics to a non-default RP.
|
|
52
|
+
start_events: true, # Whether or not you want to know when jobs started. See `event` tag description below.
|
|
53
|
+
tags: {application: 'MyApp'}, # Anything you need on top. **Make sure that tag values have low cardinality!**
|
|
54
|
+
except: [UnimportantJob], # These job classes will be executed without sending any metrics.
|
|
55
|
+
measurement_key: :values # Use :fields for InfluxDB 2 clients. Default: :values.
|
|
35
56
|
end
|
|
36
57
|
end
|
|
37
58
|
```
|
|
38
59
|
|
|
39
|
-
|
|
60
|
+
### Sidekiq 8
|
|
61
|
+
|
|
62
|
+
Sidekiq 8 stores `created_at` as epoch **milliseconds** (Integer).
|
|
63
|
+
Earlier versions used epoch **seconds** (Float).
|
|
64
|
+
The middleware detects the format automatically — no configuration change is required when upgrading Sidekiq.
|
|
65
|
+
|
|
66
|
+
### InfluxDB 2
|
|
67
|
+
|
|
68
|
+
The default payload key is `values` (InfluxDB 1.x and the [`influxdb`](https://github.com/influxdata/influxdb-ruby) gem).
|
|
69
|
+
InfluxDB 2 clients ([`influxdb-client`](https://github.com/influxdata/influxdb-client-ruby)) expect `fields` instead:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
chain.add Sidekiq::Middleware::Server::InfluxDB,
|
|
73
|
+
influxdb_client: my_influxdb2_client,
|
|
74
|
+
measurement_key: :fields
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Your client must implement `write_point(series_name, data, precision, retention)` and accept a hash with
|
|
78
|
+
`tags`, the configured measurement key (`values` or `fields`), and `timestamp`.
|
|
79
|
+
|
|
80
|
+
This library assumes that you already have an InfluxDB client object set up the way you like.
|
|
81
|
+
It does not try to create one for you.
|
|
82
|
+
If that is not the case, you can learn how to create a client
|
|
83
|
+
in [InfluxDB client documentation](https://github.com/influxdata/influxdb-ruby#creating-a-client).
|
|
40
84
|
|
|
41
85
|
**Warning:** This middleware is going to write _a lot_ of metrics.
|
|
42
86
|
Set up your InfluxDB client accordingly:
|
|
@@ -44,7 +88,7 @@ Set up your InfluxDB client accordingly:
|
|
|
44
88
|
* or install Telegraf, set up aggregation inside it, and set up InfluxDB client to send metrics to it,
|
|
45
89
|
* or both.
|
|
46
90
|
|
|
47
|
-
When you deploy this code, you will
|
|
91
|
+
When you deploy this code, you will have the following series in your InfluxDB database:
|
|
48
92
|
|
|
49
93
|
```
|
|
50
94
|
> select * from sidekiq_jobs
|
|
@@ -66,7 +110,7 @@ Tags (repetitive indexed data; for filtering and grouping by):
|
|
|
66
110
|
* `error` — if `event=error`, this tag contains the exception class name.
|
|
67
111
|
* Your own tags from the initializer.
|
|
68
112
|
|
|
69
|
-
Values (unique non-indexed data; for aggregation):
|
|
113
|
+
Values / fields (unique non-indexed data; for aggregation; the key name depends on `measurement_key`):
|
|
70
114
|
|
|
71
115
|
* `jid` — unique job ID.
|
|
72
116
|
* `creation_time` — job creation time.
|
|
@@ -95,15 +139,22 @@ Et cetera.
|
|
|
95
139
|
|
|
96
140
|
### Stats and Queues metrics
|
|
97
141
|
|
|
98
|
-
To collect metrics for task stats and queues, you need to run the following code periodically.
|
|
142
|
+
To collect metrics for task stats and queues, you need to run the following code periodically.
|
|
143
|
+
For example, you can use [Clockwork](https://rubygems.org/gems/clockwork) for that.
|
|
144
|
+
You can add settings like this to `clock.rb`:
|
|
99
145
|
|
|
100
146
|
```ruby
|
|
101
147
|
require "sidekiq/metrics/stats"
|
|
102
148
|
require "sidekiq/metrics/queues"
|
|
103
149
|
|
|
150
|
+
influx = InfluxDB::Client.new(options)
|
|
151
|
+
|
|
152
|
+
sidekiq_global_metrics = Sidekiq::Metrics::Stats.new(influxdb_client: influx)
|
|
153
|
+
sidekiq_queues_metrics = Sidekiq::Metrics::Queues.new(influxdb_client: influx)
|
|
154
|
+
|
|
104
155
|
every(1.minute, 'sidekiq_metrics') do
|
|
105
|
-
|
|
106
|
-
|
|
156
|
+
sidekiq_global_metrics.publish
|
|
157
|
+
sidekiq_queues_metrics.publish
|
|
107
158
|
end
|
|
108
159
|
```
|
|
109
160
|
|
|
@@ -133,7 +184,7 @@ Sidekiq::Metrics::Queues.new(
|
|
|
133
184
|
).publish
|
|
134
185
|
```
|
|
135
186
|
|
|
136
|
-
When you run the
|
|
187
|
+
When you run the code, you will have the following series in your InfluxDB database:
|
|
137
188
|
|
|
138
189
|
```
|
|
139
190
|
> select * from sidekiq_stats
|
|
@@ -159,24 +210,10 @@ time queue size
|
|
|
159
210
|
|
|
160
211
|
### Grafana
|
|
161
212
|
|
|
162
|
-
You can import
|
|
213
|
+
You can import a ready-made dashboard from [grafana_dashboard.json](grafana_dashboard.json).
|
|
163
214
|
|
|
164
215
|
## Development
|
|
165
216
|
|
|
166
|
-
|
|
167
|
-
* [InfluxDB client](https://github.com/influxdata/influxdb-ruby)
|
|
168
|
-
|
|
169
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
|
170
|
-
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
171
|
-
|
|
172
|
-
## Contributing
|
|
173
|
-
|
|
174
|
-
Bug reports and pull requests are welcome on GitHub at [github.com/funbox/sidekiq-influxdb](https://github.com/funbox/sidekiq-influxdb).
|
|
175
|
-
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
176
|
-
|
|
177
|
-
## Code of Conduct
|
|
178
|
-
|
|
179
|
-
Everyone interacting in the `Sidekiq::InfluxDB` project’s codebases, issue trackers,
|
|
180
|
-
chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/funbox/sidekiq-influxdb/blob/master/CODE_OF_CONDUCT.md).
|
|
217
|
+
See [Contributing Guidelines](CONTRIBUTING.md).
|
|
181
218
|
|
|
182
219
|
[](https://funbox.ru)
|
|
@@ -5,22 +5,17 @@ module Sidekiq
|
|
|
5
5
|
module Server
|
|
6
6
|
class InfluxDB
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@
|
|
18
|
-
@
|
|
19
|
-
@retention = retention_policy
|
|
20
|
-
@start_events = start_events
|
|
21
|
-
@tags = tags
|
|
22
|
-
@secret_agents = class_names(except)
|
|
23
|
-
@clock = clock
|
|
8
|
+
MILLISECONDS_PER_SECOND = 1000.0
|
|
9
|
+
|
|
10
|
+
def initialize(options = {})
|
|
11
|
+
@influxdb = options.fetch(:influxdb_client)
|
|
12
|
+
@series = options.fetch(:series_name, 'sidekiq_jobs')
|
|
13
|
+
@retention = options.fetch(:retention_policy, nil)
|
|
14
|
+
@start_events = options.fetch(:start_events, true)
|
|
15
|
+
@tags = options.fetch(:tags, {})
|
|
16
|
+
@secret_agents = class_names(options.fetch(:except, []))
|
|
17
|
+
@clock = options.fetch(:clock, -> { Time.now.to_f })
|
|
18
|
+
@measurement_key = options.fetch(:measurement_key, :values)
|
|
24
19
|
end
|
|
25
20
|
|
|
26
21
|
def call(_worker, msg, _queue)
|
|
@@ -30,49 +25,52 @@ module Sidekiq
|
|
|
30
25
|
end
|
|
31
26
|
|
|
32
27
|
started_at = @clock.call
|
|
33
|
-
waited = started_at - msg
|
|
34
|
-
record(started_at, msg, {event: 'start'}, {waited: waited}) if @start_events
|
|
28
|
+
waited = started_at - created_at_in_seconds(msg)
|
|
29
|
+
record(started_at, msg, { event: 'start' }, { waited: waited }) if @start_events
|
|
35
30
|
|
|
31
|
+
error = nil
|
|
36
32
|
begin
|
|
37
33
|
yield
|
|
38
|
-
tags = {event: 'finish'}
|
|
39
|
-
rescue => e
|
|
40
|
-
tags = {event: 'error', error: e.class.name}
|
|
34
|
+
tags = { event: 'finish' }
|
|
35
|
+
rescue StandardError => e
|
|
36
|
+
tags = { event: 'error', error: e.class.name }
|
|
37
|
+
error = e
|
|
41
38
|
end
|
|
42
39
|
|
|
43
40
|
finished_at = @clock.call
|
|
44
41
|
worked = finished_at - started_at
|
|
45
|
-
record(finished_at, msg, tags, {waited: waited, worked: worked, total: waited + worked})
|
|
42
|
+
record(finished_at, msg, tags, { waited: waited, worked: worked, total: waited + worked })
|
|
46
43
|
|
|
47
|
-
raise
|
|
44
|
+
raise error if error
|
|
48
45
|
end
|
|
49
46
|
|
|
50
47
|
private
|
|
51
48
|
|
|
52
49
|
def class_names(except)
|
|
53
|
-
Set.new([except].flatten.map
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def class_name(class_or_name)
|
|
57
|
-
class_or_name.name
|
|
58
|
-
rescue NoMethodError
|
|
59
|
-
class_or_name
|
|
50
|
+
Set.new([except].flatten.map(&:to_s))
|
|
60
51
|
end
|
|
61
52
|
|
|
62
53
|
def job_class_name(msg)
|
|
63
54
|
msg['wrapped'] || msg['class']
|
|
64
55
|
end
|
|
65
56
|
|
|
66
|
-
def
|
|
57
|
+
def created_at_in_seconds(msg)
|
|
58
|
+
created_at = msg['created_at']
|
|
59
|
+
return created_at unless created_at.is_a?(Integer)
|
|
60
|
+
|
|
61
|
+
created_at / MILLISECONDS_PER_SECOND
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def record(t, msg, tags, measurement = {})
|
|
67
65
|
save(
|
|
68
66
|
tags: {
|
|
69
67
|
class: job_class_name(msg),
|
|
70
68
|
queue: msg['queue']
|
|
71
69
|
}.merge(tags).merge(@tags),
|
|
72
|
-
|
|
70
|
+
@measurement_key => {
|
|
73
71
|
jid: msg['jid'],
|
|
74
|
-
creation_time: msg
|
|
75
|
-
}.merge(
|
|
72
|
+
creation_time: created_at_in_seconds(msg)
|
|
73
|
+
}.merge(measurement),
|
|
76
74
|
timestamp: in_correct_precision(t)
|
|
77
75
|
)
|
|
78
76
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sidekiq-influxdb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.4.
|
|
4
|
+
version: 1.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ilya Vassilevsky
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-06-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: influxdb
|