sidekiq_prometheus 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2382deff51bcba928543262df3da7a6c1acd5c90b79a9aa427349d1d190cdf3
4
- data.tar.gz: 905e723a9efa06ae33df67872e254af30a9f145fb2ef45dfd4bcf5bbe447c120
3
+ metadata.gz: 748f399320010ab1e703781c7a2c938b43c7d2a546218fe2a53397f60785be47
4
+ data.tar.gz: f7122e72637d68d0a5357308a6695994a0a6671569307e4223a1fd38efc624ba
5
5
  SHA512:
6
- metadata.gz: 35eb0aecd12715f0e2c29d500a89ee4ed33042f1a004fef4e8db62ddf942bcd85b1f41aac228f66e3d7e9bc29360871d7b0034d194a4057846a5afb5827093e4
7
- data.tar.gz: a7f66949d6db7dde393c77a19872cb2384be079988f3d598bf7031510d30c766d28311223c56fa54353e72b5b1f12ce65f986317bc97e37ed1a143fd1a65b068
6
+ metadata.gz: 598de4311c9de184940ead8482a150eb242cf610500404914a7f1ed3ce6eea8395021b1bb007d65567227d97056b8ebe1e392688ad5a7f40b4e760d90f71683b
7
+ data.tar.gz: b56334f343b2cce15e905a609ed11ac33ce5f2a42042bc6641ffe20444df2c26f5108ee275f76bca18ebcfd588321529a2d86e5edbb9d48acfff99fef1d4adb8
@@ -0,0 +1,39 @@
1
+ CHANGELOG
2
+ {{ range .Versions }}
3
+ <a name="{{ .Tag.Name }}"></a>
4
+ ## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }})
5
+
6
+ {{ range .CommitGroups -}}
7
+ ### {{ .Title }}
8
+
9
+ {{ range .Commits -}}
10
+ * {{ .Subject }}
11
+ {{ end }}
12
+ {{ end -}}
13
+
14
+ {{- if .RevertCommits -}}
15
+ ### Reverts
16
+
17
+ {{ range .RevertCommits -}}
18
+ * {{ .Revert.Header }}
19
+ {{ end }}
20
+ {{ end -}}
21
+
22
+ {{- if .MergeCommits -}}
23
+ ### Pull Requests
24
+
25
+ {{ range .MergeCommits -}}
26
+ * {{ .Header }}
27
+ {{ end }}
28
+ {{ end -}}
29
+
30
+ {{- if .NoteGroups -}}
31
+ {{ range .NoteGroups -}}
32
+ ### {{ .Title }}
33
+
34
+ {{ range .Notes }}
35
+ {{ .Body }}
36
+ {{ end }}
37
+ {{ end -}}
38
+ {{ end -}}
39
+ {{ end -}}
@@ -0,0 +1,27 @@
1
+ style: github
2
+ template: CHANGELOG.tpl.md
3
+ info:
4
+ title: CHANGELOG
5
+ repository_url: https://github.com/fastly/sidekiq-prometheus
6
+ options:
7
+ commits:
8
+ # filters:
9
+ # Type:
10
+ # - feat
11
+ # - fix
12
+ # - perf
13
+ # - refactor
14
+ commit_groups:
15
+ # title_maps:
16
+ # feat: Features
17
+ # fix: Bug Fixes
18
+ # perf: Performance Improvements
19
+ # refactor: Code Refactoring
20
+ header:
21
+ pattern: "^(\\w*)\\:\\s(.*)$"
22
+ pattern_maps:
23
+ - Type
24
+ - Subject
25
+ notes:
26
+ keywords:
27
+ - BREAKING CHANGE
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ CHANGELOG
2
+
3
+ <a name="v0.8.1"></a>
4
+ ## v0.8.1 (2019-02-08)
5
+
6
+ Initial Release!
data/README.md CHANGED
@@ -1,11 +1,15 @@
1
- # SidekiqPrometheus
1
+ # Sidekiq Prometheus
2
2
 
3
- Prometheus Instrumentation for Sidekiq.
4
3
 
