sidekiq-instrument 0.5.4 → 0.5.6

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: b4112a344ff91a81260af18c47e66fbd1806b9340f9bf0f632d84b405b308c39
4
- data.tar.gz: 8fddd4f0232f9c9a4b41ec29428e1a39a84c85dacdea09d1b60a0d535af4ff92
3
+ metadata.gz: 72c5cdbcff6df0e0af5fafd832a738c94ca639713fe370c3543c9b01907270c6
4
+ data.tar.gz: 7a785e4013569f3857f404fcf6cf38656c7c0eae57e20d8154d7d9c322ddb27b
5
5
  SHA512:
6
- metadata.gz: b75d3188a1b15368dc70d3f9fca27844f7a8c89dc52f7d632286c9e05268004c969f48ac37b5d11a9747c64342c74723e38873b0616782c8c19e082ba91531c9
7
- data.tar.gz: 5508859e81ef17cda16e7582f718f5e36b56ccc836895267927f96d997366340071e035f106252d6b0a9d336c013e451f82222284166b96ee0d26b170a149b9f
6
+ metadata.gz: b671f7aee1e48345c8e8eda768aef308782c37bc3990075a0e027fc2c3d2eb0ef9b216df928813c592db3d9d0a32c49062c145a9aff35321cd4e4e1bb6fea337
7
+ data.tar.gz: 6906d370408c343e8cc283fcc61a4cf5315b6ead830108a88d27608da8c244315a790248f5301d7d448664625b02325d741ecd340d861d7cef0935066aad13d8
@@ -15,7 +15,7 @@ jobs:
15
15
  strategy:
16
16
  matrix:
17
17
  ruby-version: [2.6, 2.7, 3.0]
18
- redis-version: [4, 5, 6]
18
+ redis-version: [7]
19
19
 
20
20
  steps:
21
21
  - name: Checkout project
@@ -28,7 +28,7 @@ jobs:
28
28
  bundler-cache: true # runs 'bundle install' and caches installed gems automatically
29
29
 
30
30
  - name: Start Redis
31
- uses: supercharge/redis-github-action@1.4.0
31
+ uses: supercharge/redis-github-action@1.5.0
32
32
  with:
33
33
  redis-version: ${{ matrix.redis-version }}
34
34
 
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sidekiq/instrument/mixin'
4
+ require 'active_support/core_ext/string/inflections'
2
5
 
3
6
  module Sidekiq::Instrument
4
7
  class ClientMiddleware
@@ -10,6 +13,7 @@ module Sidekiq::Instrument
10
13
  class_instance = klass.new
11
14
  Statter.statsd.increment(metric_name(class_instance, 'enqueue'))
12
15
  Statter.dogstatsd&.increment('sidekiq.enqueue', worker_dog_options(class_instance))
16
+ WorkerMetrics.trace_workers_increment_counter(klass.name.underscore, redis_pool)
13
17
  result = yield
14
18
  Statter.dogstatsd&.flush(sync: true)
15
19
  result
@@ -0,0 +1,14 @@
1
+ require "active_support/core_ext/class/attribute"
2
+ require "statsd/instrument"
3
+
4
+ require "sidekiq/instrument/statter"
5
+ require "sidekiq/instrument/version"
6
+ require "sidekiq/instrument/worker"
7
+ require "sidekiq/instrument/middleware/client"
8
+ require "sidekiq/instrument/middleware/server"
9
+ require "sidekiq/instrument/worker_metrics"
10
+
11
+ module Sidekiq
12
+ module Instrument
13
+ end
14
+ end
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sidekiq/instrument/mixin'
4
+ require 'active_support/core_ext/string/inflections'
2
5
 
3
6
  module Sidekiq::Instrument
4
7
  class ServerMiddleware
@@ -9,6 +12,7 @@ module Sidekiq::Instrument
9
12
  Statter.dogstatsd&.increment('sidekiq.dequeue', worker_dog_options(worker))
10
13
 
11
14
  start_time = Time.now
15
+ WorkerMetrics.trace_workers_decrement_counter(worker.class.to_s.underscore)
12
16
  yield block
13
17
  execution_time_ms = (Time.now - start_time) * 1000
14
18
  Statter.statsd.measure(metric_name(worker, 'runtime'), execution_time_ms)
@@ -22,4 +26,3 @@ module Sidekiq::Instrument
22
26
  end
23
27
  end
24
28
  end
25
-
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Instrument
3
- VERSION = "0.5.4"
3
+ VERSION = '0.5.6'
4
4
  end
