sidekiq-instrument 0.6.1 → 0.7.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: 2a5bba582f4b34766f77e4c9fb0a7c60e026111186fd3807f23c027f421e420d
4
- data.tar.gz: 6404eabed92c2b7e6a2fc4c125dcc9e0b81ab35e2db1a98a9175fcb533a0bd3b
3
+ metadata.gz: c7de17bf15cf561935e482b332b89974f84409e0c1bfa00a544503ce95bcfd8d
4
+ data.tar.gz: 1732bbfc31fc7c04fe066613845f9e81917873f2fc25ae93ec960aba611c347d
5
5
  SHA512:
6
- metadata.gz: 458c593c2cce45ec37a5c659b32d340f1244c0eed14d3c3bb9286d9b4208d095c4ab988b2c4761dbd4499b1f5a68ddf813b223536bc2dcbfcc055ceacd418728
7
- data.tar.gz: 1c825fa318f119418d6b93bf3511f433451bb004281bfc1131c553e57e0f421adc0d0b43f211aa59ea21b5d0846cc123288068c49eb96fecc26ff3433010eba0
6
+ metadata.gz: 65c1f85a4c9a13b4cd2c3585712af41b07a8979f2a736c44d971fc5175d737f7a7cb6104d6383af714d94273a41e017bae361e29f7381587294d463432bf7551
7
+ data.tar.gz: b1f25cab5729fe6bcb76a81556fc18583ab800b3dd2a66c50c9fea3834abcf1a422aabb06bc6d5ef39b69be4675ab35e1dab9c53e43920e377888fd91aa79b2d
data/README.md CHANGED
@@ -84,6 +84,12 @@ For each job, the following metrics will be reported:
84
84
  4. **shared.sidekiq._queue_._job_.error**: counter incremented each time a
85
85
  job fails.
86
86
 
87
+ For job retry attempts, the above 4 metrics will still be reported but the enqueue/dequeue metrics
88
+ will have a `.retry` appended:
89
+
90
+ 1. **shared.sidekiq._queue_._job_.enqueue.retry**
91
+ 2. **shared.sidekiq._queue_._job_.dequeue.retry**
92
+
87
93
  The metric names can be changed by overriding the `statsd_metric_name`
88
94
  method in your worker classes.
89
95
 
@@ -92,7 +98,7 @@ For each queue, the following metrics will be reported:
92
98
  2. **shared.sidekiq._queue_.latency**: gauge of how long the oldest job has been in the queue
93
99
 
94
100
  For each worker, the following metrics and tags will be reported:
95
- 1. **sidekiq.worker_metrics.inqueue.#{key}**: number of jobs "in queue" per worker, uses redis to track increment/decrement
101
+ 1. **sidekiq.worker_metrics.in_queue.#{key}**: number of jobs "in queue" per worker, uses redis to track increment/decrement (**this metric is currently inaccurate**)
96
102
 
97
103
  ## DogStatsD Keys
98
104
  For each job, the following metrics and tags will be reported:
@@ -106,14 +112,23 @@ For each job, the following metrics and tags will be reported:
106
112
  4. **sidekiq.error (tags: {queue: _queue_, worker: _job_})**: counter incremented each time a
107
113
  job fails.
108
114
 
115
+ For job retry attempts, the above 4 metrics will still be reported but the enqueue/dequeue metrics
116
+ will have a `.retry` appended:
117
+
118
+ 1. **sidekiq.enqueue.retry (tags: {queue: _queue_, worker: _job_})**
119
+ 2. **sidekiq.dequeue.retry (tags: {queue: _queue_, worker: _job_})**
120
+
109
121
  For each queue, the following metrics and tags will be reported:
110
122
  1. **sidekiq.queue.size (tags: {queue: _queue_})**: gauge of how many jobs are in the queue
111
123
  2. **sidekiq.queue.latency (tags: {queue: _queue_})**: gauge of how long the oldest job has been in the queue
112
124
 
113
125
  For each worker, the following metrics and tags will be reported:
114
- 1. **sidekiq.worker_metrics.inqueue.#{key}**: number of jobs "in queue" per worker, uses redis to track increment/decrement
126
+ 1. **sidekiq.worker_metrics.in_queue.#{key}**: number of jobs "in queue" per worker, uses redis to track increment/decrement (**this metric is currently inaccurate**)
115
127
 
116
128
  ## Worker