5
- * Sidekiq server middleware for reporting job metrics
6
- * Global metrics reporter using the Sidekiq API for reporting Sidekiq cluster stats (requires Sidekiq::Enterprise)
7
- * Sidecar Rack server to provide scrape-able endpoint for Prometheus
4
+ [![Status](https://travis-ci.org/fastly/sidekiq-prometheus.svg?branch=master)](https://travis-ci.org/fastly/sidekiq-prometheus)
5
+ ![Gem](https://img.shields.io/gem/v/sidekiq_prometheus.svg?color=blue)
6
+ [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
7
+
8
+ Prometheus Instrumentation for Sidekiq. This gem provides:
8
9
 
10
+ * Sidekiq server middleware for reporting job metrics
11
+ * Global metrics reporter that uses the Sidekiq API for reporting Sidekiq cluster stats (*requires Sidekiq::Enterprise*)
12
+ * Sidecar Rack server to provide scrape-able endpoint for Prometheus. This allows for metrics to be reported without having to run a separate prometheus exporter process.
9
13
 
10
14
  ## Installation
11
15
 
@@ -31,35 +35,42 @@ To run with the defaults add this to your Sidekiq initializer
31
35
  SidekiqPrometheus.setup
32
36
  ```
33
37
 
34
- This will register metrics, start the global reporter (if available), and start the Rack server for scraping. The default port is 9357 but this is easily configurable.
38
+ This will register metrics, start the global reporter (if available), and start the Rack server for scraping. The default port is 9359 but this is easily configurable.
35
39
 
36
- If you are running multiple services that will be reporting Sidekiq metrics you will want to take advantage of the `base_labels` configuration option. For example:
40
+ Once Sidekiq server is running you can see your metrics or scrape them with Prometheus:
37
41
 
38
42
  ```
39
- SidekiqPrometheus.configure do |config|
40
- config.base_labels = { service: 'image_api' }
41
- config.metrics_port = 9090
42
- end
43
+ $curl http://localhost:9359/metrics
43
44
 
44
- # always call setup after configure.
45
- SidekiqPrometheus.setup
45
+ # TYPE sidekiq_job_count counter
46
+ # HELP sidekiq_job_count Count of Sidekiq jobs
47
+ # TYPE sidekiq_job_duration histogram
48
+ # HELP sidekiq_job_duration Sidekiq job processing duration
49
+
50
+ [etc]
46
51
  ```
47
52
 
48
- The call to `setup` is necessary as that it what registers the metrics and instruments Sidekiq.
49
- There is also a helper method: `SidekiqPrometheus.configure!` which can be used to configure and setup in one step:
53
+ [Full documentation](https://www.rubydoc.info/gems/sidekiq_prometheus)
50
54
 
51
- ```
52
- SidekiqPrometheus.configure! do |config|
53
- config.base_labels = { service: 'dogs_api' }
54
- end
55
+ ## Configuration
55
56
 
56
- # No need to call SidekiqPrometheus.setup because the bang method did it for us/
57
+ You can configure the gem by calling `configure`:
58
+
59
+ ```ruby
60
+ SidekiqPrometheus.configure do |config|
61
+ config.base_labels = { service: 'kubernandos_api' }
62
+ end
57
63
  ```
58
64
 
59
- Once sidekiq server is running you can see your metrics or scrape them with Prometheus:
65
+ `configure` will automatically call setup so
60
66
 
61
- ```
62
- curl http://localhost:8675/metrics
67
+ If you are running multiple services that will be reporting Sidekiq metrics you will want to take advantage of the `base_labels` configuration option. For example:
68
+
69
+ ```ruby
70
+ SidekiqPrometheus.configure do |config|
71
+ config.base_labels = { service: 'image_api' }
72
+ config.metrics_port = 9090
73
+ end
63
74
  ```
64
75
 
65
76
  #### Configuration options
@@ -67,11 +78,12 @@ curl http://localhost:8675/metrics
67
78
  * `base_labels`: Hash of labels that will be included with every metric when they are registered.
68
79
  * `gc_metrics_enabled`: Boolean that determines whether to record object allocation metrics per job. The default is `true`. Setting this to `false` if you don't need this metric.
69
80
  * `global_metrics_enabled`: Boolean that determines whether to report global metrics from the PeriodicMetrics reporter. When `true` this will report on a number of stats from the Sidekiq API for the cluster. This requires Sidekiq::Enterprise as the reporter uses the leader election functionality to ensure that only one worker per cluster is reporting metrics.
70
- * `periodic_metrics_enabled`: Boolean that determines whether to run the periodic metrics reporter. `PeriodicMetrics` runs a separate thread that reports on global metrics (if enabled) as well worker GC stats (if enabled). It reports metrics on the interval defined by `periodic_reporting_interval`. Defatuls to `true`.
81
+ * `periodic_metrics_enabled`: Boolean that determines whether to run the periodic metrics reporter. `PeriodicMetrics` runs a separate thread that reports on global metrics (if enabled) as well worker GC stats (if enabled). It reports metrics on the interval defined by `periodic_reporting_interval`. Defaults to `true`.
71
82
  * `periodic_reporting_interval`: interval in seconds for reporting periodic metrics. Default: `30`
72
- * `metrics_port`: Port on which the rack server will listen. Defaults to `9357`
83
+ * `metrics_port`: Port on which the rack server will listen. Defaults to `9359`
84
+ * `registry`: An instance of `Prometheus::Client::Registry`. If you have a registry with defined metrics you can use this option to pass in your registry.
73
85
 
74
- ```
86
+ ```ruby
75
87
  SidekiqPrometheus.configure do |config|
76
88
  config.base_labels = { service: 'myapp' }
77
89
  config.gc_metrics_enabled = false
@@ -84,7 +96,7 @@ end
84
96
 
85
97
  Custom labels may be added by defining the `prometheus_labels` method in the worker class:
86
98
 
87
- ```
99
+ ```ruby
88
100
  class SomeWorker
89
101
  include Sidekiq::Worker
90
102
 
@@ -133,7 +145,7 @@ These require `SidekiqPrometheus.gc_metrics_enabled? == true` and `SidekiqPromet
133
145
 
134
146
  These require `SidekiqPrometheus.global_metrics_enabled? == true` and `SidekiqPrometheus.periodic_metrics_enabled? == true`
135
147
 
136
- Periodic metric reporting relies onSidekiq Enterprise's leader election functionality ([Ent Leader Election ](https://github.com/mperham/sidekiq/wiki/Ent-Leader-Election))
148
+ Periodic metric reporting relies on Sidekiq Enterprise's leader election functionality ([Ent Leader Election ](https://github.com/mperham/sidekiq/wiki/Ent-Leader-Election))
137
149
  which ensures that metrics are only reported once per cluster.
138
150
 
139
151
  | Metric | Type | Description |
@@ -154,6 +166,78 @@ which ensures that metrics are only reported once per cluster.
154
166
 
155
167
  The global metrics are reported with the only the `base_labels` with the exception of `sidekiq_enqueued` which will add a `queue` label and record a metric per Sidekiq queue.
156
168
 
169
+ ## Custom Worker Metrics
170
+
171
+ There are a few different ways to register custom metrics with SidekiqPrometheus. Each custom metric should be defined as a Hash with the following form:
172
+
173
+ ```ruby
174
+ {
175
+ name: :metric_name,
176
+ type: :gauge,
177
+ docstring: 'description',
178
+ base_labels: { label_name: 'label_text' },
179
+ }
180
+ ```
181
+
182
+ * `:name` (required) - Unique name of the metric and should be a symbol.
183
+ * `:type` (required) - Prometheus metric type. Supported values are: `:counter`, `:gauge`, `:histogram`, and `:summary`.
184
+ * `:docstring` (required) - Human readable description of the metric.
185
+ * `:base_labels` (optional) - Hash of labels that will be applied to every instance of this metric.
186
+
187
+ #### Registering custom metrics:
188
+
189
+ Registering a set of custom metrics is done by defining `custom_metrics` in the `configure` block:
190
+
191
+ ```ruby
192
+ SidekiqPrometheus.configure do |config|
193
+ config.custom_metrics = [
194
+ { name: :imported_records, type: :counter, :docstring: 'Count of successfully imported records' },
195
+ { name: :failed_records, type: counter:, :docstring: 'Count of failed records' },
196
+ ]
197
+ end
198
+ ```
199
+
200
+ Metrics can also be registered directly. This must done *after* `SidekiqPrometheus.configure` or `setup` has been run.
201
+
202
+ ```ruby
203
+ SidekiqPrometheus::Metrics.register(name: :logged_in_users, type: :gauge, docstring: 'Logged in users')
204
+ ```
205
+
206
+ There is also a method to register more than one metric at a time:
207
+
208
+ ```ruby
209
+ customer_worker_metrics = [
210
+ {
211
+ name: :file_count, type: :counter, docstring: 'Number of active files',
212
+ name: :file_size, type: :gauge, docstring: 'Size of files in bytes',
213
+ }
214
+ ]
215
+
216
+ SidekiqPrometheus::Metrics.register_metrics(customer_worker_metrics)
217
+ ```
218
+
219
+ #### Using custom metrics:
220
+
221
+ Once metrics are registered they can be used in your Sidekiq workers.
222
+
223
+ ```ruby
224
+ class ImportWorker
225
+ include Sidekiq::Worker
226
+
227
+ LABELS = {}
228
+
229
+ def perform(*args)
230
+ # worker code
231
+
232
+ SidekiqPrometheus[:file_count].increment(LABELS, new_file_count)
233
+ SidekiqPrometheus[:file_size].set(LABELS, total_file_size)
234
+ end
235
+
236
+ end
237
+ ```
238
+
239
+ See the [documentation of the Prometheus::Client library](https://github.com/prometheus/client_ruby) for all of the available options for setting metric values.
240
+
157
241
  ## Development
158
242
 
159
243
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -162,7 +246,7 @@ To install this gem onto your local machine, run `bundle exec rake install`.
162
246
 
163
247
  ## Contributing
164
248
 
165
- Bug reports and pull requests are welcome on GitHub at https://github.com/fastly/sidekiq_prometheus. 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.
249
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fastly/sidekiq-prometheus. 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.
166
250
 
167
251
  ## Copyright
168
252
 
@@ -174,4 +258,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
174
258
 
175
259
  ## Code of Conduct
176
260
 
177
- Everyone interacting in the SidekiqPrometheus project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sidekiq_prometheus/blob/master/CODE_OF_CONDUCT.md).
261
+ Everyone interacting in the SidekiqPrometheus project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fastly/sidekiq-prometheus/blob/master/CODE_OF_CONDUCT.md).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SidekiqPrometheus
4
- VERSION = '0.8.1'
4
+ VERSION = '0.9.0'
5
5
  end
@@ -20,6 +20,21 @@ module SidekiqPrometheus
20
20
  # @return [Hash] Custom labels applied to specific metrics
21
21
  attr_accessor :custom_labels
22
22
 
23
+ # @return [Array] Custom metrics that will be registered on setup.
24
+ # @example
25
+ # [
26
+ # {
27
+ # name: :metric_name,
28
+ # type: :prometheus_metric_type,
29
+ # docstring: 'Description of the metric',
30
+ # base_labels : { label: 'value' },
31
+ # }
32
+ # ]
33
+ # @note Each element of the array is a hash and must have the required keys: `:name`, `:type`, and `:docstring`.
34
+ # The values for `:name` and `:type` should be symbols and `:docstring` should be a string.
35
+ # `base_labels` is optional and, if used, must be a hash of labels that will be included on every instance of this metric.
36
+ attr_accessor :custom_metrics
37
+
23
38
  # @return [Boolean] Setting to control enabling/disabling GC metrics. Default: true
24
39
  attr_accessor :gc_metrics_enabled
25
40
 
@@ -42,6 +57,9 @@ module SidekiqPrometheus
42
57
  # Orverride the default Prometheus Metric Registry
43
58
  # @return [Prometheus::Client::Registry]
44
59
  attr_writer :registry
60
+
61
+ # @private
62
+ attr_writer :setup_complete
45
63
  end
46
64
 
47
65
  self.gc_metrics_enabled = true
@@ -50,6 +68,7 @@ module SidekiqPrometheus
50
68
  self.periodic_reporting_interval = 30
51
69
  self.metrics_port = 9359
52
70
  self.custom_labels = {}
71
+ self.custom_metrics = []
53
72
 
54
73
  module_function
55
74
 
@@ -60,7 +79,7 @@ module SidekiqPrometheus
60
79
  end
61
80
 
62
81
  ##
63
- # Configure SidekiqPrometheus
82
+ # Configure SidekiqPrometheus and setup for reporting
64
83
  # @example
65
84
  # SidekiqPrometheus.configure do |config|
66
85
  # config.base_labels = { service: 'images_api' }
@@ -69,16 +88,11 @@ module SidekiqPrometheus
69
88
  # end
70
89
  def configure
71
90
  yield self
72
- end
73
-
74
- ##
75
- # Configure and call setup immediately after
76
- def configure!
77
- yield self
78
91
  setup
79
92
  end
80
93
 
81
- ##
94
+ alias configure! configure
95
+
82
96
  # Helper method for +gc_metrics_enabled+ configuration setting
83
97
  # @return [Boolean] defaults to true
84
98
  def gc_metrics_enabled?
@@ -117,17 +131,33 @@ module SidekiqPrometheus
117
131
  # Prometheus client metric registry
118
132
  # @return [Prometheus::Client::Registry]
119
133
  def registry
120
- @registry ||= client.registry
134
+ @registry ||= client::Registry.new
135
+ end
136
+
137
+ ##
138
+ # Register custom metrics
139
+ # Internal method called by +setup+. This method should not be called from application code in most cases.
140
+ def register_custom_metrics
141
+ return if custom_metrics.empty?
142
+
143
+ raise SidekiqPrometheus::Error, 'custom_metrics is not an array.' unless custom_metrics.is_a?(Array)
144
+
145
+ SidekiqPrometheus::Metrics.register_metrics(custom_metrics)
121
146
  end
122
147
 
123
148
  ##
124
149
  # register metrics and instrument sidekiq
125
150
  def setup
151
+ return false if @setup_complete
126
152
  SidekiqPrometheus::Metrics.register_sidekiq_job_metrics
127
153
  SidekiqPrometheus::Metrics.register_sidekiq_gc_metric if gc_metrics_enabled?
128
154
  SidekiqPrometheus::Metrics.register_sidekiq_worker_gc_metrics if gc_metrics_enabled? && periodic_metrics_enabled?
129
155
  SidekiqPrometheus::Metrics.register_sidekiq_global_metrics if global_metrics_enabled? && periodic_metrics_enabled?
156
+ register_custom_metrics
157
+
130
158
  sidekiq_setup
159
+
160
+ self.setup_complete = true
131
161
  end
132
162
 
133
163
  ##
@@ -163,14 +193,10 @@ module SidekiqPrometheus
163
193
  )
164
194
  end
165
195
  end
166
-
167
- private
168
-
169
- def metrics
170
- SidekiqPrometheus::Metrics
171
- end
172
196
  end
173
197
 
198
+ class SidekiqPrometheus::Error < StandardError; end
199
+
174
200
  require 'sidekiq_prometheus/job_metrics'
175
201
  require 'sidekiq_prometheus/metrics'
176
202
  require 'sidekiq_prometheus/periodic_metrics'
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ['lib']
23
23
 
24
- spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'bundler'
25
25
  spec.add_development_dependency 'pry'
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
27
27
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_prometheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Eklund
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-08 00:00:00.000000000 Z
11
+ date: 2019-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pry
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -129,9 +129,12 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
+ - ".chglog/CHANGELOG.tpl.md"
133
+ - ".chglog/config.yml"
132
134
  - ".gitignore"
133
135
  - ".rubocop.yml"
134
136
  - ".travis.yml"
137
+ - CHANGELOG.md
135
138
  - CODE_OF_CONDUCT.md
136
139
  - Gemfile
137
140
  - LICENSE.txt
@@ -165,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
168
  version: '0'
166
169
  requirements: []
167
170
  rubyforge_project:
168
- rubygems_version: 2.7.3
171
+ rubygems_version: 2.7.9
169
172
  signing_key:
170
173
  specification_version: 4
171
174
  summary: Prometheus Instrumentation for Sidekiq