docbrown/app/services/loggers/log_worker_queue_stats.rb
Kirk Haines ed74f2f245
Abstract DatadogStatsClient to ForemStatsClient (#12304)
* This change abstracts the DatadogStatsClient into a ForemStatsClient.
The purpose of this abstraction is to set the foundation for a subsequent PR that will allow one to use New Relic for recording Forem stats, instead of Datadog, if there is a New Relic configuration found.
This specific change creates an abstraction layer that can be built upon, without changing any actual default behavior. All specs still pass.

* Use delegate instead of explicit methods.

* Delegate instead of explicit methods.

* Fix the error.

* Refactor according to the suggestions in the comments.

* Ooops.  Stats work better when all of the code is committed.

* Removing the alias of count to increment since that was done in error.
2021-01-27 11:25:44 -05:00

34 lines
1.1 KiB
Ruby

module Loggers
class LogWorkerQueueStats
class << self
def call
queues = Sidekiq::Queue.all.map(&:itself)
record_totals(queues)
record_queue_stats(queues)
end
private
def record_totals(queues)
log_to_datadog("sidekiq.queues.total_size", queues.sum(&:size))
log_to_datadog("sidekiq.queues.total_workers", Sidekiq::Workers.new.size)
end
def record_queue_stats(queues)
queue_hash = queues.map do |queue|
[queue.name, { size: queue.size, latency: queue.latency }]
end.to_h
queue_hash.each do |queue_name, queue_values|
latency = queue_values.fetch(:latency, 0)
size = queue_values.fetch(:size, 0)
log_to_datadog("sidekiq.queues.latency", latency, ["sidekiq_queue:#{queue_name}"])
log_to_datadog("sidekiq.queues.size", size, ["sidekiq_queue:#{queue_name}"])
end
end
def log_to_datadog(metric_name, value, tags = [])
ForemStatsClient.gauge(metric_name, value, tags: tags)
end
end
end
end