sidekiq-monitoring 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +133 -0
  3. data/VERSION +1 -0
  4. data/lib/sidekiq-monitoring.rb +86 -0
  5. metadata +143 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe841d0534ee44c3874a938fe210f0ebe2eb61ba
4
+ data.tar.gz: 8921f0909260f88dea2875ca2a46dd327c5c4c84
5
+ SHA512:
6
+ metadata.gz: 800b10be6050093c91a90d081e4ac1b66de180c360f5b4310088a80f0b36cd5edc9342767b073c811e88e35b2787aaff80f70cf021990fc55a2fe2ef21fb5ab0
7
+ data.tar.gz: 86d7c72544cc46c118db18705a02456b9954ddcb7efac08ae77727f1a30afd575299e7d11b97f13f1cca346cd4677abef80e36532e155cfce6b899d8280fdd4b
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Rails 3 and 4
2
+
3
+ Add `sinatra` (and `sprockets` if you are on Rails 3.0) to your Gemfile
4
+
5
+ ```ruby
6
+ # if you require 'sinatra' you get the DSL extended to Object
7
+ gem 'sinatra', '>= 1.3.0', :require => nil
8
+ ```
9
+
10
+ Add the following to your `config/routes.rb`:
11
+
12
+ ```ruby
13
+ mount SidekiqMonitoring => '/checks'
14
+ ```
15
+
16
+ # URL endpoint
17
+
18
+ To know the state of your sidekiq queues, go to: `<your_website_url>/checks/sidekiq_queues`
19
+ Please remember to mount the route before going to this URL
20
+
21
+ # Define custom threshold
22
+
23
+ Add the following to an initializer:
24
+
25
+ ```ruby
26
+ SidekiqMonitoring.thresholds = {
27
+ 'queue_name_1' => [warning, critical],
28
+ 'queue_name_2' => [warning, critical],
29
+ 'queue_name_3' => [warning, critical]
30
+ }
31
+ ```
32
+
33
+ Or if you want to override the default threshold in case of the queue name
34
+ isn't specified in your thresholds:
35
+
36
+ ```ruby
37
+ SidekiqMonitoring.thresholds = {
38
+ 'default' => [warning, critical],
39
+ 'queue_name' => [warning, critical],
40
+ }
41
+ ```
42
+
43
+ ### Security
44
+
45
+ In a production application you'll likely want to protect access to this information. You can use the constraints feature of routing (in the _config/routes.rb_ file) to accomplish this:
46
+
47
+ #### Token
48
+
49
+ Allow any user who have a valid token
50
+
51
+ ```ruby
52
+ constraints lambda { |req| req.params[:access_token] == '235b0ddfa5867d81a3232fa6c997a382' } do
53
+ mount SidekiqMonitoring, :at => '/checks'
54
+ end
55
+ ```
56
+
57
+ #### Devise
58
+
59
+ Allow any authenticated `User`
60
+
61
+ ```ruby
62
+ # config/routes.rb
63
+ authenticate :user do
64
+ mount SidekiqMonitoring => '/checks'
65
+ end
66
+ ```
67
+
68
+ Same as above but also ensures that `User#admin?` returns true
69
+
70
+ ```ruby
71
+ # config/routes.rb
72
+ authenticate :user, lambda { |u| u.admin? } do
73
+ mount SidekiqMonitoring => '/checks'
74
+ end
75
+ ```
76
+
77
+ #### Authlogic
78
+
79
+ ```ruby
80
+ # lib/admin_constraint.rb
81
+ class AdminConstraint
82
+ def matches?(request)
83
+ return false unless request.cookies['user_credentials'].present?
84
+ user = User.find_by_persistence_token(request.cookies['user_credentials'].split(':')[0])
85
+ user && user.admin?
86
+ end
87
+ end
88
+
89
+ # config/routes.rb
90
+ require "admin_constraint"
91
+ mount SidekiqMonitoring => '/checks', :constraints => AdminConstraint.new
92
+ ```
93
+
94
+ #### Restful Authentication
95
+
96
+ Checks a `User` model instance that responds to `admin?`
97
+
98
+ ```ruby
99
+ # lib/admin_constraint.rb
100
+ class AdminConstraint
101
+ def matches?(request)
102
+ return false unless request.session[:user_id]
103
+ user = User.find request.session[:user_id]
104
+ user && user.admin?
105
+ end
106
+ end
107
+
108
+ # config/routes.rb
109
+ require 'admin_constraint'
110
+ mount SidekiqMonitoring => '/checks', :constraints => AdminConstraint.new
111
+ ```
112
+
113
+ #### Custom External Authentication
114
+
115
+ ```ruby
116
+ class AuthConstraint
117
+ def self.admin?(request)
118
+ return false unless (cookie = request.cookies['auth'])
119
+
120
+ Rails.cache.fetch(cookie['user'], :expires_in => 1.minute) do
121
+ auth_data = JSON.parse(Base64.decode64(cookie['data']))
122
+ response = HTTParty.post(Auth.validate_url, :query => auth_data)
123
+
124
+ response.code == 200 && JSON.parse(response.body)['roles'].to_a.include?('Admin')
125
+ end
126
+ end
127
+ end
128
+
129
+ # config/routes.rb
130
+ constraints lambda {|request| AuthConstraint.admin?(request) } do
131
+ mount SidekiqMonitoring => '/checks'
132
+ end
133
+ ```
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4
@@ -0,0 +1,86 @@
1
+ require 'sinatra/base'
2
+ require 'multi_json'
3
+
4
+ class SidekiqMonitoring < Sinatra::Base
5
+
6
+ cattr_accessor :thresholds
7
+
8
+ get '/sidekiq_queues' do
9
+ content_type :json
10
+ MultiJson.dump SidekiqMonitoring::Global.new(thresholds)
11
+ end
12
+
13
+ class Queue
14
+
15
+ ALERT_STATUS = {
16
+ 'OK' => 0,
17
+ 'WARNING' => 1,
18
+ 'CRITICAL' => 2,
19
+ 'UNKNOWN' => 3
20
+ }
21
+
22
+ DEFAULT_THRESHOLD = [ 1_000, 2_000 ]
23
+
24
+ attr_accessor :name, :size, :warning_threshold, :critical_threshold, :status
25
+
26
+ def as_json(options = {})
27
+ {
28
+ 'name' => name,
29
+ 'size' => size,
30
+ 'warning_threshold' => warning_threshold,
31
+ 'critical_threshold' => critical_threshold,
32
+ 'status' => status
33
+ }
34
+ end
35
+
36
+ def initialize(name, size, thresholds = nil)
37
+ @name = name
38
+ @size = size
39
+ @warning_threshold, @critical_threshold = (thresholds.present? ? thresholds : DEFAULT_THRESHOLD)
40
+ @status = monitoring_status
41
+ end
42
+
43
+ def <=>(other)
44
+ ALERT_STATUS[status] <=> ALERT_STATUS[other.status]
45
+ end
46
+
47
+ def monitoring_status
48
+ return 'CRITICAL' if size >= critical_threshold
49
+ return 'WARNING' if size >= warning_threshold
50
+ 'OK'
51
+ end
52
+
53
+ end
54
+
55
+ class Global
56
+
57
+ attr_accessor :thresholds
58
+
59
+ def as_json(options = {})
60
+ {
61
+ 'global_status' => global_status,
62
+ 'queues' => queues.as_json
63
+ }
64
+ end
65
+
66
+ def global_status
67
+ @global_status ||= queues.sort.last.try(:status) || 'UNKNOWN'
68
+ end
69
+
70
+ def initialize(thresholds = {})
71
+ @thresholds = thresholds
72
+ end
73
+
74
+ def queues
75
+ @queues ||= Sidekiq::Queue.all.collect{ |queue|
76
+ Queue.new(queue.name, queue.size, thresholds_from_queue(queue.name))
77
+ }
78
+ end
79
+
80
+ def thresholds_from_queue(queue_name)
81
+ thresholds.fetch(queue_name) { |threshold| threshold['default'] }
82
+ end
83
+
84
+ end
85
+
86
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-monitoring
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.4'
5
+ platform: ruby
6
+ authors:
7
+ - Jérémy Carlier
8
+ autorequire:
9
+ bindir: ''
10
+ cert_chain: []
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sidekiq
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.1.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.1.0
111
+ description: Give a state of sidekiq available queues
112
+ email: jeremy.carlier@dimelo.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - README.md
118
+ - VERSION
119
+ - lib/sidekiq-monitoring.rb
120
+ homepage: http://github.com/dimelo/sidekiq-monitoring
121
+ licenses: []
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 1.9.2
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.2.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Addons to provide a monitoring API for Sidekiq
143
+ test_files: []