Giter Club home page Giter Club logo

sidekiq-influxdb's Introduction

Sidekiq::InfluxDB

Gem Version Travis CI Coveralls

Sidekiq server middleware that writes job lifecycle events as points to an InfluxDB database. Also includes classes that write global Sidekiq metrics and queue metrics.

Installation

Add this gem to your application's Gemfile:

bundle add sidekiq-influxdb

Usage

Add included middleware to your application's Sidekiq middleware stack. The following examples assume that you already have an InfluxDB client object in the influxdb variable. This will create a middleware with all defaults (suitable for most deployments):

# 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
  end
end

You can customize the middleware by passing more options:

# 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,
                series_name: 'sidekiq_jobs',  # This is the default one.
                retention_policy: 'rp_name',  # In case you want to write metrics to a non-default RP.
                start_events: true,           # Whether or not you want to know when jobs started. See `event` tag description below.
                tags: {application: 'MyApp'}, # Anything you need on top. **Make sure that tag values have low cardinality!**
                except: [UnimportantJob]      # These job classes will be executed without sending any metrics.
  end
end

This library assumes that you already have an InfluxDB client object set up the way you like. It does not try to create one for you. If that is not the case, 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 have 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 from except: keyword argument are skipped (no data is sent to InfluxDB).
  • event — what happened to the job at the specified time: start, finish, or error. If you initialize the middleware with start_events: false, there will be no start events.
  • error — if event=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 the queue 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 Clockwork for that. You can add settings like this to clock.rb:

require "sidekiq/metrics/stats"
require "sidekiq/metrics/queues"

influx = InfluxDB::Client.new(options)

sidekiq_global_metrics = Sidekiq::Metrics::Stats.new(influxdb_client: influx)
sidekiq_queues_metrics = Sidekiq::Metrics::Queues.new(influxdb_client: influx)

every(1.minute, 'sidekiq_metrics') do
  sidekiq_global_metrics.publish
  sidekiq_queues_metrics.publish
end

For stats metrics:

require "sidekiq/metrics/stats"

Sidekiq::Metrics::Stats.new(
  influxdb_client: InfluxDB::Client.new(options), # 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(options), # REQUIRED
  series_name: 'sidekiq_queues',                  # optional, default shown
  retention_policy: nil,                          # optional, default nil
  tags: {},                                       # optional, default {}
).publish

When you run the code, you will have 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 a ready-made dashboard from grafana_dashboard.json.

Development

See Contributing Guidelines.

Sponsored by FunBox

sidekiq-influxdb's People

Contributors

igoradamenko avatar stonegod avatar tuwilof avatar vassilevsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

katafrakt

sidekiq-influxdb's Issues

Idea: exclude some workers

There's one more feature idea I would like to discuss. Currently in our organisation we send metrics to InfluxDB synchronously, but I'm thinking about moving it to separate Sidekiq workers, so that Influx failure won't fails whole request or another Sidekiq job and so it can be retries later. In this case I would obviously (or maybe not obviously?) like to exclude those Influx workers from metrics reporting.

How do you feel about adding a config option to exclude some workers (or maybe queues) from sending metrics to Influx?

support ruby 3.0.1

We updated ruby and pick up bugs

wrong number of arguments (given 1, expected 0; required keyword: influxdb_client)

it broke our stagings because all the tasks hit

we had to disable the library in our project

Setting a field in config

Hi! Thank you for this nice gem. I have one enhancement proposal.

My use case is this:

We have a couple of microservices using Sidekiq. I want to track their performance in one series (say sidekiq_jobs, but I want to be able to indicate from which service it comes, so I can narrow down the metrics. Would it be possible to add something like this on config level?

For example:

chain.add Sidekiq::InfluxDB::ServerMiddleware,
     influxdb_client: InfluxDB::Client.new(options),
     series_name: 'sidekiq_jobs',
     additional_fields: { app: 'microservice_1' }

If you agree this is a good thing to add, I can try to work on it myself.

Missing data in Grafana demo

Hi!

Thanks for the great middleware. It's gonna be really useful.

I took the demo JSON and put up a dashboard on Grafana. I changed the datasource to my own.
Sadly the top 4 panels don't have any data. They refer to a series sidekiq_stats and other metrics I don't see anywhere in the code.

Here's my config:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add(Sidekiq::Middleware::Server::InfluxDB, influxdb_client: InfluxDB::Rails.client,
                                                     series_name: "sidekiq_jobs",
                                                     tags: {
                                                       application: InfluxDB::Rails.configuration.application_name,
                                                       env:         Rails.env,
                                                     })
  end
end

Am i missing a configuration?

Thanks,
Mathieu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.