rails-monitoring 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e272c26624859b7d257917d9009e8d45e1d263f
4
+ data.tar.gz: f1cd488fff890a489f480d44237c9ed538c9f2c5
5
+ SHA512:
6
+ metadata.gz: 13a8655264c6ed5f5b3f1c12b9612aabed4113e5026012889709249b2168856fa95c991eb32068eb87d7a05f439aa617d7590a35c4849652fc2bfce671334e3b
7
+ data.tar.gz: 7e8898ec1768c3d8030e706a60a6f28387ec9c16054c543289a342c2d1312cdd6d6a8cc4adb1ce9c95a8ca27c19acfd25be9d6d4a48f9ee0c522817de406f5bf
@@ -0,0 +1,87 @@
1
+ # rails-monitoring[![Build Status](https://travis-ci.org/bitcrowd/rails-monitoring.svg?branch=master)](https://travis-ci.org/bitcrowd/rails-monitoring)
2
+
3
+ Rails engine that provides a JSON API which serves Sidekiq and Whenever
4
+ status information from a HTTP-auth protected endpoint.
5
+ This status information is gathered by scheduling a frequently
6
+ running job that saves timestamps in Redis.
7
+
8
+ ## Installation
9
+
10
+ Add gem to your project:
11
+
12
+ ```ruby
13
+ gem 'rails-monitoring'
14
+ ```
15
+
16
+ Mount engine in `routes.rb`:
17
+ ```ruby
18
+ Rails.application.routes.draw do
19
+ mount Rails::Monitoring::Engine => '/monitoring'
20
+ end
21
+ ```
22
+
23
+ Schedule job in `whenever`'s `schedule.rb`:
24
+
25
+ ```ruby
26
+ every 5.minutes do
27
+ runner 'Rails::Monitoring::Status.refresh'
28
+ end
29
+ ```
30
+
31
+ Be sure that your parent app provides HTTP basic authentication credentials
32
+ using an initializer
33
+
34
+ ```ruby
35
+ # app/config/initializers/monitoring.rb
36
+ Rails::Monitoring.http_auth_name = 'user'
37
+ Rails::Monitoring.http_auth_password = 'password'
38
+ ```
39
+
40
+ This engine's controller inherits from `ApplicationController` by default. You
41
+ can change this using the `parent_controller` option
42
+
43
+ ```ruby
44
+ Rails::Monitoring.parent_controller = 'SomeOtherController'
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ Navigate to `/monitoring/status` and receive Sidekiq status information:
50
+
51
+ {
52
+ "timestamps": {
53
+ "whenever_ran": "2018-01-23 15:05:02",
54
+ "sidekiq_performed": "2018-01-23 15:05:02",
55
+ "requested": "2018-01-23 15:08:28"
56
+ },
57
+ "sidekiq": {
58
+ "active_workers": 0,
59
+ "queue_sizes": {
60
+ "mailers": 0,
61
+ "default": 0,
62
+ "scheduled": 0,
63
+ "retries": 0,
64
+ "dead": 0
65
+ },
66
+ "recent_history": {
67
+ "processed": {
68
+ "2018-01-23": 18,
69
+ "2018-01-22": 0,
70
+ "2018-01-21": 0,
71
+ "2018-01-20": 0,
72
+ "2018-01-19": 0
73
+ },
74
+ "failed": {
75
+ "2018-01-23": 0,
76
+ "2018-01-22": 0,
77
+ "2018-01-21": 0,
78
+ "2018-01-20": 0,
79
+ "2018-01-19": 0
80
+ }
81
+ },
82
+ "totals": {
83
+ "processed": 18,
84
+ "failed": 0
85
+ }
86
+ }
87
+ }
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Rails::Monitoring'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,7 @@
1
+ module Rails
2
+ module Monitoring
3
+ class ApplicationController < Rails::Monitoring.parent_controller.constantize
4
+ protect_from_forgery with: :exception
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,72 @@
1
+ module Rails
2
+ module Monitoring
3
+ class StatusController < ApplicationController
4
+ http_basic_authenticate_with name: Rails::Monitoring.http_auth_name,
5
+ password: Rails::Monitoring.http_auth_password
6
+
7
+ def status
8
+ render json: status_data
9
+ end
10
+
11
+ private
12
+
13
+ def status_data
14
+ {
15
+ timestamps: timestamps,
16
+ sidekiq: sidekiq_data
17
+ }
18
+ end
19
+
20
+ def timestamps
21
+ Sidekiq.redis do |conn|
22
+ {
23
+ whenever_ran: conn.get('monitoring:timestamp:whenever_ran'),
24
+ sidekiq_performed: conn.get('monitoring:timestamp:sidekiq_performed'),
25
+ requested: Time.current.to_s(:db)
26
+ }
27
+ end
28
+ end
29
+
30
+ def sidekiq_data
31
+ {
32
+ active_workers: sidekiq_stats.workers_size,
33
+ queue_sizes: sidekiq_queue_sizes,
34
+ recent_history: recent_history,
35
+ totals: totals
36
+ }
37
+ end
38
+
39
+ def sidekiq_stats
40
+ @sidekiq_stats ||= Sidekiq::Stats.new
41
+ end
42
+
43
+ def sidekiq_history
44
+ @sidekiq_history ||= Sidekiq::Stats::History.new(5)
45
+ end
46
+
47
+ def recent_history
48
+ {
49
+ processed: sidekiq_history.processed,
50
+ failed: sidekiq_history.failed
51
+ }
52
+ end
53
+
54
+ def totals
55
+ {
56
+ processed: sidekiq_stats.processed,
57
+ failed: sidekiq_stats.failed
58
+ }
59
+ end
60
+
61
+ def sidekiq_queue_sizes
62
+ queue_sizes = sidekiq_stats.queues
63
+
64
+ queue_sizes.merge(
65
+ scheduled: sidekiq_stats.scheduled_size,
66
+ retries: sidekiq_stats.retry_size,
67
+ dead: sidekiq_stats.dead_size
68
+ )
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,6 @@
1
+ module Rails
2
+ module Monitoring
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ module Rails
2
+ module Monitoring
3
+ class RefreshStatusJob < ApplicationJob
4
+ def perform
5
+ Sidekiq.redis do |conn|
6
+ conn.set('monitoring:timestamp:sidekiq_performed', Time.current.to_s(:db))
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Rails
2
+ module Monitoring
3
+ class Status
4
+ def self.refresh
5
+ Sidekiq.redis do |conn|
6
+ conn.set('monitoring:timestamp:whenever_ran', Time.current.to_s(:db))
7
+ end
8
+ RefreshStatusJob.perform_later
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ Rails::Monitoring::Engine.routes.draw do
2
+ controller :status do
3
+ get :status
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails/monitoring/engine'
2
+
3
+ module Rails
4
+ module Monitoring
5
+ mattr_accessor :parent_controller
6
+ self.parent_controller = '::ApplicationController'
7
+
8
+ mattr_accessor :http_auth_name
9
+ mattr_accessor :http_auth_password
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq/api'
3
+
4
+ module Rails
5
+ module Monitoring
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace Rails::Monitoring
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Rails
2
+ module Monitoring
3
+ VERSION = '0.2.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-monitoring
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Julian Dobmann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq
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: fakeredis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: minitest-around
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
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: rubocop
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: rubocop-bitcrowd
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: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: timecop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: |
126
+ Rails engine that provides a JSON API which serves Sidekiq and Whenever
127
+ status information from a HTTP-auth protected endpoint.
128
+ This status information is gathered by scheduling a frequently
129
+ running job that saves timestamps in Redis.
130
+ email:
131
+ - julian@bitcrowd.net
132
+ executables: []
133
+ extensions: []
134
+ extra_rdoc_files: []
135
+ files:
136
+ - README.md
137
+ - Rakefile
138
+ - app/controllers/rails/monitoring/application_controller.rb
139
+ - app/controllers/rails/monitoring/status_controller.rb
140
+ - app/jobs/rails/monitoring/application_job.rb
141
+ - app/jobs/rails/monitoring/refresh_status_job.rb
142
+ - app/services/rails/monitoring/status.rb
143
+ - config/routes.rb
144
+ - lib/rails/monitoring.rb
145
+ - lib/rails/monitoring/engine.rb
146
+ - lib/rails/monitoring/version.rb
147
+ homepage: https://github.com/bitcrowd/rails-monitoring
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.6.13
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Provide Rails status information via JSON API.
171
+ test_files: []