goodguide-health 1.1.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0716629f003fdcec5f0910a175f16dd9ae662bd4
4
+ data.tar.gz: 560e32471355d373a7474acc50f8b4f88216e12a
5
+ SHA512:
6
+ metadata.gz: b7f959dafdd143d1658e3ec17de56adf8702aee09bd2bb0d70b4861b735a9d2323ca7b6a366dd6654f5df8ad22fe8155ba56b1a866c1e73384c77ae97129607b
7
+ data.tar.gz: 35d470db29e4dc9b6deb1fdde865bac0ed2baf278a8029c7256c34f6680c6e25ce42b4d834da1492cda7b2606ba5e631420b6dea1863a947b259abf7f02f8911
@@ -0,0 +1,3 @@
1
+ .bundle/
2
+ pkg/
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 GoodGuide
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.
@@ -0,0 +1,49 @@
1
+ # Goodguide::Health
2
+
3
+ This is a very simple Rack application which provides GoodGuide's internal notion of a health-check endpoint to apps which embed it.
4
+
5
+ ## Configuration
6
+
7
+ Goodguide::Health is mainly a wrapper around our fork of [Pinglish][], which is a simple Rack app which provides a "ping" endpoint which has a few powerful guarantees and enables the creation of arbitrary "health check" procs each of which are run during a status check.
8
+
9
+ This is exposed for configuration as in the following example:
10
+
11
+ ```ruby
12
+ # config/initializers/health.rb
13
+
14
+ Goodguide::Health.configure do |health|
15
+ health.check :foo do
16
+ # health check logic
17
+ end
18
+ end
19
+ ```
20
+
21
+ Please see the Pinglish docs/source for more info on exactly the semantics of the health checks and how they are meant to be used.
22
+
23
+ Goodguide::Health is a singleton class, which makes configuration easy when you usually will have to inject the configured instance into a Rails routes table earlier in the boot process.
24
+
25
+ ## Usage
26
+
27
+ In the simple case, you can just `mount` it in a Rails app:
28
+
29
+ ```ruby
30
+ # config/routes.rb
31
+ MyAwesomeRailsApp::Application.routes.draw do
32
+ mount Goodguide::Health.app, at: '/health', as: 'health'
33
+ end
34
+ ```
35
+
36
+ (`#app` exposes the composed Rack app which mounts [Pinglish][] at `/status`)
37
+
38
+ Or in a more basic Rack context, you can `run` it directly:
39
+
40
+ ```ruby
41
+ # config.ru
42
+ map '/health' do
43
+ run Goodguide::Health.app
44
+ end
45
+
46
+ run MyAwesomeRackApp
47
+ ```
48
+
49
+ [Pinglish]: https://github.com/goodguide/pinglish
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,9 @@
1
+ require 'goodguide/health'
2
+
3
+ Goodguide::Health.configure do |health|
4
+ health.check :foo do
5
+ true
6
+ end
7
+ end
8
+
9
+ run Goodguide::Health.app
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'goodguide/health/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'goodguide-health'
8
+ spec.version = Goodguide::Health::VERSION
9
+ spec.authors = ['Ryan Taylor Long']
10
+ spec.email = ['ryan.long@goodguide.com']
11
+ spec.summary = 'application health summary check/endpoint'
12
+ spec.homepage = ''
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^test/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'goodguide-pinglish', '~> 1.0'
21
+ spec.add_dependency 'rack', '>= 1.3'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'minitest'
26
+ spec.add_development_dependency 'rack-test'
27
+ spec.add_development_dependency 'timecop'
28
+ end
@@ -0,0 +1,98 @@
1
+ require 'time'
2
+ require 'forwardable'
3
+ require 'rack/builder'
4
+ require 'rack/urlmap'
5
+ require 'pinglish'
6
+ require 'goodguide/health/version'
7
+ require 'goodguide/health/newline_middleware'
8
+
9
+ module Goodguide
10
+ class Health
11
+ class <<self
12
+ extend Forwardable
13
+
14
+ def app
15
+ health = instance
16
+ Rack::Builder.app do
17
+ use NewlineMiddleware
18
+
19
+ map '/status' do
20
+ run health.pinglish
21
+ end
22
+ map '/' do
23
+ run health
24
+ end
25
+ end
26
+ end
27
+
28
+ def reset
29
+ @instance = nil
30
+ end
31
+
32
+ def_delegator :instance, :configure
33
+
34
+ private
35
+
36
+ def instance
37
+ @instance ||= new
38
+ end
39
+ end
40
+
41
+ def initialize
42
+ @booted_at = Time.now.utc
43
+ end
44
+
45
+ attr_reader :booted_at
46
+ attr_writer :revision, :deployed_at, :hostname, :gg_env
47
+
48
+ def hostname
49
+ @hostname ||= ENV.fetch('DEPLOYMENT_HOST') {
50
+ ENV.fetch('HOSTNAME') {
51
+ output = `hostname 2>/dev/null`
52
+ $? == 0 ? output.chomp : nil
53
+ }
54
+ }
55
+ end
56
+
57
+ def revision
58
+ @revision ||= ENV.fetch('GIT_REVISION') {
59
+ ENV.fetch('DEPLOYMENT_REVISION') {
60
+ output = `git rev-parse HEAD 2>/dev/null`
61
+ $? == 0 ? output.chomp : nil
62
+ }
63
+ }
64
+ end
65
+
66
+ def deployed_at
67
+ @deployed_at ||= ENV.key?('DEPLOYMENT_TIMESTAMP') ?
68
+ Time.at(ENV['DEPLOYMENT_TIMESTAMP'].to_i).utc :
69
+ booted_at
70
+ end
71
+
72
+ def gg_env
73
+ @gg_env ||= ENV['GG_ENV']
74
+ end
75
+
76
+ def call(env)
77
+ [200, { 'Content-Type' => 'text/plain' }, ["OK\n"]]
78
+ end
79
+
80
+ def configure(&block)
81
+ block.call(self)
82
+ end
83
+
84
+ def pinglish
85
+ @pinglish ||= Pinglish.new do |ping|
86
+ ping.check(:host) { hostname }
87
+ ping.check(:deployed_at) { deployed_at.iso8601 }
88
+ ping.check(:revision) { revision }
89
+ ping.check(:started_at) { booted_at.iso8601 }
90
+ ping.check(:gg_env) { gg_env }
91
+ end
92
+ end
93
+
94
+ def check(*args, &block)
95
+ pinglish.check(*args, &block)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,19 @@
1
+ module Goodguide
2
+ class Health
3
+ class NewlineMiddleware
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ status, headers, body = @app.call(env)
10
+
11
+ unless body.last.end_with?("\n")
12
+ body.push("\n")
13
+ end
14
+
15
+ [status, headers, body]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Goodguide
2
+ class Health
3
+ VERSION = "1.1.2"
4
+ end
5
+ end
@@ -0,0 +1,168 @@
1
+ require 'test_helper'
2
+ require 'timecop'
3
+
4
+ class Goodguide::HealthTest < Minitest::Test
5
+ include Rack::Test::Methods
6
+
7
+ def app_boot_time
8
+ Time.at(1430161200)
9
+ end
10
+
11
+ attr_reader :app
12
+
13
+ def setup
14
+ Goodguide::Health.reset
15
+ Timecop.freeze(app_boot_time) do
16
+ # force initialization of the instance with time frozen
17
+ @app = Goodguide::Health.app
18
+ end
19
+ Timecop.freeze(Time.now)
20
+ end
21
+
22
+ def teardown
23
+ Timecop.return
24
+ end
25
+
26
+ def test_root_path
27
+ get '/'
28
+
29
+ assert last_response.ok?
30
+ assert_equal "OK\n", last_response.body
31
+ end
32
+
33
+ def test_status_default
34
+ get '/status'
35
+
36
+ assert last_response.ok?
37
+ assert_status_response_matches parsed_response, default_status_response
38
+ end
39
+
40
+ def test_status_termination
41
+ get '/status'
42
+
43
+ assert last_response.ok?
44
+ assert last_response.body.end_with?("\n"), 'Output should end with an ASCII newline char'
45
+ end
46
+
47
+ def test_status_with_deployment_info
48
+ timestamp = 1430162520
49
+ Goodguide::Health.configure do |health|
50
+ health.revision = 'foo'
51
+ health.deployed_at = Time.at(timestamp).utc
52
+ end
53
+ get '/status'
54
+
55
+ assert last_response.ok?
56
+ assert_status_response_matches parsed_response, default_status_response.merge(
57
+ deployed_at: Time.at(timestamp).utc.iso8601,
58
+ revision: 'foo'
59
+ )
60
+ end
61
+
62
+ def test_status_with_gg_env
63
+ Goodguide::Health.configure do |health|
64
+ health.gg_env = 'production'
65
+ end
66
+ get '/status'
67
+
68
+ assert last_response.ok?
69
+ assert_status_response_matches parsed_response, default_status_response.merge(gg_env: 'production')
70
+ end
71
+
72
+ def test_status_with_custom_checks
73
+ Goodguide::Health.configure do |health|
74
+ health.check :custom_check do
75
+ :foobarbaz
76
+ end
77
+ end
78
+ get '/status'
79
+
80
+ assert last_response.ok?
81
+ assert_status_response_matches parsed_response, default_status_response.merge(
82
+ custom_check: 'foobarbaz'
83
+ )
84
+ end
85
+
86
+ def test_status_with_custom_checks_which_fail
87
+ Goodguide::Health.configure do |health|
88
+ health.check :custom_check_that_fails do
89
+ raise "foo"
90
+ end
91
+ end
92
+ get '/status'
93
+
94
+ assert_equal 503, last_response.status
95
+ assert_status_response_matches parsed_response, default_status_response.merge(
96
+ status: 'failures',
97
+ failures: ['custom_check_that_fails'],
98
+ custom_check_that_fails: {
99
+ 'state' => 'error',
100
+ 'exception' => 'RuntimeError',
101
+ 'message' => 'foo',
102
+ }
103
+ )
104
+ end
105
+
106
+ def test_status_with_custom_checks_which_timeout
107
+ Goodguide::Health.configure do |health|
108
+ health.check :custom_check_that_times_out, timeout: 0.01 do
109
+ sleep 1
110
+ end
111
+ end
112
+ get '/status'
113
+
114
+ assert_equal 503, last_response.status
115
+ assert_status_response_matches parsed_response, default_status_response.merge(
116
+ status: 'failures',
117
+ timeouts: ['custom_check_that_times_out'],
118
+ )
119
+ end
120
+
121
+ def test_status_with_subset_of_checks
122
+ Goodguide::Health.configure do |health|
123
+ health.check :custom_check_that_times_out, timeout: 0.01 do
124
+ sleep 1
125
+ end
126
+
127
+ health.check :foobar do
128
+ 123
129
+ end
130
+ end
131
+ get '/status?checks=foobar'
132
+
133
+ assert_equal 200, last_response.status
134
+ assert_status_response_matches parsed_response, {
135
+ status: 'ok',
136
+ now: Time.now.to_i,
137
+ foobar: 123,
138
+ }
139
+ end
140
+
141
+ private
142
+
143
+ def git_revision
144
+ `git rev-parse HEAD`.chomp
145
+ end
146
+
147
+ def parsed_response
148
+ JSON.parse(last_response.body)
149
+ end
150
+
151
+ def default_status_response
152
+ {
153
+ status: 'ok',
154
+ now: Time.now.utc.to_i,
155
+ deployed_at: app_boot_time.utc.iso8601,
156
+ started_at: app_boot_time.utc.iso8601,
157
+ host: `hostname`.chomp,
158
+ revision: git_revision,
159
+ }
160
+ end
161
+
162
+ def assert_status_response_matches(actual, expected)
163
+ expected.each do |key, value|
164
+ assert_equal value, actual[key.to_s], "value for `#{key}` did not match"
165
+ end
166
+ assert_equal expected.keys.map(&:to_s).sort, actual.keys.sort, "keys did not match"
167
+ end
168
+ end
@@ -0,0 +1,6 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'minitest/autorun'
4
+ require 'rack/test'
5
+
6
+ require 'goodguide/health'
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goodguide-health
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Taylor Long
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: goodguide-pinglish
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.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.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
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: rack-test
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: timecop
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
+ description:
112
+ email:
113
+ - ryan.long@goodguide.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - config.ru
124
+ - goodguide-health.gemspec
125
+ - lib/goodguide/health.rb
126
+ - lib/goodguide/health/newline_middleware.rb
127
+ - lib/goodguide/health/version.rb
128
+ - test/goodguide/health_test.rb
129
+ - test/test_helper.rb
130
+ homepage: ''
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.6.6
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: application health summary check/endpoint
154
+ test_files:
155
+ - test/goodguide/health_test.rb
156
+ - test/test_helper.rb