diff --git a/lib/sidekiq/instrument/middleware/client.rb b/lib/sidekiq/instrument/middleware/client.rb index 2338aa0..6d9d8db 100644 --- a/lib/sidekiq/instrument/middleware/client.rb +++ b/lib/sidekiq/instrument/middleware/client.rb @@ -20,7 +20,7 @@ def call(worker_class, job, queue, _redis_pool) if Sidekiq::Context.current[:class].present? WorkerMetrics.trace_workers_increment_counter(klass.name.underscore) Statter.statsd.increment(metric_name(class_instance, 'enqueue')) - Statter.dogstatsd&.increment('sidekiq.enqueue', worker_dog_options(class_instance)) + Statter.dogstatsd&.increment('sidekiq.enqueue', worker_dog_options(class_instance, job)) end Statter.dogstatsd&.flush(sync: true) diff --git a/lib/sidekiq/instrument/middleware/server.rb b/lib/sidekiq/instrument/middleware/server.rb index 7e570dc..bb0134f 100644 --- a/lib/sidekiq/instrument/middleware/server.rb +++ b/lib/sidekiq/instrument/middleware/server.rb @@ -9,16 +9,16 @@ class ServerMiddleware def call(worker, job, _queue, &block) dequeue_string = is_retry(job) ? 'dequeue.retry' : 'dequeue' - Statter.dogstatsd&.increment("sidekiq.#{dequeue_string}", worker_dog_options(worker)) + Statter.dogstatsd&.increment("sidekiq.#{dequeue_string}", worker_dog_options(worker, job)) Statter.statsd.increment(metric_name(worker, dequeue_string)) start_time = Time.now yield block execution_time_ms = (Time.now - start_time) * 1000 - Statter.dogstatsd&.timing('sidekiq.runtime', execution_time_ms, worker_dog_options(worker)) + Statter.dogstatsd&.timing('sidekiq.runtime', execution_time_ms, worker_dog_options(worker, job)) Statter.statsd.measure(metric_name(worker, 'runtime'), execution_time_ms) rescue Exception => e - dd_options = worker_dog_options(worker) + dd_options = worker_dog_options(worker, job) dd_options[:tags] << "error:#{e.class.name}" # if we have retries left, increment the enqueue.retry counter to indicate the job is going back on the queue diff --git a/lib/sidekiq/instrument/mixin.rb b/lib/sidekiq/instrument/mixin.rb index 40d0518..eaa1b4c 100644 --- a/lib/sidekiq/instrument/mixin.rb +++ b/lib/sidekiq/instrument/mixin.rb @@ -8,8 +8,14 @@ def metric_name(worker, event) end end - def worker_dog_options(worker) - { tags: ["queue:#{queue_name(worker)}", "worker:#{underscore(class_name(worker))}"] } + def worker_dog_options(worker, job) + tags = job.dig('tags') || [] + { + tags: [ + "queue:#{queue_name(worker)}", + "worker:#{underscore(class_name(worker))}" + ].concat(tags) + } end def max_retries(worker) diff --git a/lib/sidekiq/instrument/version.rb b/lib/sidekiq/instrument/version.rb index 68af9f6..ed564bc 100644 --- a/lib/sidekiq/instrument/version.rb +++ b/lib/sidekiq/instrument/version.rb @@ -1,5 +1,5 @@ module Sidekiq module Instrument - VERSION = '0.7.2' + VERSION = '0.7.3' end end diff --git a/spec/sidekiq-instrument/client_middleware_spec.rb b/spec/sidekiq-instrument/client_middleware_spec.rb index 4951028..8c2b0b3 100644 --- a/spec/sidekiq-instrument/client_middleware_spec.rb +++ b/spec/sidekiq-instrument/client_middleware_spec.rb @@ -46,6 +46,17 @@ ).to receive(:increment).with('sidekiq.enqueue', { tags: ['queue:default', 'worker:my_worker'] }).once MyWorker.perform_async end + + context 'with additional tag(s)' do + it 'increments DogStatsD enqueue counter with additional tag(s)' do + tag = 'test_worker' + + expect( + Sidekiq::Instrument::Statter.dogstatsd + ).to receive(:increment).with('sidekiq.enqueue', { tags: ['queue:default', 'worker:my_worker', tag] }).once + MyWorker.set(tags: [tag]).perform_async + end + end end context 'with statsd_metric_name' do diff --git a/spec/sidekiq-instrument/server_middleware_spec.rb b/spec/sidekiq-instrument/server_middleware_spec.rb index e296127..7eedbf0 100644 --- a/spec/sidekiq-instrument/server_middleware_spec.rb +++ b/spec/sidekiq-instrument/server_middleware_spec.rb @@ -56,6 +56,18 @@ MyWorker.perform_async end + context 'with additional tag(s)' do + let(:tag) { 'test_worker' } + let(:expected_dog_options) { { tags: ['queue:default', 'worker:my_worker', tag] } } + + it 'increments DogStatsD dequeue counter with additional tag(s)' do + expect( + Sidekiq::Instrument::Statter.dogstatsd + ).to receive(:increment).with('sidekiq.dequeue', expected_dog_options).once + MyWorker.set(tags: [tag]).perform_async + end + end + # TODO: These tests are meaningless until we fix the WorkerMetrics class # # context 'with WorkerMetrics.enabled true' do @@ -140,6 +152,28 @@ end end + context 'with additional tag(s)' do + let(:tag) { 'test_worker' } + let(:expected_dog_options) { { tags: ['queue:default', 'worker:my_worker', tag] } } + let(:expected_error_dog_options) { { tags: ['queue:default', 'worker:my_worker', tag, 'error:RuntimeError'] } } + + it 'increments DogStatsD dequeue counter with additional tag(s)' do + 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_error_dog_options).once + + begin + MyWorker.set(tags: [tag]).perform_async + rescue StandardError + nil + end + end + end + context 'when the worker has retries disabled' do shared_examples 'it does not attempt to track retries' do |retry_value| before do