129
+
130
+ **WARNING: The metrics reported by this Worker are currently inaccurate.**
131
+
117
132
  There is a worker, `Sidekiq::Instrument::Worker`, that submits gauges
118
133
  for various interesting statistics; namely, the bulk of the information in `Sidekiq::Stats`
119
134
  and the sizes of each individual queue. While the worker class is a fully valid Sidekiq worker,
@@ -7,16 +7,24 @@ module Sidekiq::Instrument
7
7
  class ClientMiddleware
8
8
  include Sidekiq::Instrument::MetricNames
9
9
 
10
- def call(worker_class, job, queue, redis_pool)
10
+ def call(worker_class, job, queue, _redis_pool)
11
11
  # worker_class is a const in sidekiq >= 6.x
12
12
  klass = Object.const_get(worker_class.to_s)
13
13
  class_instance = klass.new
14
- Statter.statsd.increment(metric_name(class_instance, 'enqueue'))
15
- Statter.dogstatsd&.increment('sidekiq.enqueue', worker_dog_options(class_instance))
16
- WorkerMetrics.trace_workers_increment_counter(klass.name.underscore, redis_pool)
17
- result = yield
14
+
15
+ # This is needed because the ClientMiddleware is called twice for scheduled jobs
16
+ # - Once when it gets scheduled
17
+ # - Once when it gets dequeued for processing
18
+ # We only want to increment the enqueue metric when the job is scheduled and
19
+ # Sidekiq::Context.current[:class] is only ever set when the job is scheduled
20
+ if Sidekiq::Context.current[:class].present?
21
+ WorkerMetrics.trace_workers_increment_counter(klass.name.underscore)
22
+ Statter.statsd.increment(metric_name(class_instance, 'enqueue'))
23
+ Statter.dogstatsd&.increment('sidekiq.enqueue', worker_dog_options(class_instance))
24
+ end
25
+
18
26
  Statter.dogstatsd&.flush(sync: true)
19
- result
27
+ yield
20
28
  end
21
29
  end
22
30
  end
@@ -7,22 +7,40 @@ module Sidekiq::Instrument
7
7
  class ServerMiddleware
8
8
  include Sidekiq::Instrument::MetricNames
9
9
 
10
- def call(worker, job, queue, &block)
11
- Statter.statsd.increment(metric_name(worker, 'dequeue'))
12
- Statter.dogstatsd&.increment('sidekiq.dequeue', worker_dog_options(worker))
10
+ def call(worker, job, _queue, &block)
11
+ dequeue_string = is_retry(job) ? 'dequeue.retry' : 'dequeue'
12
+ Statter.dogstatsd&.increment("sidekiq.#{dequeue_string}", worker_dog_options(worker))
13
+ Statter.statsd.increment(metric_name(worker, dequeue_string))
13
14
 
14
15
  start_time = Time.now
15
- WorkerMetrics.trace_workers_decrement_counter(worker.class.to_s.underscore)
16
16
  yield block
17
17
  execution_time_ms = (Time.now - start_time) * 1000
18
18
  Statter.statsd.measure(metric_name(worker, 'runtime'), execution_time_ms)
19
19
  Statter.dogstatsd&.timing('sidekiq.runtime', execution_time_ms, worker_dog_options(worker))
20
20
  rescue StandardError => e
21
+ # if we have retries left, increment the enqueue.retry counter to indicate the job is going back on the queue
22
+ if max_retries(worker) > current_retries(job) + 1
23
+ WorkerMetrics.trace_workers_increment_counter(worker.class.to_s.underscore)
24
+ Statter.dogstatsd&.increment('sidekiq.enqueue.retry', worker_dog_options(worker))
25
+ end
26
+
21
27
  Statter.statsd.increment(metric_name(worker, 'error'))
22
28
  Statter.dogstatsd&.increment('sidekiq.error', worker_dog_options(worker))
23
29
  raise e
24
30
  ensure
31
+ WorkerMetrics.trace_workers_decrement_counter(worker.class.to_s.underscore)
25
32
  Statter.dogstatsd&.flush(sync: true)
26
33
  end
34
+
35
+ private
36
+
37
+ # returns -1 if no retries have been attempted
38
+ def current_retries(job)
39
+ job["retry_count"] || -1
40
+ end
41
+
42
+ def is_retry(job)
43
+ current_retries(job) >= 0
44
+ end
27
45
  end