5
5
  end
@@ -9,10 +9,10 @@ module Sidekiq::Instrument
9
9
  # (i.e. the metrics will reported as :processed, :workers, :pending, and :failed).
10
10
  # Feel free to override.
11
11
  METRIC_NAMES = {
12
- processed: nil,
12
+ processed: nil,
13
13
  workers_size: :workers,
14
- enqueued: :pending,
15
- failed: nil
14
+ enqueued: :pending,
15
+ failed: nil
16
16
  }
17
17
 
18
18
  def perform
@@ -28,18 +28,45 @@ module Sidekiq::Instrument
28
28
  working = Sidekiq::Workers.new.count
29
29
  Statter.statsd.gauge('shared.sidekiq.stats.working', working)
30
30
  Statter.dogstatsd&.gauge('sidekiq.working', working)
31
-
32
- info.queues.each do |name, size|
33
- Statter.statsd.gauge("shared.sidekiq.#{name}.size", size)
34
- Statter.dogstatsd&.gauge('sidekiq.queue.size', size, tags: ["queue:#{name}"])
35
- end
36
-
31
+ send_worker_metrics
37
32
  Sidekiq::Queue.all.each do |queue|
33
+ Statter.statsd.gauge("shared.sidekiq.#{queue.name}.size", queue.size)
34
+ Statter.dogstatsd&.gauge('sidekiq.queue.size', queue.size, tags: dd_tags(queue))
35
+
38
36
  Statter.statsd.gauge("shared.sidekiq.#{queue.name}.latency", queue.latency)
39
- Statter.dogstatsd&.gauge('sidekiq.queue.latency', queue.latency, tags: ["queue:#{queue.name}"])
37
+ Statter.dogstatsd&.gauge('sidekiq.queue.latency', queue.latency, tags: dd_tags(queue))
40
38
  end
41
39
 
42
40
  Statter.dogstatsd&.flush(sync: true)
43
41
  end
42
+
43
+ private
44
+
45
+ # @param [Sidekiq::Queue] queue used for stats emission
46
+ # @return [Array<String>] an array of tags
47
+ # @example this method can be override to add more tags
48
+ # class MyStatsWorker < Sidekiq::Instrument::Worker
49
+ # private
50
+ #
51
+ # def dd_tags(queue)
52
+ # custom_tags = []
53
+ # queue_type = queue.name.match?(/readonly$/) ? 'read_only' : 'regular'
54
+ # custom_tags << "queue_type:#{queue_type}"
55
+ #
56
+ # super(queue) | custom_tags
57
+ # end
58
+ # end
59
+ def dd_tags(queue)
60
+ ["queue:#{queue.name}"]
61
+ end
62
+
63
+ def send_worker_metrics
64
+ return unless WorkerMetrics.enabled
65
+
66
+ WorkerMetrics.workers_in_queue.each do |key, value|
67
+ Statter.statsd.gauge("shared.sidekiq.trace.inqueue.#{key}", value)
68
+ Statter.dogstatsd&.gauge("shared.sidekiq.trace.inqueue.#{key}", value)
69
+ end
70
+ end
44
71
  end
45
72
  end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+ require 'redis-client'
