health-monitor-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # health-monitor-rails
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/health-monitor-rails.png)](http://badge.fury.io/rb/health-monitor-rails)
4
+ [![Build Status](https://travis-ci.org/lbeder/health-monitor-rails.png)](https://travis-ci.org/lbeder/health-monitor-rails)
5
+ [![Dependency Status](https://gemnasium.com/lbeder/health-monitor-rails.png)](https://gemnasium.com/lbeder/health-monitor-rails)
6
+ [![Coverage Status](https://coveralls.io/repos/lbeder/health-monitor-rails/badge.png)](https://coveralls.io/r/lbeder/health-monitor-rails)
7
+
8
+ This is a health monitoring Rails mountable plug-in, which checks various services (db, cache, sidekiq, redis, etc.).
9
+
10
+ Mounting this gem will add a '/check' route to your application, which can be used for health monitoring the application and its various services.
11
+
12
+ ## Setup
13
+
14
+ If you are using bundler add health-monitor-rails to your Gemfile:
15
+
16
+ ```ruby
17
+ gem 'health-monitor-rails'
18
+ ```
19
+
20
+ Then run:
21
+
22
+ ```bash
23
+ $ bundle install
24
+ ```
25
+
26
+ Otherwise install the gem:
27
+
28
+ ```bash
29
+ $ gem install health-monitor-rails
30
+ ```
31
+
32
+ ## Usage
33
+ You can mount this inside your app routes by adding this to config/routes.rb:
34
+
35
+ ```ruby
36
+ mount HealthMonitor::Engine => '/health'
37
+ ```
38
+
39
+ ## Supported service providers
40
+ The following services are currently supported:
41
+ * DB
42
+ * Cache
43
+ * Redis
44
+ * Sidekiq
45
+
46
+ ## Configuration
47
+
48
+ ### Adding providers
49
+ By default, only the database check is enabled. You can add more service providers from an initializer:
50
+
51
+ ```ruby
52
+ HealthMonitor.configure do |config|
53
+ config.providers += [:cache, :redis, :sidekiq]
54
+ end
55
+ ```
56
+
57
+ You can also override the list with a custom set of providers:
58
+ ```ruby
59
+ HealthMonitor.configure do |config|
60
+ config.providers = [:sidekiq]
61
+ end
62
+ ```
63
+
64
+ ### Adding a custom error callback
65
+ If you need to perform any additional error handling (for example, for additional error reporting), you can configure a custom error callback:
66
+
67
+ ```ruby
68
+ HealthMonitor.configure do |config|
69
+ config.error_callback = proc do |e|
70
+ logger.error "Health check failed with: #{e.message}"
71
+
72
+ Raven.capture_exception(e)
73
+ end
74
+ end
75
+ ```
76
+
77
+ ## License
78
+
79
+ The MIT License (MIT)
80
+
81
+ Copyright (c) 2014
82
+
83
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
84
+ this software and associated documentation files (the "Software"), to deal in
85
+ the Software without restriction, including without limitation the rights to
86
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
87
+ the Software, and to permit persons to whom the Software is furnished to do so,
88
+ subject to the following conditions:
89
+
90
+ The above copyright notice and this permission notice shall be included in all
91
+ copies or substantial portions of the Software.
92
+
93
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
94
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
95
+ FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
96
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
97
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
98
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
9
+ load 'rails/tasks/engine.rake'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ require 'rspec/core/rake_task'
14
+
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.libs << 'lib'
17
+ t.libs << 'test'
18
+ t.pattern = 'test/**/*_test.rb'
19
+ t.verbose = false
20
+ end
21
+
22
+ task default: :test
@@ -0,0 +1,22 @@
1
+ class HealthController < ActionController::Base
2
+ layout nil
3
+
4
+ # GET /health/check
5
+ def check
6
+ HealthMonitor.check!
7
+
8
+ render text: "Healh check has passed: #{Time.now.to_s(:db)}\n"
9
+ rescue Exception => e
10
+ render text: "Healh check has failed: #{Time.now.to_s(:db)}, error: #{e.message}\n",
11
+ :status => :service_unavailable
12
+ end
13
+
14
+ private
15
+ def process_with_silence(*args)
16
+ logger.quietly do
17
+ process_without_silence(*args)
18
+ end
19
+ end
20
+
21
+ alias_method_chain :process, :silence
22
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ HealthMonitor::Engine.routes.draw do
2
+ controller :health do
3
+ get :check
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'health_monitor/version'
2
+ require 'health_monitor/engine'
3
+ require 'health_monitor/configuration'
4
+ require 'health_monitor/monitor'
@@ -0,0 +1,9 @@
1
+ module HealthMonitor
2
+ class Configuration
3
+ attr_accessor :providers, :error_callback
4
+
5
+ def initialize
6
+ @providers = [:database]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module HealthMonitor
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace HealthMonitor
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'health_monitor/configuration'
2
+
3
+ module HealthMonitor
4
+ extend self
5
+
6
+ attr_accessor :configuration
7
+
8
+ def configure
9
+ self.configuration ||= Configuration.new
10
+
11
+ yield configuration if block_given?
12
+ end
13
+
14
+ def check!
15
+ configuration.providers.each do |provider|
16
+ require "health_monitor/providers/#{provider}"
17
+
18
+ monitor = "HealthMonitor::Providers::#{provider.capitalize}".constantize.new
19
+ monitor.check!
20
+ end
21
+ rescue Exception => e
22
+ configuration.error_callback.call(e) if configuration.error_callback
23
+ raise
24
+ end
25
+ end
26
+
27
+ HealthMonitor.configure
@@ -0,0 +1,14 @@
1
+ module HealthMonitor
2
+ module Providers
3
+ class Cache
4
+ def check!
5
+ time = Time.now.to_s
6
+
7
+ Rails.cache.write(:health, time)
8
+ fetched = Rails.cache.read(:health)
9
+
10
+ raise "different values (now: #{time}, fetched: #{fetched}" if fetched != time
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module HealthMonitor
2
+ module Providers
3
+ class DBException < StandardError; end
4
+
5
+ class Database
6
+ def check!
7
+ # Check connection to the DB:
8
+ query = 'SELECT version FROM schema_migrations'
9
+ ActiveRecord::Base.connection.select_value(query).to_i
10
+ rescue => e
11
+ raise DBException.new(e.message)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require 'redis/namespace'
2
+
3
+ module HealthMonitor
4
+ module Providers
5
+ class RedisException < StandardError; end
6
+
7
+ class Redis
8
+ def check!
9
+ time = Time.now.to_s(:db)
10
+
11
+ r = ::Redis.new
12
+ r.set(:health, time)
13
+ fetched = r.get(:health)
14
+
15
+ raise "different values (now: #{time}, fetched: #{fetched}" if fetched != time
16
+ rescue => e
17
+ raise RedisException.new(e.message)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'sidekiq'
2
+
3
+ module HealthMonitor
4
+ module Providers
5
+ class SidekiqException < StandardError; end
6
+
7
+ class Sidekiq
8
+ def check!
9
+ ::Sidekiq::Workers.new.size
10
+ rescue => e
11
+ raise SidekiqException.new(e.message)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module HealthMonitor
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: health-monitor-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Leonid Beder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: database_cleaner
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: spork
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: pry
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: coveralls
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
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
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rake
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: rediska
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: sidekiq
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ description: Health monitoring Rails plug-in, which checks various services (db, cache,
175
+ sidekiq, redis, etc.).
176
+ email:
177
+ - leonid.beder@gmail.com
178
+ executables: []
179
+ extensions: []
180
+ extra_rdoc_files: []
181
+ files:
182
+ - app/controllers/health_controller.rb
183
+ - config/routes.rb
184
+ - lib/health-monitor-rails.rb
185
+ - lib/health_monitor/configuration.rb
186
+ - lib/health_monitor/engine.rb
187
+ - lib/health_monitor/monitor.rb
188
+ - lib/health_monitor/providers/cache.rb
189
+ - lib/health_monitor/providers/database.rb
190
+ - lib/health_monitor/providers/redis.rb
191
+ - lib/health_monitor/providers/sidekiq.rb
192
+ - lib/health_monitor/version.rb
193
+ - Rakefile
194
+ - README.md
195
+ homepage: https://github.com/lbeder/health-monitor-rails
196
+ licenses:
197
+ - MIT
198
+ post_install_message:
199
+ rdoc_options: []
200
+ require_paths:
201
+ - lib
202
+ required_ruby_version: !ruby/object:Gem::Requirement
203
+ none: false
204
+ requirements:
205
+ - - ! '>='
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ segments:
209
+ - 0
210
+ hash: -2918892546612586905
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ none: false
213
+ requirements:
214
+ - - ! '>='
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ segments:
218
+ - 0
219
+ hash: -2918892546612586905
220
+ requirements: []
221
+ rubyforge_project:
222
+ rubygems_version: 1.8.25
223
+ signing_key:
224
+ specification_version: 3
225
+ summary: Health monitoring Rails plug-in, which checks various services (db, cache,
226
+ sidekiq, redis, etc.)
227
+ test_files: []