28
46
  end
@@ -12,6 +12,13 @@ module Sidekiq::Instrument
12
12
  { tags: ["queue:#{queue_name(worker)}", "worker:#{underscore(class_name(worker))}"] }
13
13
  end
14
14
 
15
+ def max_retries(worker)
16
+ retries = worker.class.get_sidekiq_options['retry'] || Sidekiq[:max_retries]
17
+ return Sidekiq[:max_retries] if retries.to_s.eql?("true")
18
+ return 0 if retries.eql?("false")
19
+ retries
20
+ end
21
+
15
22
  private
16
23
 
17
24
  def queue_name(worker)
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Instrument
3
- VERSION = '0.6.1'
3
+ VERSION = '0.7.0'
4
4
  end
5
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sidekiq'
2
4
  require 'sidekiq/api'
3
5
 
@@ -13,7 +15,7 @@ module Sidekiq::Instrument
13
15
  workers_size: :workers,
14
16
  enqueued: :pending,
15
17
  failed: nil
16
- }
18
+ }.freeze
17
19
 
18
20
  def perform
19
21
  info = Sidekiq::Stats.new
@@ -64,8 +66,8 @@ module Sidekiq::Instrument
64
66
  return unless WorkerMetrics.enabled
65
67
 
66
68
  WorkerMetrics.workers_in_queue.each do |key, value|
67
- Statter.statsd.gauge("shared.sidekiq.worker_metrics.inqueue.#{key}", value)
68
- Statter.dogstatsd&.gauge("shared.sidekiq.worker_metrics.inqueue", value, tags: ["worker:#{key}"])
69
+ Statter.statsd.gauge("shared.sidekiq.worker_metrics.in_queue.#{key}", value)
70
+ Statter.dogstatsd&.gauge('shared.sidekiq.worker_metrics.in_queue', value, tags: ["worker:#{key}"])
69
71
  end
70
72
  end
71
73
  end
@@ -10,7 +10,7 @@ module Sidekiq
10
10
  class_attribute :enabled, :namespace
11
11
 
12
12
  class << self
13
- def trace_workers_increment_counter(klass_name, sidekiq_redis_pool_user)
13
+ def trace_workers_increment_counter(klass_name)
14
14
  return unless enabled?
15
15
 
16
16
  Sidekiq.redis do |redis|
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'bundler', '~> 2.0', '>= 2.0.2'
26
26
  spec.add_development_dependency 'rake', '~> 12.0'
27
27
  spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop', '~> 1.0'
28
29
  spec.add_development_dependency 'pry-byebug', '~> 3.4'
29
30
  spec.add_development_dependency 'simplecov'
30
31
  spec.add_development_dependency 'simplecov-cobertura'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-instrument
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loan Application Services
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-12 00:00:00.000000000 Z
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -140,6 +140,20 @@ dependencies:
140
140
  - - "~>"
141
141
  - !ruby/object:Gem::Version
142
142
  version: '3.0'
143
+ - !ruby/object:Gem::Dependency
144
+ name: rubocop
145
+ requirement: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: '1.0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - "~>"
155
+ - !ruby/object:Gem::Version
156
+ version: '1.0'
143
157
  - !ruby/object:Gem::Dependency
144
158
  name: pry-byebug
145
159
  requirement: !ruby/object:Gem::Requirement
@@ -182,7 +196,7 @@ dependencies:
182
196
  - - ">="
183
197
  - !ruby/object:Gem::Version
184
198
  version: '0'
185
- description:
199
+ description:
186
200
  email:
187
201
  - application_services@enova.com
188
202
  executables: []
@@ -213,7 +227,7 @@ homepage: https://github.com/enova/sidekiq-instrument
213
227
  licenses:
214
228
  - MIT
215
229
  metadata: {}
216
- post_install_message:
230
+ post_install_message:
217
231
  rdoc_options: []
218
232
  require_paths:
219
233
  - lib
@@ -228,8 +242,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
242
  - !ruby/object:Gem::Version
229
243
  version: '0'
230
244
  requirements: []
231
- rubygems_version: 3.0.3
232
- signing_key:
245
+ rubygems_version: 3.0.3.1
246
+ signing_key:
233
247
  specification_version: 4
234
248
  summary: StatsD & DogStatsD Instrumentation for Sidekiq
235
249
  test_files: []