Skip to content

Pro Metrics

Shahid Khaliq edited this page Sep 1, 2023 · 25 revisions

Sidekiq Pro can send runtime metrics to Statsd for distribution. Set up a global metrics handler and add the middleware to track job execution.

Enabling Metrics

# Add gem 'dogstatsd-ruby' to your Gemfile
require 'datadog/statsd'

# Be careful here.  I've found that when using UDP, Statsd can perform a DNS lookup
# on this hostname for every single packet. This can lead to mysterious but severe
# performance issues.
#
# You might consider installing a local caching DNS resolver instead (e.g. dnsmasq)
# or performing the DNS resolution once, here in the initializer.
Sidekiq.configure_server do |config|
  config.dogstatsd = ->{ Datadog::Statsd.new("metrics.example.com", 8125) }
  config.server_middleware do |chain|
    require 'sidekiq/middleware/server/statsd'
    chain.add Sidekiq::Middleware::Server::Statsd
  end
end

Importing Statsd

Other metrics systems will have ingesters for importing Statsd metric data into their native format:

Metrics

Sidekiq Pro will send the following metrics for each job:

jobs.count => counter
jobs.success => counter
jobs.failure => counter
jobs.perform => gauge (time)

i.e. count will always be incremented. success or failure will be incremented based on the outcome. perform tracks the amount of time (in milliseconds) spent in the worker. Your Statsd system may provide additional metrics based on these explicit ones..

Tags

Sidekiq Pro sends several tags with each metric. Some Statsd platforms will accept these tags and allow you to visualize/report the perform metrics based on these additional dimensions:

tags: ["worker:VideoEncodeWorker", "queue:bulk"] 

Dynamic Options

If you want to set per-job options for the metrics, you can customize the options used within the middleware:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    require 'sidekiq/middleware/server/statsd'
    chain.add Sidekiq::Middleware::Server::Statsd
  end

  Sidekiq::Middleware::Server::Statsd.options = ->(w, j, q) do
    {
      tags: ["worker:#{w}", "queue:#{q}"],
      sample_rate: (j['dd_rate'] || 1.0)
    }
  end
end

Note how we are using an attribute within the job payload to control the sample_rate option, like so:

class Myapp::HighVolumeJob
  include Sidekiq::Job
  sidekiq_options dd_rate: 0.1

Slick!

Commercial Metrics

Several other metrics are sent for various Pro and Enterprise features:

jobs.expired - when a job is expired
jobs.recovered.push - when a job is recovered by reliable_push after network outage
jobs.recovered.fetch - when a job is recovered by super_fetch after process crash
jobs.poison - when a poison pill job is detected and killed by super_fetch
batch.created - when a batch is created
batch.complete - when a batch is completed
batch.success - when a batch is successful

statsd-ruby

As of Sidekiq Pro 6.0, the dogstatsd-ruby gem is the only supported Statsd client library. statsd-ruby support was removed as it was not getting nearly as much maintenance and it does not support features like tagging.