Skip to content

Commit

Permalink
add optional tags to datadog metrics (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: orioldsm <[email protected]>
Co-authored-by: Felipe Andrade <[email protected]>
  • Loading branch information
orioldsm and kaisensan authored Sep 23, 2024
1 parent b76e5b4 commit ecb0426
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/sidekiq/instrument/middleware/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lib/sidekiq/instrument/middleware/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/sidekiq/instrument/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ 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: [
"queue:#{queue_name(worker)}",
"worker:#{underscore(class_name(worker))}"
].concat(job.fetch('tags', []))
}
end

def max_retries(worker)
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/instrument/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Sidekiq
module Instrument
VERSION = '0.7.2'
VERSION = '0.7.3'
end
end
11 changes: 11 additions & 0 deletions spec/sidekiq-instrument/client_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions spec/sidekiq-instrument/server_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ecb0426

Please sign in to comment.