5
+ module Sidekiq
6
+ module Instrument
7
+ # Stores worker count with a key sidekiq_instrument_trace_workers:#{namespace}:in_queue
8
+ # Values are hash having keys as worker names.
9
+ class WorkerMetrics
10
+ class_attribute :enabled, :namespace, :redis_config
11
+
12
+ class_attribute :redis_password
13
+
14
+ class << self
15
+ def redis_pool_user
16
+ @redis_pool_user ||= begin
17
+ redis_client_config = RedisClient.config(**redis_config)
18
+ @redis = redis_client_config.new_pool(
19
+ timeout: 0.5, size: Integer(ENV.fetch('RAILS_MAX_THREADS', 5))
20
+ )
21
+ end
22
+ end
23
+
24
+ def reset_redis
25
+ @redis = nil
26
+ end
27
+
28
+ def trace_workers_increment_counter(klass_name, sidekiq_redis_pool_user)
29
+ return unless enabled?
30
+
31
+ if redis_config?
32
+ redis_pool_user.with do |redis|
33
+ redis.call 'HINCRBY', worker_metric_name, klass_name, 1
34
+ end
35
+ else
36
+ sidekiq_redis_pool_user.with do |redis|
37
+ redis.hincrby worker_metric_name, klass_name, 1
38
+ end
39
+ end
40
+ end
41
+
42
+ def trace_workers_decrement_counter(klass_name)
43
+ return unless enabled?
44
+
45
+ if redis_config?
46
+ redis_pool_user.with do |redis|
47
+ redis.call 'HINCRBY', worker_metric_name, klass_name, -1
48
+ end
49
+ else
50
+ Sidekiq.redis do |redis|
51
+ redis.hincrby worker_metric_name, klass_name, -1
52
+ end
53
+ end
54
+ end
55
+
56
+ def reset_counters
57
+ return unless enabled?
58
+
59
+ if redis_config?
60
+ redis_pool_user.with do |redis|
61
+ all_keys = redis.call 'HGETALL', worker_metric_name
62
+ redis.call 'HDEL', worker_metric_name, all_keys.keys
63
+ end
64
+ else
65
+ Sidekiq.redis do |redis|
66
+ all_keys = redis.hgetall worker_metric_name
67
+ redis.hdel worker_metric_name, all_keys.keys
68
+ end
69
+ end
70
+ end
71
+
72
+ def reset_counter(key)
73
+ return unless enabled?
74
+
75
+ if redis_config?
76
+ redis_pool_user.with do |redis|
77
+ redis.call 'HDEL', worker_metric_name, key
78
+ end
79
+ else
80
+ Sidekiq.redis do |redis|
81
+ redis.hdel worker_metric_name, key
82
+ end
83
+ end
84
+ end
85
+
86
+ def workers_in_queue
87
+ return unless enabled?
88
+
89
+ if redis_config?
90
+ redis_pool_user.with do |redis|
91
+ redis.call 'HGETALL', worker_metric_name
92
+ end
93
+ else
94
+ Sidekiq.redis do |redis|
95
+ redis.hgetall worker_metric_name
96
+ end
97
+ end
98
+ end
99
+
100
+ def worker_metric_name
101
+ "sidekiq_instrument_trace_workers:#{namespace}:in_queue"
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -6,6 +6,7 @@ require "sidekiq/instrument/version"
6
6
  require "sidekiq/instrument/worker"
7
7
  require "sidekiq/instrument/middleware/client"
8
8
  require "sidekiq/instrument/middleware/server"
9
+ require "sidekiq/instrument/worker_metrics"
9
10
 
10
11
  module Sidekiq
11
12
  module Instrument
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_dependency 'statsd-instrument', '>= 2.0.4'
21
21
  spec.add_dependency 'dogstatsd-ruby', '~> 5.5'
22
22
  spec.add_dependency 'activesupport', '>= 5.1', '< 7'
23
+ spec.add_dependency "redis-client", ">= 0.11.0"
23
24
 
24
25
  spec.add_development_dependency 'bundler', '~> 2.0', '>= 2.0.2'
25
26
  spec.add_development_dependency 'rake', '~> 12.0'
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.5.4
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loan Application Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-17 00:00:00.000000000 Z
11
+ date: 2023-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -78,6 +78,20 @@ dependencies:
78
78
  - - "<"
79
79
  - !ruby/object:Gem::Version
80
80
  version: '7'
81
+ - !ruby/object:Gem::Dependency
82
+ name: redis-client
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 0.11.0
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 0.11.0
81
95
  - !ruby/object:Gem::Dependency
82
96
  name: bundler
83
97
  requirement: !ruby/object:Gem::Requirement
@@ -187,11 +201,13 @@ files:
187
201
  - bin/setup
188
202
  - lib/sidekiq/instrument.rb
189
203
  - lib/sidekiq/instrument/middleware/client.rb
204
+ - lib/sidekiq/instrument/middleware/instrument.rb
190
205
  - lib/sidekiq/instrument/middleware/server.rb
191
206
  - lib/sidekiq/instrument/mixin.rb
192
207
  - lib/sidekiq/instrument/statter.rb
193
208
  - lib/sidekiq/instrument/version.rb
194
209
  - lib/sidekiq/instrument/worker.rb
210
+ - lib/sidekiq/instrument/worker_metrics.rb
195
211
  - sidekiq-instrument.gemspec
196
212
  homepage: https://github.com/enova/sidekiq-instrument
197
213
  licenses:
@@ -212,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
228
  - !ruby/object:Gem::Version
213
229
  version: '0'
214
230
  requirements: []
215
- rubygems_version: 3.1.6
231
+ rubygems_version: 3.0.3
216
232
  signing_key:
217
233
  specification_version: 4
218
234
  summary: StatsD & DogStatsD Instrumentation for Sidekiq