sidekiq-instrument 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.simplecov +8 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sidekiq/instrument/middleware/client.rb +14 -0
- data/lib/sidekiq/instrument/middleware/server.rb +16 -0
- data/lib/sidekiq/instrument/mixin.rb +14 -0
- data/lib/sidekiq/instrument/version.rb +5 -0
- data/lib/sidekiq/instrument/worker.rb +32 -0
- data/lib/sidekiq/instrument.rb +7 -0
- data/sidekiq-instrument.gemspec +27 -0
- metadata +165 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b91f56b76bb74cae296dabe8f002fd50a22eed0e
|
4
|
+
data.tar.gz: 648a7a5fd46007137612630b37782574e38441ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42284f2bbb7ed52d15dd7271ef428cce8e8b891dffb6c4ed8698f9f1f8c1757260aea3fd4edcd61b0a0f2b67ce8c7ada6d3583cb15f54de710d0d714da760ab5
|
7
|
+
data.tar.gz: 9f5a83e2f9f77812345aec2f60dceb2ca8421615a34c1354edf89cb6db0e9a592e9e5cf7942f2f7465a58b39a00f11eeb28ce3410e62c0440339c3ce5e9fd3e2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.simplecov
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Matt Larraz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Sidekiq::Instrument
|
2
|
+
[![Build Status](https://travis-ci.org/enova/sidekiq-instrument.svg?branch=master)](https://travis-ci.org/enova/sidekiq-instrument)
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/github/enova/sidekiq-instrument/badge.svg?branch=master)](https://coveralls.io/github/enova/sidekiq-instrument?branch=master)
|
4
|
+
|
5
|
+
Reports job metrics using Shopify's [statsd-instrument][statsd-instrument] library, incrementing a counter for each enqueue and dequeue per job type, and timing the full runtime of your perform method.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sidekiq-instrument'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sidekiq-instrument
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
For now, this library assumes you have already initialized `StatsD` on your own;
|
26
|
+
the `statsd-instrument` gem may have chosen reasonable defaults for you already. If not,
|
27
|
+
a typical Rails app would just use an initializer:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# config/initializers/statsd.rb
|
31
|
+
require 'statsd-instrument'
|
32
|
+
StatsD.prefix = 'my-app'
|
33
|
+
StatsD.backend = StatsD::Instrument::Backends::UDPBackend.new('some-server:8125')
|
34
|
+
```
|
35
|
+
|
36
|
+
Then add the client and server middlewares in your Sidekiq initializer:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'sidekiq/instrument'
|
40
|
+
|
41
|
+
Sidekiq.configure_server do |config|
|
42
|
+
config.server_middleware do |chain|
|
43
|
+
chain.add Sidekiq::Instrument::ServerMiddleware
|
44
|
+
end
|
45
|
+
|
46
|
+
config.client_middleware do |chain|
|
47
|
+
chain.add Sidekiq::Instrument::ClientMiddleware
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
Sidekiq.configure_client do |config|
|
52
|
+
config.client_middleware do |chain|
|
53
|
+
chain.add Sidekiq::Instrument::ClientMiddleware
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
## StatsD Keys
|
59
|
+
For each job, the following metrics will be reported:
|
60
|
+
|
61
|
+
1. **shared.sidekiq._queue_._job_.enqueue**: counter incremented each time a
|
62
|
+
job is pushed onto the queue.
|
63
|
+
2. **shared.sidekiq._queue_._job_.dequeue**: counter incremented just before
|
64
|
+
worker begins performing a job.
|
65
|
+
3. **shared.sidekiq._queue_._job_.runtime**: timer of the total time spent
|
66
|
+
in `perform`, in milliseconds.
|
67
|
+
3. **shared.sidekiq._queue_._job_.error**: counter incremented each time a
|
68
|
+
job fails.
|
69
|
+
|
70
|
+
The metric names can be changed by overriding the `statsd_metric_name`
|
71
|
+
method in your worker classes.
|
72
|
+
|
73
|
+
## Worker
|
74
|
+
There is a worker, `Sidekiq::Instrument::Worker`, that submits gauges
|
75
|
+
for various interesting statistics; namely, the bulk of the information in `Sidekiq::Stats`
|
76
|
+
and the sizes of each individual queue. While the worker class is a fully valid Sidekiq worker,
|
77
|
+
you should inherit from it your own job implementation instead of using it directly:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
# app/jobs/sidekiq_stats_job.rb
|
81
|
+
class SidekiqStatsJob < Sidekiq::Instrument::Worker
|
82
|
+
METRIC_NAMES = %w[
|
83
|
+
processed
|
84
|
+
failed
|
85
|
+
]
|
86
|
+
|
87
|
+
sidekiq_options queue: :stats
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
In this example, we override the default stats with the ones we want reported by defining `METRIC_NAMES`.
|
92
|
+
This can be either an Array or a Hash (if you also want to map a stat to a different metric name).
|
93
|
+
|
94
|
+
You can schedule this however you see fit. A simple way is to use [sidekiq-scheduler][sidekiq-scheduler] to run it every N minutes.
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/enova/sidekiq-instrument.
|
105
|
+
|
106
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
110
|
+
|
111
|
+
[statsd-instrument]: https://github.com/Shopify/statsd-instrument
|
112
|
+
[sidekiq-scheduler]: https://github.com/moove-it/sidekiq-scheduler
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sidekiq/instrument"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'sidekiq/instrument/mixin'
|
2
|
+
|
3
|
+
module Sidekiq::Instrument
|
4
|
+
class ClientMiddleware
|
5
|
+
include Sidekiq::Instrument::MetricNames
|
6
|
+
|
7
|
+
def call(worker_class, job, queue, redis_pool)
|
8
|
+
klass = Object.const_get(worker_class)
|
9
|
+
StatsD.increment metric_name(klass.new, 'enqueue')
|
10
|
+
|
11
|
+
yield
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'sidekiq/instrument/mixin'
|
2
|
+
|
3
|
+
module Sidekiq::Instrument
|
4
|
+
class ServerMiddleware
|
5
|
+
include Sidekiq::Instrument::MetricNames
|
6
|
+
|
7
|
+
def call(worker, job, queue, &block)
|
8
|
+
StatsD.increment(metric_name(worker, 'dequeue'))
|
9
|
+
|
10
|
+
StatsD.measure(metric_name(worker,'runtime'), &block)
|
11
|
+
rescue StandardError => e
|
12
|
+
StatsD.increment(metric_name(worker, 'error'))
|
13
|
+
raise e
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Sidekiq::Instrument
|
2
|
+
module MetricNames
|
3
|
+
def metric_name(worker, event)
|
4
|
+
if worker.respond_to?(:statsd_metric_name)
|
5
|
+
worker.send(:statsd_metric_name, event)
|
6
|
+
else
|
7
|
+
queue = worker.sidekiq_options_hash['queue']
|
8
|
+
name = worker.class.name
|
9
|
+
|
10
|
+
"shared.sidekiq.#{queue}.#{name}.#{event}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'sidekiq'
|
2
|
+
require 'sidekiq/api'
|
3
|
+
|
4
|
+
module Sidekiq::Instrument
|
5
|
+
class Worker
|
6
|
+
include Sidekiq::Worker
|
7
|
+
|
8
|
+
# These defaults are for compatibility with Resque's stats names
|
9
|
+
# (i.e. the metrics will reported as :processed, :workers, :pending, and :failed).
|
10
|
+
# Feel free to override.
|
11
|
+
METRIC_NAMES = {
|
12
|
+
processed: nil,
|
13
|
+
workers_size: :workers,
|
14
|
+
enqueued: :pending,
|
15
|
+
failed: nil
|
16
|
+
}
|
17
|
+
|
18
|
+
def perform
|
19
|
+
info = Sidekiq::Stats.new
|
20
|
+
|
21
|
+
self.class::METRIC_NAMES.each do |method, stat|
|
22
|
+
stat ||= method
|
23
|
+
|
24
|
+
StatsD.gauge "shared.sidekiq.stats.#{stat}", info.send(method)
|
25
|
+
end
|
26
|
+
|
27
|
+
working = Sidekiq::ProcessSet.new.select { |p| p[:busy] == 1 }.count
|
28
|
+
|
29
|
+
StatsD.gauge "shared.sidekiq.stats.working", working
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sidekiq/instrument/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sidekiq-instrument'
|
8
|
+
spec.version = Sidekiq::Instrument::VERSION
|
9
|
+
spec.authors = ['Matt Larraz']
|
10
|
+
spec.email = ['mlarraz@enova.com']
|
11
|
+
|
12
|
+
spec.summary = 'StatsD instrumentation for Sidekiq'
|
13
|
+
spec.homepage = 'https://github.com/enova/sidekiq-instrument'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_dependency 'sidekiq', '~> 4.0'
|
20
|
+
spec.add_dependency 'statsd-instrument', '~> 2.0', '>= 2.0.4'
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.4'
|
26
|
+
spec.add_development_dependency 'coveralls', '~> 0.8'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq-instrument
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Larraz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sidekiq
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: statsd-instrument
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.0.4
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '2.0'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.0.4
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.12'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.12'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: pry-byebug
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '3.4'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '3.4'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: coveralls
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0.8'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0.8'
|
117
|
+
description:
|
118
|
+
email:
|
119
|
+
- mlarraz@enova.com
|
120
|
+
executables: []
|
121
|
+
extensions: []
|
122
|
+
extra_rdoc_files: []
|
123
|
+
files:
|
124
|
+
- ".gitignore"
|
125
|
+
- ".rspec"
|
126
|
+
- ".simplecov"
|
127
|
+
- ".travis.yml"
|
128
|
+
- Gemfile
|
129
|
+
- LICENSE.txt
|
130
|
+
- README.md
|
131
|
+
- Rakefile
|
132
|
+
- bin/console
|
133
|
+
- bin/setup
|
134
|
+
- lib/sidekiq/instrument.rb
|
135
|
+
- lib/sidekiq/instrument/middleware/client.rb
|
136
|
+
- lib/sidekiq/instrument/middleware/server.rb
|
137
|
+
- lib/sidekiq/instrument/mixin.rb
|
138
|
+
- lib/sidekiq/instrument/version.rb
|
139
|
+
- lib/sidekiq/instrument/worker.rb
|
140
|
+
- sidekiq-instrument.gemspec
|
141
|
+
homepage: https://github.com/enova/sidekiq-instrument
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.5.1
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: StatsD instrumentation for Sidekiq
|
165
|
+
test_files: []
|