Sidekiq::InfluxDB
Sidekiq middleware that writes job lifecycle events as points to an InfluxDB database.
Installation
Add this gem to your application's Gemfile
:
bundle add sidekiq-influxdb
Usage
Add included middleware to your application's Sidekiq middleware stack:
# config/initializers/sidekiq.rb
require "sidekiq/middleware/server/influxdb"
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add Sidekiq::Middleware::Server::InfluxDB,
influxdb_client: InfluxDB::Client.new(), # REQUIRED
series_name: 'sidekiq_jobs', # optional, default shown
retention_policy: nil, # optional, default nil
start_events: true, # optional, default true
tags: { application: 'MyApp' }, # optional, default {}
except: [UnimportantJob1, UnimportantJob2] # optional, default []
end
end
You can learn how to create a client in InfluxDB client documentation.
Warning: This middleware is going to write a lot of metrics. Set up your InfluxDB client accordingly:
- either set
async: true
in the client's options to use its built-in batching feature, - or install Telegraf, set up aggregation inside it, and set up InfluxDB client to send metrics to it,
- or both.
When you deploy this code, you will start getting the following series in your InfluxDB database:
> select * from sidekiq_jobs
name: sidekiq_jobs
time application class creation_time error event jid queue total waited worked
---- ----------- ----- ------------- ----- ----- --- ----- ----- ------ ------
1511707465061000000 MyApp FooJob 1511707459.0186539 start 51cc82fe75fbeba37b1ff18f default 6.042410135269165
1511707465061000000 MyApp FooJob 1511707459.0186539 finish 51cc82fe75fbeba37b1ff18f default 8.046684265136719 6.042410135269165 2.0042741298675537
1511707467068000000 MyApp BarJob 1511707461.019835 start 3891f241ab84d3aba728822e default 6.049134016036987
1511707467068000000 MyApp BarJob 1511707461.019835 NoMethodError error 3891f241ab84d3aba728822e default 8.056788206100464 6.049134016036987 2.0076541900634766
Tags (repetitive indexed data; for filtering and grouping by):
time
— standard InfluxDB timestamp. Precision of the supplied client is respected.queue
— queue name.class
— job class name. Classes fromexcept:
keyword argument are skipped (no data is sent to InfluxDB).event
— what happened to the job at the specifiedtime
:start
,finish
, orerror
. If you initialize the middleware withstart_events: false
, there will be nostart
events.error
— ifevent=error
, this tag contains the exception class name.- Your own tags from the initializer.
Values (unique non-indexed data; for aggregation):
jid
— unique job ID.creation_time
— job creation time.
Values calculated by this gem (in seconds):
waited
— how long the job waited in thequeue
until Sidekiq got around to starting it.worked
— how long it took to perform the job from start to finish or to an exception.total
— how much time passed from job creation to finish. How long it took to do the job, in total.
This schema allows querying various job metrics effectively.
For example, how many reports have been generated in the last day:
SELECT COUNT(jid) FROM sidekiq_jobs WHERE class = 'ReportGeneration' AND time > now() - 1d
How many different jobs were executed with errors in the last day:
SELECT COUNT(jid) FROM sidekiq_jobs WHERE event = 'error' AND time > now() - 1d GROUP BY class
Et cetera.
Stats and Queues metrics
To collect metrics for task stats and queues, you need to run the following code periodically. For example, you can use the gem clockwork
for that. You can add settings like this to clock.rb
:
require "sidekiq/metrics/stats"
require "sidekiq/metrics/queues"
every(1.minute, 'sidekiq_metrics') do
Sidekiq::Metrics::Stats.new(influxdb_client: InfluxDB::Client.new()).publish
Sidekiq::Metrics::Queues.new(influxdb_client: InfluxDB::Client.new()).publish
end
For stats metrics:
require "sidekiq/metrics/stats"
Sidekiq::Metrics::Stats.new(
influxdb_client: InfluxDB::Client.new(), # REQUIRED
series_name: 'sidekiq_stats', # optional, default shown
retention_policy: nil, # optional, default nil
tags: {}, # optional, default {}
).publish
For queues metrics:
require "sidekiq/metrics/queues"
Sidekiq::Metrics::Queues.new(
influxdb_client: InfluxDB::Client.new(), # REQUIRED
series_name: 'sidekiq_queues', # optional, default shown
retention_policy: nil, # optional, default nil
tags: {}, # optional, default {}
).publish
When you run the scripts, you will get the following series in your InfluxDB database:
> select * from sidekiq_stats
name: sidekiq_stats
time size stat
---- ---- ----
1582502419000000000 9999 dead
1582502419000000000 0 workers
1582502419000000000 0 enqueued
1582502419000000000 23020182 processed
> select * from sidekiq_queues
name: sidekiq_queues
time queue size
---- ----- ----
1582502418000000000 default 0
1582502418000000000 queue_name_1 0
Visualization
Grafana
You can import the ready-made dashboard from grafana_dashboard.json.
Development
After checking out the repo, run bin/setup
to install dependencies.
You can also run bin/console
for an interactive prompt that will allow you to experiment.
Contributing
Bug reports and pull requests are welcome on GitHub at github.com/funbox/sidekiq-influxdb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Code of Conduct
Everyone interacting in the Sidekiq::InfluxDB
project’s codebases, issue trackers,
chat rooms and mailing lists is expected to follow the code of conduct.