leafy-health 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: 19b3d546b5202841ed7456d0e3558311f3d485d8
4
+ data.tar.gz: 2942aaf142b3c01f4ddaf1ffd07d2d011d5bbc4b
5
+ SHA512:
6
+ metadata.gz: 11fcc8168409f777ebfed83bc2286615e40b5e0b50223d7dfa992512b57a0ca8fabf41ee108afb1a93f46cd6fec17ce4c53c644157f8a864f21531ab80fea2af
7
+ data.tar.gz: dfbb26d6b09877fe02f6a3dacf56aec6d6008868b17e0cd936bab395663bd202274f3dd60449e8d9d474e9ee4d56a05fe8e36f27fe822ab888cb40ae1e62072e
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .yardoc
2
+ doc
3
+ pkg
4
+ *.lock
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ # vim: syntax=Ruby
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Lookout
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # Leafy-Health
2
+
3
+ ## installation
4
+
5
+ via rubygems
6
+ ```
7
+ gem install leafy-health
8
+ ```
9
+ or add to your Gemfile
10
+ ```
11
+ gem 'leqfy-health'
12
+ ```
13
+
14
+ installing the gem also takes care of the jar dependencies with jruby-1.7.16+
15
+
16
+ ## usage
17
+
18
+ an instance of the registry ```Leafy::Health::Registry``` can register and unresgister health-checks under a given name. any object with a #call method will do or block on the register method.
19
+
20
+ registry = Leafy::Health::Registry.new
21
+
22
+ you can ask the registry which names have already health-checks registered:
23
+
24
+ registry.names
25
+
26
+ ### simple health check
27
+
28
+ simple in the sense that either call returns ```nil``` which means healthy or a message which is the unhealthy state. the message can be any ```String```.
29
+
30
+ registry.register( 'simple.block') do
31
+ if app.crashed
32
+ 'application crashed'
33
+ end
34
+ end
35
+
36
+ or with a health-check object
37
+
38
+ class AppCheck
39
+ def call
40
+ if app.crashed
41
+ 'application crashed'
42
+ end
43
+ enf
44
+ end
45
+ registry.register( 'simple.class', AppCheck.new )
46
+
47
+ ### health checks with message on healthy state
48
+
49
+ here the call method gets an argument which allows to create both
50
+ healthy and unhealthy states with message.
51
+
52
+ registry.register( 'app.block') do |health|
53
+ if app.crashed
54
+ health.unhealthy( 'application crashed' )
55
+ else
56
+ health.healthy( 'application ok' )
57
+ end
58
+ end
59
+
60
+ or with a health-check object
61
+
62
+ class AppCheck
63
+ def call( health )
64
+ if app.crashed
65
+ health.unhealthy( 'application crashed' )
66
+ else
67
+ health.healthy( 'application ok' )
68
+ end
69
+ end
70
+ end
71
+ registry.register( 'app.class', AppCheck.new )
72
+
73
+ ### unregister health checks
74
+
75
+ registry.unregister( 'app.class' )
76
+
77
+ ### note
78
+
79
+ currently there is not further introspection on the registry and its health-check. with the ```Leafy::Json::HealthWriter``` (from leafy-rack) you can get a json representation of the current **health report**
80
+
81
+ Leafy::Json::HealthWriter.to_json( registry.health )
82
+
83
+ ## developement
84
+
85
+ get all the gems and jars in place
86
+
87
+ gem install jar-dependencies --development
88
+ bundle install
89
+
90
+ for running all specs
91
+
92
+ rake
93
+
94
+ or
95
+
96
+ rspec spec/reporter_spec.rb
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new
5
+
6
+ require "yard"
7
+ YARD::Rake::YardocTask.new do |t|
8
+ t.files = ['lib/**/*.rb']
9
+ t.options += ["--title", "Leafy Health API"]
10
+ end
11
+
12
+ task :default => [ :spec ]
13
+
14
+ # vim: syntax=Ruby
@@ -0,0 +1,26 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ require File.expand_path( '../lib/leafy/health/version', __FILE__ )
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'leafy-health'
7
+ s.version = Leafy::Health::VERSION
8
+ s.author = 'christian meier'
9
+ s.email = [ 'christian.meier@lookout.com' ]
10
+
11
+ s.license = 'MIT'
12
+ s.summary = %q(provides an API to register healthchecks)
13
+ s.homepage = 'https://github.com/lookout/leafy'
14
+ s.description = %q(provides an API to register healthchecks which uses dropwizrd-metrics-healthchecks)
15
+
16
+ s.files = `git ls-files`.split($/)
17
+
18
+ s.requirements << 'jar io.dropwizard.metrics:metrics-healthchecks, 3.1.0'
19
+
20
+ s.add_runtime_dependency 'jar-dependencies', '~> 0.1.8'
21
+ s.add_development_dependency 'rspec', '~> 3.1.0'
22
+ s.add_development_dependency 'yard', '~> 0.8.7'
23
+ s.add_development_dependency 'rake', '~> 10.2'
24
+ end
25
+
26
+ # vim: syntax=Ruby
@@ -0,0 +1,90 @@
1
+ require 'leafy/health'
2
+ module Leafy
3
+ module Health
4
+ class Registry
5
+
6
+ class HealthCheck < com.codahale.metrics.health.HealthCheck
7
+
8
+ # create healthy result object with given message
9
+ #
10
+ # param [String] optional result message, can be nil
11
+ # return [com.codahale.metrics.health.HealthCheck::Result]
12
+ def healthy( result = nil )
13
+ com.codahale.metrics.health.HealthCheck::Result.healthy( result )
14
+ end
15
+
16
+ # create unhealthy result object with given message
17
+ #
18
+ # param [String] result message
19
+ # return [com.codahale.metrics.health.HealthCheck::Result]
20
+ def unhealthy( result )
21
+ com.codahale.metrics.health.HealthCheck::Result.unhealthy( result )
22
+ end
23
+
24
+ def initialize( block )
25
+ super()
26
+ @block = block
27
+ end
28
+
29
+ def check
30
+ case result = @block.call( self )
31
+ when String
32
+ unhealthy( result )
33
+ when NilClass
34
+ healthy
35
+ else
36
+ result
37
+ end
38
+ end
39
+ end
40
+
41
+ # state ofthe registry
42
+ attr_reader :health
43
+
44
+ def initialize
45
+ @health = com.codahale.metrics.health.HealthCheckRegistry.new
46
+ end
47
+
48
+ # register a HealthCheck under a given name
49
+ #
50
+ # @param [String] name
51
+ # @param [String] instead of block any check object which responds to 'call'
52
+ # @yieldparam [HealthCheckRegistry::HealthCheck] which has convienient methods to create healthy and unhealthy results with message
53
+ # @yieldreturn [String] if the healthcheck fails return the message
54
+ # @yieldreturn [NilClass] if the healthcheck succeeds
55
+ # @yieldreturn [com.codahale.metrics.health.HealthCheck::Result] if the check produces its own result object
56
+ def register(name, check = nil, &block )
57
+ if check and not block_given? and check.respond_to? :call
58
+ @health.register( name, HealthCheck.new( check ) )
59
+
60
+ elsif check.nil? and block_given?
61
+ @health.register( name, HealthCheck.new( block ) )
62
+ else
63
+ raise 'needs either a block and object with call method'
64
+ end
65
+ end
66
+
67
+ # unregister a HealthCheck for a given name
68
+ #
69
+ # @param [String] name
70
+ def unregister(name)
71
+ @health.unregister(name)
72
+ end
73
+
74
+ # the names of all registered HealthCheck
75
+ #
76
+ # @return [Array<String>] names of HealthCheck in order of their registration
77
+ def names
78
+ @health.names.to_a
79
+ end
80
+
81
+ # # run a healthcheck for a given name
82
+ # #
83
+ # # @param [String] name
84
+ # # @return [Java::ComCodahaleMetricsHealth::HealthCheck::Result] result of the health-check
85
+ # def run_health_check(name)
86
+ # @health.runHealthCheck(name)
87
+ # end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,6 @@
1
+ module Leafy
2
+ module Health
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
6
+
@@ -0,0 +1,2 @@
1
+ require 'leafy-health_jars'
2
+ require 'leafy/health/registry'
@@ -0,0 +1,2 @@
1
+ require 'leafy-health_jars'
2
+ require 'leafy/health/registry'
@@ -0,0 +1,5 @@
1
+ # this is a generated file, to avoid over-writing it just delete this comment
2
+ require 'jar_dependencies'
3
+
4
+ require_jar( 'io.dropwizard.metrics', 'metrics-healthchecks', '3.1.0' )
5
+ require_jar( 'org.slf4j', 'slf4j-api', '1.7.7' )
@@ -0,0 +1,63 @@
1
+ require 'leafy/health'
2
+
3
+ describe Leafy::Health::Registry do
4
+
5
+ subject { Leafy::Health::Registry.new }
6
+
7
+ it 'registers and unregister check as block' do
8
+ subject.register('me') do
9
+ 'error'
10
+ end
11
+ expect(subject.names).to eq ['me']
12
+ expect(subject.health.run_health_checks.keys).to eq ['me']
13
+
14
+ subject.unregister('me')
15
+ expect(subject.names).to be_empty
16
+ end
17
+
18
+ it 'registers and unregister check as object with call method' do
19
+ subject.register('me', Proc.new {} )
20
+ expect(subject.names).to eq ['me']
21
+ expect(subject.health.run_health_checks.keys).to eq ['me']
22
+
23
+ subject.unregister('me')
24
+ expect(subject.names).to be_empty
25
+ end
26
+
27
+ it 'fails register check as object without call method' do
28
+ expect { subject.register('me', Object.new ) }.to raise_error
29
+ end
30
+
31
+ describe Leafy::Health::Registry::HealthCheck do
32
+
33
+ it 'is healthy with simple block' do
34
+ check = Leafy::Health::Registry::HealthCheck.new( Proc.new { nil } )
35
+ expect( check.check.healthy? ).to be true
36
+ expect( check.check.message ).to be nil
37
+ end
38
+
39
+ it 'is healthy' do
40
+ checker = Proc.new do |health|
41
+ health.healthy( 'ok' )
42
+ end
43
+ check = Leafy::Health::Registry::HealthCheck.new( checker )
44
+ expect( check.check.healthy? ).to be true
45
+ expect( check.check.message ).to eq 'ok'
46
+ end
47
+
48
+ it 'is unhealthy with simple block' do
49
+ check = Leafy::Health::Registry::HealthCheck.new( Proc.new { 'sick' } )
50
+ expect( check.check.healthy? ).to be false
51
+ expect( check.check.message ).to eq 'sick'
52
+ end
53
+
54
+ it 'is unhealthy' do
55
+ checker = Proc.new do |health|
56
+ health.unhealthy( 'not ok' )
57
+ end
58
+ check = Leafy::Health::Registry::HealthCheck.new( checker )
59
+ expect( check.check.healthy? ).to be false
60
+ expect( check.check.message ).to eq 'not ok'
61
+ end
62
+ end
63
+ end
data/spec/setup.rb ADDED
@@ -0,0 +1 @@
1
+ $LOAD_PATH.unshift File.expand_path( '../../lib', __FILE__ )
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leafy-health
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - christian meier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jar-dependencies
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.7
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.2'
69
+ description: provides an API to register healthchecks which uses dropwizrd-metrics-healthchecks
70
+ email:
71
+ - christian.meier@lookout.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - leafy-health.gemspec
82
+ - lib/leafy-health.rb
83
+ - lib/leafy-health_jars.rb
84
+ - lib/leafy/health.rb
85
+ - lib/leafy/health/registry.rb
86
+ - lib/leafy/health/version.rb
87
+ - spec/registry_spec.rb
88
+ - spec/setup.rb
89
+ homepage: https://github.com/lookout/leafy
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements:
108
+ - jar io.dropwizard.metrics:metrics-healthchecks, 3.1.0
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: provides an API to register healthchecks
114
+ test_files: []