-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stats for Workers in queue (#21)
* Add stats for Workers in queue * review * redis_version update * specify host port * upgrade workflow step * more specs * execute tests only for redis 7 * fix reset_counters --------- Co-authored-by: Manoj Naduviledath <[email protected]>
- Loading branch information
Showing
12 changed files
with
298 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "active_support/core_ext/class/attribute" | ||
require "statsd/instrument" | ||
|
||
require "sidekiq/instrument/statter" | ||
require "sidekiq/instrument/version" | ||
require "sidekiq/instrument/worker" | ||
require "sidekiq/instrument/middleware/client" | ||
require "sidekiq/instrument/middleware/server" | ||
require "sidekiq/instrument/worker_metrics" | ||
|
||
module Sidekiq | ||
module Instrument | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Sidekiq | ||
module Instrument | ||
VERSION = '0.5.5' | ||
VERSION = '0.5.6' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'redis' | ||
require 'redis-client' | ||
module Sidekiq | ||
module Instrument | ||
# Stores worker count with a key sidekiq_instrument_trace_workers:#{namespace}:in_queue | ||
# Values are hash having keys as worker names. | ||
class WorkerMetrics | ||
class_attribute :enabled, :namespace, :redis_config | ||
|
||
class_attribute :redis_password | ||
|
||
class << self | ||
def redis_pool_user | ||
@redis_pool_user ||= begin | ||
redis_client_config = RedisClient.config(**redis_config) | ||
@redis = redis_client_config.new_pool( | ||
timeout: 0.5, size: Integer(ENV.fetch('RAILS_MAX_THREADS', 5)) | ||
) | ||
end | ||
end | ||
|
||
def reset_redis | ||
@redis = nil | ||
end | ||
|
||
def trace_workers_increment_counter(klass_name, sidekiq_redis_pool_user) | ||
return unless enabled? | ||
|
||
if redis_config? | ||
redis_pool_user.with do |redis| | ||
redis.call 'HINCRBY', worker_metric_name, klass_name, 1 | ||
end | ||
else | ||
sidekiq_redis_pool_user.with do |redis| | ||
redis.hincrby worker_metric_name, klass_name, 1 | ||
end | ||
end | ||
end | ||
|
||
def trace_workers_decrement_counter(klass_name) | ||
return unless enabled? | ||
|
||
if redis_config? | ||
redis_pool_user.with do |redis| | ||
redis.call 'HINCRBY', worker_metric_name, klass_name, -1 | ||
end | ||
else | ||
Sidekiq.redis do |redis| | ||
redis.hincrby worker_metric_name, klass_name, -1 | ||
end | ||
end | ||
end | ||
|
||
def reset_counters | ||
return unless enabled? | ||
|
||
if redis_config? | ||
redis_pool_user.with do |redis| | ||
all_keys = redis.call 'HGETALL', worker_metric_name | ||
redis.call 'HDEL', worker_metric_name, all_keys.keys | ||
end | ||
else | ||
Sidekiq.redis do |redis| | ||
all_keys = redis.hgetall worker_metric_name | ||
redis.hdel worker_metric_name, all_keys.keys | ||
end | ||
end | ||
end | ||
|
||
def reset_counter(key) | ||
return unless enabled? | ||
|
||
if redis_config? | ||
redis_pool_user.with do |redis| | ||
redis.call 'HDEL', worker_metric_name, key | ||
end | ||
else | ||
Sidekiq.redis do |redis| | ||
redis.hdel worker_metric_name, key | ||
end | ||
end | ||
end | ||
|
||
def workers_in_queue | ||
return unless enabled? | ||
|
||
if redis_config? | ||
redis_pool_user.with do |redis| | ||
redis.call 'HGETALL', worker_metric_name | ||
end | ||
else | ||
Sidekiq.redis do |redis| | ||
redis.hgetall worker_metric_name | ||
end | ||
end | ||
end | ||
|
||
def worker_metric_name | ||
"sidekiq_instrument_trace_workers:#{namespace}:in_queue" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.