leafy-health 0.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cdc45ec69142d34dfae94a796e5a0ef9eddb4ff5
4
+ data.tar.gz: dba5e44c0c2174009e152ab2ba865b829f2f0774
5
+ SHA512:
6
+ metadata.gz: e81fc0e1c3ca060adfc4544e94e153e8c665a6fa250667f117537317fef8f62eb4fe57586289e824000f33894787969e3cd5530a8cc35511a7db2d1f336e183a
7
+ data.tar.gz: e32d2286f628db6fad6e7201ad79d7e75c2e6e34f667cfc3037abf6bba3723458b29a64833f35085e7690c408595b4d7f3e22818fef274341942027a3117e12f
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,123 @@
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 'leafy-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 < Leafy::Health::HealthCheck
39
+ def call
40
+ if app.crashed
41
+ 'application crashed'
42
+ end
43
+ end
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
53
+ if app.crashed
54
+ unhealthy( 'application crashed' )
55
+ else
56
+ healthy( 'application ok' )
57
+ end
58
+ end
59
+
60
+ or with a health-check object
61
+
62
+ class AppCheck < Leafy::Health::HealthCheck
63
+ def call
64
+ if app.crashed
65
+ unhealthy( 'application crashed' )
66
+ else
67
+ healthy( 'application ok' )
68
+ end
69
+ end
70
+ end
71
+ registry.register( 'app.class', AppCheck.new )
72
+
73
+ ### health checks with structural data as message
74
+
75
+ registry.register( 'app.block') do
76
+ if app.crashed
77
+ unhealthy( :host => 'localhost', :msg => 'not good' )
78
+ else
79
+ healthy( :host => 'localhost', :msg => 'application ok' )
80
+ end
81
+ end
82
+
83
+ or as health-check object
84
+
85
+ class AppCheck < Leafy::Health::HealthCheck
86
+ def call
87
+ if app.crashed
88
+ unhealthy( :host => 'localhost', :msg => 'application crashed' )
89
+ else
90
+ healthy( :host => 'localhost', :msg => 'application ok' )
91
+ end
92
+ end
93
+ end
94
+ registry.register( 'app.class', AppCheck.new )
95
+
96
+ ### unregister health checks
97
+
98
+ registry.unregister( 'app.class' )
99
+
100
+ ### builtin ThreadDeadlockHeathCheck
101
+
102
+ registry.register( 'app.deadlock', Leafy::Health::ThreadDeadlockHeathCheck.new )
103
+
104
+ ### note
105
+
106
+ 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**
107
+
108
+ Leafy::Json::HealthWriter.to_json( registry.health )
109
+
110
+ ## developement
111
+
112
+ get all the gems and jars in place
113
+
114
+ gem install jar-dependencies --development
115
+ bundle install
116
+
117
+ for running all specs
118
+
119
+ rake
120
+
121
+ or
122
+
123
+ 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,28 @@
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.platform = 'java'
12
+ s.license = 'MIT'
13
+ s.summary = %q(provides an API to register healthchecks)
14
+ s.homepage = 'https://github.com/lookout/leafy'
15
+ s.description = %q(provides an API to register healthchecks which uses dropwizrd-metrics-healthchecks)
16
+
17
+ s.files = `git ls-files`.split($/)
18
+
19
+ s.requirements << 'jar io.dropwizard.metrics:metrics-healthchecks, 3.1.0'
20
+ s.requirements << 'jar io.dropwizard.metrics:metrics-jvm, 3.1.0'
21
+
22
+ s.add_runtime_dependency 'jar-dependencies', '~> 0.1.8'
23
+ s.add_development_dependency 'rspec', '~> 3.1'
24
+ s.add_development_dependency 'yard', '~> 0.8.7'
25
+ s.add_development_dependency 'rake', '~> 10.2'
26
+ end
27
+
28
+ # vim: syntax=Ruby
@@ -0,0 +1,2 @@
1
+ require 'leafy-health_jars'
2
+ require 'leafy/health/registry'
@@ -0,0 +1,7 @@
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' )
6
+ require_jar( 'io.dropwizard.metrics', 'metrics-jvm', '3.1.0' )
7
+ require_jar( 'io.dropwizard.metrics', 'metrics-core', '3.1.0' )
@@ -0,0 +1,2 @@
1
+ require 'leafy-health_jars'
2
+ require 'leafy/health/registry'
@@ -0,0 +1,72 @@
1
+ require 'leafy/health'
2
+
3
+ # add json serialization definition
4
+ class com.codahale.metrics.health.HealthCheck::Result
5
+
6
+ attr_writer :data
7
+
8
+ def to_json( *args )
9
+ { :healthy => healthy?, :message => @data || message }.to_json( *args )
10
+ end
11
+ end
12
+
13
+ module Leafy
14
+ module Health
15
+ ThreadDeadlockHealthCheck = com.codahale.metrics.health.jvm.ThreadDeadlockHealthCheck
16
+ class HealthCheck < com.codahale.metrics.health.HealthCheck
17
+
18
+ def initialize( &block )
19
+ @block = block if block
20
+ end
21
+
22
+ # create healthy result object with given message
23
+ #
24
+ # param [String] optional result message, can be nil
25
+ # return [com.codahale.metrics.health.HealthCheck::Result]
26
+ def healthy( result = nil )
27
+ if result.is_a? Hash
28
+ r = com.codahale.metrics.health.HealthCheck::Result.healthy( result.to_json )
29
+ r.data = result
30
+ r
31
+ else
32
+ com.codahale.metrics.health.HealthCheck::Result.healthy( result )
33
+ end
34
+ end
35
+
36
+ # create unhealthy result object with given message
37
+ #
38
+ # param [String] result message
39
+ # return [com.codahale.metrics.health.HealthCheck::Result]
40
+ def unhealthy( result )
41
+ if result.is_a? Hash
42
+ r = com.codahale.metrics.health.HealthCheck::Result.unhealthy( result.to_json )
43
+ r.data = result
44
+ r
45
+ else
46
+ com.codahale.metrics.health.HealthCheck::Result.unhealthy( result )
47
+ end
48
+ end
49
+
50
+ def check
51
+ case result = call
52
+ when String
53
+ unhealthy( result )
54
+ when NilClass
55
+ healthy
56
+ when com.codahale.metrics.health.HealthCheck::Result
57
+ result
58
+ else
59
+ raise 'wrong result type'
60
+ end
61
+ end
62
+
63
+ def call
64
+ if @block
65
+ instance_eval( &@block )
66
+ else
67
+ 'health check "call" method not implemented'
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,55 @@
1
+ require 'leafy/health'
2
+ require 'leafy/health/health_check'
3
+ module Leafy
4
+ module Health
5
+ class Registry
6
+
7
+ # state ofthe registry
8
+ attr_reader :health
9
+
10
+ def initialize
11
+ @health = com.codahale.metrics.health.HealthCheckRegistry.new
12
+ end
13
+
14
+ # register a HealthCheck under a given name
15
+ #
16
+ # @param [String] name
17
+ # @param [String] instead of block any check object which responds to 'call'
18
+ # @yieldparam [HealthCheckRegistry::HealthCheck] which has convienient methods to create healthy and unhealthy results with message
19
+ # @yieldreturn [String] if the healthcheck fails return the message
20
+ # @yieldreturn [NilClass] if the healthcheck succeeds
21
+ # @yieldreturn [com.codahale.metrics.health.HealthCheck::Result] if the check produces its own result object
22
+ def register(name, check = nil, &block )
23
+ if check and not block_given? and check.is_a? com.codahale.metrics.health.HealthCheck
24
+ @health.register( name, check )
25
+ elsif check.nil? and block_given?
26
+ @health.register( name, HealthCheck.new( &block ) )
27
+ else
28
+ raise 'needs either a block and object with call method'
29
+ end
30
+ end
31
+
32
+ # unregister a HealthCheck for a given name
33
+ #
34
+ # @param [String] name
35
+ def unregister(name)
36
+ @health.unregister(name)
37
+ end
38
+
39
+ # the names of all registered HealthCheck
40
+ #
41
+ # @return [Array<String>] names of HealthCheck in order of their registration
42
+ def names
43
+ @health.names.to_a
44
+ end
45
+
46
+ # # run a healthcheck for a given name
47
+ # #
48
+ # # @param [String] name
49
+ # # @return [Java::ComCodahaleMetricsHealth::HealthCheck::Result] result of the health-check
50
+ # def run_health_check(name)
51
+ # @health.runHealthCheck(name)
52
+ # end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,6 @@
1
+ module Leafy
2
+ module Health
3
+ VERSION = '0.4.0'.freeze
4
+ end
5
+ end
6
+
@@ -0,0 +1,164 @@
1
+ require 'leafy/health'
2
+ require 'json'
3
+
4
+ describe Leafy::Health::Registry do
5
+
6
+ subject { Leafy::Health::Registry.new }
7
+
8
+ it 'has thread-deadlock-health-check' do
9
+ subject.register( 'app', Leafy::Health::ThreadDeadlockHealthCheck.new )
10
+
11
+ expect(subject.names).to eq ['app']
12
+ end
13
+
14
+ it 'registers and unregister check as block' do
15
+ subject.register('me') do
16
+ 'error'
17
+ end
18
+ expect(subject.names).to eq ['me']
19
+
20
+ results = subject.health.run_health_checks
21
+ expect(results.keys).to eq ['me']
22
+
23
+ first = results.values.to_array.first
24
+ expect(first.message).to eq 'error'
25
+ expect(first.healthy?).to eq false
26
+
27
+ subject.unregister('me')
28
+ expect(subject.names).to be_empty
29
+ end
30
+
31
+ it 'registers and unregister check as block using dsl' do
32
+ subject.register('me') do
33
+ healthy 'ok'
34
+ end
35
+ expect(subject.names).to eq ['me']
36
+
37
+ results = subject.health.run_health_checks
38
+ expect(results.keys).to eq ['me']
39
+
40
+ first = results.values.to_array.first
41
+ expect(first.message).to eq 'ok'
42
+ expect(first.healthy?).to eq true
43
+
44
+ subject.unregister('me')
45
+ expect(subject.names).to be_empty
46
+ end
47
+
48
+ it 'registers and unregister check as HealthCheck with block' do
49
+ subject.register('me', Leafy::Health::HealthCheck.new {} )
50
+
51
+ expect(subject.names).to eq ['me']
52
+
53
+ results = subject.health.run_health_checks
54
+ expect(results.keys).to eq ['me']
55
+
56
+ first = results.values.to_array.first
57
+ expect(first.message).to be_nil
58
+ expect(first.healthy?).to eq true
59
+
60
+ subject.unregister('me')
61
+ expect(subject.names).to be_empty
62
+ end
63
+
64
+ it 'registers and unregister check as HealthCheck without implementing call' do
65
+ subject.register('me', Leafy::Health::HealthCheck.new )
66
+
67
+ expect(subject.names).to eq ['me']
68
+
69
+ results = subject.health.run_health_checks
70
+ expect(results.keys).to eq ['me']
71
+
72
+ first = results.values.to_array.first
73
+ expect(first.message).to eq 'health check "call" method not implemented'
74
+ expect(first.healthy?).to eq false
75
+
76
+ subject.unregister('me')
77
+ expect(subject.names).to be_empty
78
+ end
79
+
80
+ it 'fails register check as wrong object' do
81
+ expect { subject.register('me', Object.new ) }.to raise_error
82
+ end
83
+
84
+ describe Leafy::Health::HealthCheck do
85
+
86
+ it 'is healthy default with simple block' do
87
+ check = Leafy::Health::HealthCheck.new do
88
+ nil
89
+ end
90
+ expect( check.check.healthy? ).to be true
91
+ expect( check.check.message ).to be nil
92
+ expect( check.check.to_json ).to eq "{\"healthy\":true,\"message\":null}"
93
+ end
94
+
95
+ it 'is healthy with simple block' do
96
+ check = Leafy::Health::HealthCheck.new do
97
+ healthy 'happy'
98
+ end
99
+ expect( check.check.healthy? ).to be true
100
+ expect( check.check.message ).to eq 'happy'
101
+ expect( check.check.to_json ).to eq "{\"healthy\":true,\"message\":\"happy\"}"
102
+ end
103
+
104
+ it 'is healthy with structural message' do
105
+ check = Leafy::Health::HealthCheck.new do
106
+ healthy :msg => 'happy', :date => '11-11-2011'
107
+ end
108
+ expect( check.check.healthy? ).to be true
109
+ expect( check.check.to_json ).to eq "{\"healthy\":true,\"message\":{\"msg\":\"happy\",\"date\":\"11-11-2011\"}}"
110
+ end
111
+
112
+ it 'is healthy default' do
113
+ check = Leafy::Health::HealthCheck.new
114
+ def check.call; nil; end
115
+ expect( check.check.healthy? ).to be true
116
+ expect( check.check.message ).to eq nil
117
+ end
118
+
119
+ it 'is healthy' do
120
+ check = Leafy::Health::HealthCheck.new
121
+ def check.call; healthy( 'ok' ); end
122
+ expect( check.check.healthy? ).to be true
123
+ expect( check.check.message ).to eq 'ok'
124
+ end
125
+
126
+ it 'is unhealthy default with simple block' do
127
+ check = Leafy::Health::HealthCheck.new do
128
+ 'sick'
129
+ end
130
+ expect( check.check.healthy? ).to be false
131
+ expect( check.check.message ).to eq 'sick'
132
+ end
133
+
134
+ it 'is unhealthy with simple block' do
135
+ check = Leafy::Health::HealthCheck.new do
136
+ unhealthy 'really sick'
137
+ end
138
+ expect( check.check.healthy? ).to be false
139
+ expect( check.check.message ).to eq 'really sick'
140
+ end
141
+
142
+ it 'is unhealthy with structural message' do
143
+ check = Leafy::Health::HealthCheck.new do
144
+ unhealthy :msg => 'oh je', :date => '11-11-2011'
145
+ end
146
+ expect( check.check.healthy? ).to be false
147
+ expect( check.check.to_json ).to eq "{\"healthy\":false,\"message\":{\"msg\":\"oh je\",\"date\":\"11-11-2011\"}}"
148
+ end
149
+
150
+ it 'is unhealthy default' do
151
+ check = Leafy::Health::HealthCheck.new
152
+ def check.call; 'not ok'; end
153
+ expect( check.check.healthy? ).to be false
154
+ expect( check.check.message ).to eq 'not ok'
155
+ end
156
+
157
+ it 'is unhealthy' do
158
+ check = Leafy::Health::HealthCheck.new
159
+ def check.call; unhealthy( 'not ok' ); end
160
+ expect( check.check.healthy? ).to be false
161
+ expect( check.check.message ).to eq 'not ok'
162
+ end
163
+ end
164
+ end
data/spec/setup.rb ADDED
@@ -0,0 +1 @@
1
+ $LOAD_PATH.unshift File.expand_path( '../../lib', __FILE__ )
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leafy-health
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: java
6
+ authors:
7
+ - christian meier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: 0.1.8
19
+ name: jar-dependencies
20
+ prerelease: false
21
+ type: :runtime
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
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.1'
33
+ name: rspec
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.8.7
47
+ name: yard
48
+ prerelease: false
49
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '10.2'
61
+ name: rake
62
+ prerelease: false
63
+ type: :development
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/health_check.rb
86
+ - lib/leafy/health/registry.rb
87
+ - lib/leafy/health/version.rb
88
+ - spec/registry_spec.rb
89
+ - spec/setup.rb
90
+ homepage: https://github.com/lookout/leafy
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements:
109
+ - jar io.dropwizard.metrics:metrics-healthchecks, 3.1.0
110
+ - jar io.dropwizard.metrics:metrics-jvm, 3.1.0
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: provides an API to register healthchecks
116
+ test_files: []
117
+ has_rdoc: