Skip to content

Commit

Permalink
add rubocop as development dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
hkim3163 committed Jun 23, 2023
1 parent 2d04457 commit f551002
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/sidekiq/instrument/middleware/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Sidekiq::Instrument
class ServerMiddleware
include Sidekiq::Instrument::MetricNames

def call(worker, job, queue, &block)
def call(worker, _job, _queue, &block)
Statter.statsd.increment(metric_name(worker, 'dequeue'))
Statter.dogstatsd&.increment('sidekiq.dequeue', worker_dog_options(worker))

Expand Down
6 changes: 4 additions & 2 deletions lib/sidekiq/instrument/worker.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'sidekiq'
require 'sidekiq/api'

Expand All @@ -13,7 +15,7 @@ class Worker
workers_size: :workers,
enqueued: :pending,
failed: nil
}
}.freeze

def perform
info = Sidekiq::Stats.new
Expand Down Expand Up @@ -65,7 +67,7 @@ def send_worker_metrics

WorkerMetrics.workers_in_queue.each do |key, value|
Statter.statsd.gauge("shared.sidekiq.worker_metrics.in_queue.#{key}", value)
Statter.dogstatsd&.gauge("shared.sidekiq.worker_metrics.in_queue", value, tags: ["worker:#{key}"])
Statter.dogstatsd&.gauge('shared.sidekiq.worker_metrics.in_queue', value, tags: ["worker:#{key}"])
end
end
end
Expand Down
1 change: 1 addition & 0 deletions sidekiq-instrument.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 2.0', '>= 2.0.2'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 1.0'
spec.add_development_dependency 'pry-byebug', '~> 3.4'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'simplecov-cobertura'
Expand Down
26 changes: 17 additions & 9 deletions spec/sidekiq-instrument/client_middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'sidekiq/instrument/middleware/client'

RSpec.describe Sidekiq::Instrument::ClientMiddleware do
Expand Down Expand Up @@ -28,32 +30,34 @@

context 'without statsd_metric_name' do
it 'increments the StatsD enqueue counter' do
expect {
expect do
MyWorker.perform_async
}.to trigger_statsd_increment('shared.sidekiq.default.MyWorker.enqueue')
end.to trigger_statsd_increment('shared.sidekiq.default.MyWorker.enqueue')
end

it 'increments the DogStatsD enqueue counter' do
expect(Sidekiq::Instrument::Statter.dogstatsd).to receive(:increment).with('sidekiq.enqueue', { tags: ['queue:default', 'worker:my_worker'] }).once
expect(
Sidekiq::Instrument::Statter.dogstatsd
).to receive(:increment).with('sidekiq.enqueue', { tags: ['queue:default', 'worker:my_worker'] }).once
MyWorker.perform_async
end
end

context 'with statsd_metric_name' do
it 'increments the enqueue counter' do
expect {
expect do
MyOtherWorker.perform_async
}.to trigger_statsd_increment('my_other_worker.enqueue')
end.to trigger_statsd_increment('my_other_worker.enqueue')
end
end

context 'with WorkerMetrics.enabled true' do
it 'increments the enqueue counter' do
Sidekiq::Instrument::WorkerMetrics.enabled = true
MyOtherWorker.perform_async
expect(Redis.new.hget(worker_metric_name ,'my_other_worker')).to eq('1')
expect(Redis.new.hget(worker_metric_name, 'my_other_worker')).to eq('1')
MyOtherWorker.perform_async
expect(Redis.new.hget(worker_metric_name ,'my_other_worker')).to eq('2')
expect(Redis.new.hget(worker_metric_name, 'my_other_worker')).to eq('2')
end
end

Expand All @@ -78,8 +82,12 @@
end

it 'does not increase the redis counter' do
expect(Redis.new.hget worker_metric_name ,'my_worker').to eq(nil)
MyWorker.perform_async rescue nil
expect(Redis.new.hget(worker_metric_name, 'my_worker')).to eq(nil)
begin
MyWorker.perform_async
rescue StandardError
nil
end
end

it 're-raises the error' do
Expand Down
67 changes: 47 additions & 20 deletions spec/sidekiq-instrument/server_middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'sidekiq/instrument/middleware/server'

RSpec.describe Sidekiq::Instrument::ServerMiddleware do
Expand Down Expand Up @@ -25,22 +27,22 @@

context 'when a job succeeds' do
it 'increments StatsD dequeue counter' do
expect {
expect do
MyWorker.perform_async
}.to trigger_statsd_increment('shared.sidekiq.default.MyWorker.dequeue')
end.to trigger_statsd_increment('shared.sidekiq.default.MyWorker.dequeue')
end

it 'increments DogStatsD dequeue counter' do
expect(
Sidekiq::Instrument::Statter.dogstatsd
).to receive(:increment).with('sidekiq.dequeue', expected_dog_options).once
).to receive(:increment).with('sidekiq.dequeue', expected_dog_options).once
MyWorker.perform_async
end

it 'measures StatsD job runtime' do
expect {
expect do
MyWorker.perform_async
}.to trigger_statsd_measure('shared.sidekiq.default.MyWorker.runtime')
end.to trigger_statsd_measure('shared.sidekiq.default.MyWorker.runtime')
end

it 'measures DogStatsD job runtime' do
Expand All @@ -51,19 +53,23 @@
context 'with WorkerMetrics.enabled true' do
it 'increments the in queue counter' do
Sidekiq::Instrument::WorkerMetrics.enabled = true
Redis.new.hdel worker_metric_name ,'my_other_worker'
Redis.new.hdel worker_metric_name, 'my_other_worker'
MyOtherWorker.perform_async
expect(Redis.new.hget worker_metric_name ,'my_other_worker').to eq('-1')
expect(Redis.new.hget(worker_metric_name, 'my_other_worker')).to eq('-1')
end
end

context 'with WorkerMetrics.enabled true and an errored job' do
it 'decrements the in queue counter' do
Sidekiq::Instrument::WorkerMetrics.enabled = true
MyOtherWorker.perform_async
expect(Redis.new.hget worker_metric_name ,'my_other_worker').to eq('-1')
MyOtherWorker.perform_async rescue nil
expect(Redis.new.hget worker_metric_name ,'my_other_worker').to eq('-2')
expect(Redis.new.hget(worker_metric_name, 'my_other_worker')).to eq('-1')
begin
MyOtherWorker.perform_async
rescue StandardError
nil
end
expect(Redis.new.hget(worker_metric_name, 'my_other_worker')).to eq('-2')
end
end
end
Expand All @@ -74,30 +80,51 @@
end

it 'increments the StatsD failure counter' do
expect {
MyWorker.perform_async rescue nil
}.to trigger_statsd_increment('shared.sidekiq.default.MyWorker.error')
expect do
MyWorker.perform_async
rescue StandardError
nil
end.to trigger_statsd_increment('shared.sidekiq.default.MyWorker.error')
end

it 'increments the DogStatsD failure counter' do
expect(Sidekiq::Instrument::Statter.dogstatsd).to receive(:increment).with('sidekiq.dequeue', expected_dog_options).once
expect(
Sidekiq::Instrument::Statter.dogstatsd
).to receive(:increment).with('sidekiq.dequeue', expected_dog_options).once
expect(Sidekiq::Instrument::Statter.dogstatsd).not_to receive(:time)
expect(Sidekiq::Instrument::Statter.dogstatsd).to receive(:increment).with('sidekiq.error', expected_dog_options).once
MyWorker.perform_async rescue nil
expect(
Sidekiq::Instrument::Statter.dogstatsd
).to receive(:increment).with('sidekiq.error', expected_dog_options).once

begin
MyWorker.perform_async
rescue StandardError
nil
end
end

it 'does not increase the redis counter' do
expect(Redis.new.hget worker_metric_name ,'my_worker').to eq(nil)
MyWorker.perform_async rescue nil
expect(Redis.new.hget(worker_metric_name, 'my_worker')).to eq(nil)
begin
MyWorker.perform_async
rescue StandardError
nil
end
end

it 're-raises the error' do
expect { MyWorker.perform_async }.to raise_error 'foo'
end

it 'calls the decrement counter' do
expect(Sidekiq::Instrument::WorkerMetrics).to receive(:trace_workers_decrement_counter).with('my_worker').once
MyWorker.perform_async rescue nil
expect(
Sidekiq::Instrument::WorkerMetrics
).to receive(:trace_workers_decrement_counter).with('my_worker').once
begin
MyWorker.perform_async
rescue StandardError
nil
end
end
end

Expand Down
48 changes: 32 additions & 16 deletions spec/sidekiq-instrument/worker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'sidekiq/instrument/worker'

RSpec.describe Sidekiq::Instrument::Worker do
Expand All @@ -9,7 +11,7 @@
let(:worker) { described_class.new }

before do
Redis.new.hdel worker_metric_name ,'my_worker'
Redis.new.hdel worker_metric_name, 'my_worker'
end

shared_examples 'worker behavior' do |expected_stats|
Expand Down Expand Up @@ -147,7 +149,7 @@
chain.add Sidekiq::Instrument::ServerMiddleware
end
end

after do
Sidekiq.server_middleware do |chain|
chain.remove Sidekiq::Instrument::ServerMiddleware
Expand All @@ -157,22 +159,32 @@
it 'increments the in queue counter' do
Sidekiq::Instrument::WorkerMetrics.enabled = true
redis = Redis.new
expect(redis.hget(worker_metric_name ,'my_worker')).to be nil
expect(redis.hget(worker_metric_name, 'my_worker')).to be nil
MyWorker.perform_async
expect(redis.hget(worker_metric_name ,'my_worker')).to eq('1')
expect(redis.hget(worker_metric_name, 'my_worker')).to eq('1')
MyWorker.perform_async
expect(redis.hget(worker_metric_name ,'my_worker')).to eq('2')
expect(redis.hget(worker_metric_name, 'my_worker')).to eq('2')
end

it 'increments the DogStatsD failure counter' do
expect(Sidekiq::Instrument::Statter.dogstatsd).to receive(:increment).with('sidekiq.enqueue', expected_dog_options).once
expect(
Sidekiq::Instrument::Statter.dogstatsd
).to receive(:increment).with('sidekiq.enqueue', expected_dog_options).once
expect(Sidekiq::Instrument::Statter.dogstatsd).not_to receive(:time)
MyWorker.perform_async rescue nil
begin
MyWorker.perform_async
rescue StandardError
nil
end
end

it 'does not increase the redis counter' do
expect(Redis.new.hget worker_metric_name ,'my_worker').to eq(nil)
MyWorker.perform_async rescue nil
expect(Redis.new.hget(worker_metric_name, 'my_worker')).to eq(nil)
begin
MyWorker.perform_async
rescue StandardError
nil
end
end
end

Expand All @@ -183,7 +195,7 @@
chain.add Sidekiq::Instrument::ServerMiddleware
end
end

after do
Sidekiq::Testing.server_middleware do |chain|
chain.remove Sidekiq::Instrument::ServerMiddleware
Expand All @@ -193,21 +205,25 @@
it 'does not increment the in queue counter' do
Sidekiq::Instrument::WorkerMetrics.enabled = true
redis = Redis.new
expect(redis.hget(worker_metric_name ,'my_worker')).to be nil
expect(redis.hget(worker_metric_name, 'my_worker')).to be nil
MyWorker.perform_async
expect(redis.hget(worker_metric_name ,'my_worker')).to eq('0')
expect(redis.hget(worker_metric_name, 'my_worker')).to eq('0')
MyWorker.perform_async
expect(redis.hget(worker_metric_name ,'my_worker')).to eq('0')
expect(redis.hget(worker_metric_name, 'my_worker')).to eq('0')
redis.hincrby(worker_metric_name, 'my_worker', 1)
MyWorker.perform_async
expect(redis.hget(worker_metric_name ,'my_worker')).to eq('1')
expect(redis.hget(worker_metric_name, 'my_worker')).to eq('1')
end

it 'calls the decrement counter' do
expect(
Sidekiq::Instrument::WorkerMetrics
).to receive(:trace_workers_decrement_counter).with('my_worker').once
MyWorker.perform_async rescue nil
).to receive(:trace_workers_decrement_counter).with('my_worker').once
begin
MyWorker.perform_async
rescue StandardError
nil
end
end
end
end
Expand Down

0 comments on commit f551002

Please sign in to comment.