wellness 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40af274c19cbb00938818ef0631110dacb17ab42
4
+ data.tar.gz: 5ba2fb05f9af7596a65c53abe0e8736d3e9618df
5
+ SHA512:
6
+ metadata.gz: d142661b49a6691d62decd0ff70bcd21fa7743ded4402a2ddcf4e626d27029ab41f2c9d8e677bd4f023cfc2af7e12336c53e6bd093d2290a2791ac33f0650739
7
+ data.tar.gz: f7e046d436b3181a7f2e5b29714d6d731f4f6e1dcf10408699c5bc85a9dff9fb8ca9f6b1aca9644885de5503633d64230e2d0cb0a369a7f423fde9e02c253d42
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ wellness
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wellness.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matthew Johnston
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Wellness
2
+
3
+ A rack middleware library that adds a health check to your service. It comes
4
+ with pre made services and has the option and flexibility for you to make your
5
+ own.
6
+
7
+ ## Usage - Rails
8
+
9
+ There are some pre configured services that are provided with this gem.
10
+ However, you must require them when you need them. This is because they have
11
+ external dependencies that need to be loaded, that your application may not
12
+ necessarily have.
13
+
14
+ ```rb
15
+ # app/config/application.rb
16
+
17
+ # Add this to the top just below the bundler requires
18
+ require 'wellness/services/postgres_service'
19
+ require 'wellness/services/redis_service'
20
+
21
+ # Within the configuration block
22
+ system = Wellness::System.new('my-uber-duber-app')
23
+ pg = Wellness::Services::PostgresService.new({
24
+ host: ENV['POSTGRESQL_HOST'],
25
+ port: ENV['POSTGRESQL_PORT'],
26
+ database: ENV['POSTGRESQL_DATABASE'],
27
+ user: ENV['POSTGRESQL_USERNAME'],
28
+ password: ENV['POSTGRESQL_PASSWORD']
29
+ })
30
+ redis = Wellness::Services::RedisService.new({
31
+ host: ENV['REDIS_HOST']
32
+ })
33
+ system.add_service('database', pg)
34
+ system.add_service('redis', redis)
35
+ config.middleware.insert_before('::ActiveRecord::QueryCache', 'Wellness::Checker', system)
36
+ ```
37
+
38
+ ## Usage - Sinatra
39
+
40
+ ```ruby
41
+ require 'wellness'
42
+ require 'wellness/services/postgres_service'
43
+ require 'wellness/services/redis_service'
44
+
45
+ system = Wellness::System.new('my-uber-duber-app')
46
+ pg = Wellness::Services::PostgresService.new({
47
+ host: ENV['POSTGRESQL_HOST'],
48
+ port: ENV['POSTGRESQL_PORT'],
49
+ database: ENV['POSTGRESQL_DATABASE'],
50
+ user: ENV['POSTGRESQL_USERNAME'],
51
+ password: ENV['POSTGRESQL_PASSWORD']
52
+ })
53
+ redis = Wellness::Services::RedisService.new({
54
+ host: ENV['REDIS_HOST']
55
+ })
56
+ system.add_service('database', pg)
57
+ system.add_service('redis', redis)
58
+
59
+ use(Wellness::Checker, system)
60
+ ```
61
+
62
+ ## Custom Services
63
+
64
+ Creating custom services is really easy. Always extend
65
+ `Wellness::Services::Base`.
66
+
67
+ Once that is done, you must implement the `#check` method.
68
+
69
+ The parameters passed into the service at creation are stored under `#params`.
70
+ Under no circumstances, should you ever modify the original parameters list at
71
+ run time. It can lead to unintended consequences, and weird bugs.
72
+
73
+ ```ruby
74
+ # Your custom service
75
+ class MyCustomService < Wellness::Services::Base
76
+ def check
77
+ if params[:foo]
78
+ passed_check
79
+ {
80
+ 'status': 'HEALTHY'
81
+ }
82
+ else
83
+ failed_check
84
+ {
85
+ 'status': 'UNHEALTHY'
86
+ }
87
+ end
88
+ end
89
+ end
90
+
91
+ # Initialize the wellness system
92
+ system = Wellness::System.new('my-app')
93
+ service = MyCustomService.new({foo: 'bar'})
94
+ system.add_service('some service', service)
95
+
96
+ # Load it into your rack
97
+ use(Wellness::Checker, system)
98
+ ```
99
+
100
+ ## Contributing
101
+
102
+ 1. Fork it
103
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
104
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
105
+ 4. Push to the branch (`git push origin my-new-feature`)
106
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/wellness.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Wellness
2
+ end
3
+
4
+ require 'wellness/version'
5
+ require 'wellness/services/base'
6
+ require 'wellness/system'
7
+ require 'wellness/checker'
@@ -0,0 +1,42 @@
1
+ module Wellness
2
+
3
+ class Checker
4
+ def initialize(app, system, options={})
5
+ @app = app
6
+ @system = system
7
+
8
+ # Optional arguments
9
+ @health_status_path = options[:status_path] || '/health/status'
10
+ @health_details_path = options[:details_path] || '/health/details'
11
+ end
12
+
13
+ def call(env)
14
+ case env['PATH_INFO']
15
+ when @health_status_path
16
+ health_status(env)
17
+ when @health_details_path
18
+ health_details(env)
19
+ else
20
+ @app.call(env)
21
+ end
22
+ end
23
+
24
+ def health_status(env)
25
+ if @system.check
26
+ [200, {'Content-Type' => 'text/json'}, [{status: 'HEALTHY'}.to_json]]
27
+ else
28
+ [500, {'Content-Type' => 'text/json'}, [{status: 'UNHEALTHY'}.to_json]]
29
+ end
30
+
31
+ end
32
+
33
+ def health_details(env)
34
+ if @system.check
35
+ [200, {'Content-Type' => 'text/json'}, [@system.to_json]]
36
+ else
37
+ [500, {'Content-Type' => 'text/json'}, [@system.to_json]]
38
+ end
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,48 @@
1
+ module Wellness
2
+ module Services
3
+
4
+ # @author Matthew A. Johnston
5
+ class Base
6
+ def initialize(params={})
7
+ @params = params
8
+ @health = false
9
+ end
10
+
11
+ # Flags the check as failed
12
+ def failed_check
13
+ @health = false
14
+ end
15
+
16
+ # Flags the check as passed
17
+ def passed_check
18
+ @health = true
19
+ end
20
+
21
+ def params
22
+ @params.dup
23
+ end
24
+
25
+ # Returns true if the service is healthy, otherwise false
26
+ # @return [Boolean]
27
+ def healthy?
28
+ !!@health
29
+ end
30
+
31
+ # @return [Hash]
32
+ def call
33
+ @last_check = self.check
34
+ end
35
+
36
+ # @return [Hash]
37
+ def check
38
+ {}
39
+ end
40
+
41
+ # @return [Hash]
42
+ def last_check
43
+ @last_check || {}
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,63 @@
1
+ require 'pg'
2
+ require 'wellness/services/base'
3
+
4
+ module Wellness
5
+ module Services
6
+
7
+ # @author Matthew A. Johnston
8
+ class PostgresService < Wellness::Services::Base
9
+ def check
10
+ case ping
11
+ when PG::Constants::PQPING_NO_ATTEMPT
12
+ ping_failed('no attempt made to ping')
13
+ when PG::Constants::PQPING_NO_RESPONSE
14
+ ping_failed('no response from ping')
15
+ when PG::Constants::PQPING_REJECT
16
+ ping_failed('ping was rejected')
17
+ else
18
+ ping_successful
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def ping
25
+ PG::Connection.ping(connection_options)
26
+ end
27
+
28
+ # @return [Hash]
29
+ def connection_options
30
+ {
31
+ host: self.params[:host],
32
+ port: self.params[:port],
33
+ dbname: self.params[:database],
34
+ user: self.params[:user],
35
+ password: self.params[:password]
36
+ }
37
+ end
38
+
39
+ # @param message [String] the reason it failed
40
+ # @return [Hash]
41
+ def ping_failed(message)
42
+ failed_check
43
+ {
44
+ status: 'UNHEALTHY',
45
+ details: {
46
+ error: message
47
+ }
48
+ }
49
+ end
50
+
51
+ # @return [Hash]
52
+ def ping_successful
53
+ passed_check
54
+ {
55
+ status: 'HEALTHY',
56
+ details: {
57
+ }
58
+ }
59
+ end
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,46 @@
1
+ require 'redis'
2
+ require 'wellness/services/base'
3
+
4
+ module Wellness
5
+ module Services
6
+
7
+ class RedisService < Wellness::Services::Base
8
+ KEYS = [
9
+ 'used_memory_human',
10
+ 'connected_clients',
11
+ 'keyspace_misses',
12
+ 'keyspace_hits',
13
+ 'evicted_keys',
14
+ 'expired_keys',
15
+ 'sync_partial_err',
16
+ 'sync_partial_ok',
17
+ 'sync_full',
18
+ 'rejected_connections',
19
+ 'total_commands_processed',
20
+ 'total_connections_received',
21
+ 'uptime_in_seconds',
22
+ 'uptime_in_days'
23
+ ]
24
+
25
+ def check
26
+ client = Redis.new(self.params)
27
+ details = client.info.select { |k,_| KEYS.include?(k) }
28
+
29
+ passed_check
30
+ {
31
+ status: 'HEALTHY',
32
+ details: details
33
+ }
34
+ rescue Redis::BaseError => error
35
+ failed_check
36
+ {
37
+ status: 'UNHEALTHY',
38
+ details: {
39
+ error: error.message
40
+ }
41
+ }
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,39 @@
1
+ module Wellness
2
+ module Services
3
+
4
+ # @author Matthew A. Johnston
5
+ class SidekiqService < Wellness::Services::Base
6
+ KEYS = %w(redis_stats uptime_in_days connected_clients used_memory_human used_memory_peak_human)
7
+
8
+ def check
9
+ sidekiq_stats = Sidekiq::Stats.new
10
+ queue = Sidekiq::Queue.new
11
+ redis = Redis.new(host: params[:redis_host])
12
+ redis_stats = redis.info.select { |k, _| KEYS.include?(k) }
13
+ workers_size = redis.scard("workers").to_i
14
+
15
+ {
16
+ status: 'HEALTHY',
17
+ details: {
18
+ processed: sidekiq_stats.processed,
19
+ failed: sidekiq_stats.failed,
20
+ busy: workers_size,
21
+ enqueued: sidekiq_stats.enqueued,
22
+ scheduled: sidekiq_stats.scheduled_size,
23
+ retries: sidekiq_stats.retry_size,
24
+ default_latency: queue.latency,
25
+ redis: redis_stats
26
+ }
27
+ }
28
+ rescue => error
29
+ {
30
+ status: 'UNHEALTHY',
31
+ details: {
32
+ error: error.message
33
+ }
34
+ }
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,63 @@
1
+ module Wellness
2
+
3
+ # @author Matthew A. Johnston
4
+ class System
5
+ attr_reader :name, :services
6
+
7
+ attr_accessor :details
8
+
9
+ # @param name [String] the name of the system
10
+ def initialize(name)
11
+ @name = name
12
+ @services = Hash.new
13
+ @details = Hash.new
14
+ end
15
+
16
+ # Add a service to this system
17
+ #
18
+ # @param name [String, Symbol] the name of the service
19
+ # @param service [Wellness::Service] the service you wish to add
20
+ def add_service(name, service)
21
+ @services[name] = service
22
+ end
23
+
24
+ # Remove a service from this system
25
+ #
26
+ # @param name [String, Symbol]
27
+ # @return [Wellness::Service] the service removed, else nil
28
+ def remove_service(name)
29
+ @services.delete(name)
30
+ end
31
+
32
+ # Checks all of the services
33
+ # @return
34
+ def check
35
+ @services.values.each { |service| service.call }
36
+ healthy?
37
+ end
38
+
39
+ # Returns true if the system is healthy, false otherwise
40
+ #
41
+ # @return [Boolean]
42
+ def healthy?
43
+ @services.values.all? { |service| service.healthy? }
44
+ end
45
+
46
+ def to_json(*)
47
+ dependencies = Hash.new
48
+
49
+ @services.each do |name, service|
50
+ dependencies[name] = service.last_check
51
+ end
52
+
53
+ data = {
54
+ status: (healthy? ? 'HEALTHY' : 'UNHEALTHY'),
55
+ details: @details,
56
+ dependencies: dependencies
57
+ }
58
+
59
+ data.to_json
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,3 @@
1
+ module Wellness
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'wellness'
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wellness::Checker do
4
+ pending
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wellness::System do
4
+ let(:name) { 'testing-app' }
5
+ let(:system) { described_class.new(name) }
6
+
7
+ describe '#name' do
8
+ subject { system.name }
9
+ it 'equals "testing-app"' do
10
+ expect(subject).to eq(name)
11
+ end
12
+ end
13
+
14
+ describe '#add_service' do
15
+ let(:service) { double('Service') }
16
+ subject { system.add_service('foo', service) }
17
+
18
+ it 'adds the service to the system' do
19
+ expect { subject }.to change(system.services, :length).to(1)
20
+ end
21
+ end
22
+
23
+ describe '#remove_service' do
24
+ let(:service) { double('Service') }
25
+ subject { system.remove_service('foo') }
26
+ before { system.add_service('foo', service) }
27
+ it 'removes the service from the system' do
28
+ expect { subject }.to change(system.services, :length).to(0)
29
+ end
30
+ end
31
+
32
+ end
data/wellness.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wellness/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'wellness'
8
+ spec.version = Wellness::VERSION
9
+ spec.authors = ['Matthew Johnston']
10
+ spec.email = ['warmwaffles@gmail.com']
11
+ spec.description = 'A rack middleware health check'
12
+ spec.summary = 'A rack middleware health check'
13
+ spec.homepage = 'https://github.com/warmwaffles/wellness'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency('rack', '~> 1.5')
22
+
23
+ spec.add_development_dependency('bundler', '~> 1.3')
24
+ spec.add_development_dependency('rake')
25
+ spec.add_development_dependency('rspec')
26
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wellness
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Johnston
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
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
+ description: A rack middleware health check
70
+ email:
71
+ - warmwaffles@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .ruby-gemset
78
+ - .ruby-version
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/wellness.rb
84
+ - lib/wellness/checker.rb
85
+ - lib/wellness/services/base.rb
86
+ - lib/wellness/services/postgres_service.rb
87
+ - lib/wellness/services/redis_service.rb
88
+ - lib/wellness/services/sidekiq_service.rb
89
+ - lib/wellness/system.rb
90
+ - lib/wellness/version.rb
91
+ - spec/spec_helper.rb
92
+ - spec/wellness/checker_spec.rb
93
+ - spec/wellness/system_spec.rb
94
+ - wellness.gemspec
95
+ homepage: https://github.com/warmwaffles/wellness
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.1.11
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: A rack middleware health check
119
+ test_files:
120
+ - spec/spec_helper.rb
121
+ - spec/wellness/checker_spec.rb
122
+ - spec/wellness/system_spec.rb