rack-healthz 0.1.0

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: 8f2db9eb6e4e77c61d09a7c24c09ed1054be13a9
4
+ data.tar.gz: 47d097eda0ea43b797347f8c5c72be986a9adde3
5
+ SHA512:
6
+ metadata.gz: 6f4c748f5b46b7128b12ad568c873d9dada21b438c885ff2738d591e7c949f48d79e236c8fc0e60635885a887e0b3381703eca359d7affda37158322baecba34
7
+ data.tar.gz: ebbd397052a57252725f2edd24f094532d0871de79a476bd1d43d50aabe4c38d03f4d5c28dc422ac77374ca3d7191fbe4d5f351c7691181c8de157d5133c412b
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-healthz.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-healthz (0.1.0)
5
+ autoloaded
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ autoloaded (2.2.1)
11
+ rack (2.0.7)
12
+ rake (10.5.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 2.0)
19
+ rack
20
+ rack-healthz!
21
+ rake (~> 10.0)
22
+
23
+ BUNDLED WITH
24
+ 2.0.1
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Rack::Healthz
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rack/healthz`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rack-healthz'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rack-healthz
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rack-healthz.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/healthz"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/config.ru ADDED
@@ -0,0 +1,13 @@
1
+ require 'rack/healthz'
2
+
3
+ use Rack::Healthz
4
+
5
+ app = Proc.new do |env|
6
+ if env["PATH_INFO"] == "/foo"
7
+ ['404', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
8
+ else
9
+ ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
10
+ end
11
+ end
12
+
13
+ run app
@@ -0,0 +1,86 @@
1
+ require "rack/healthz/version"
2
+ require 'autoloaded'
3
+ require 'json'
4
+
5
+ module Rack
6
+ class Healthz
7
+ Autoloaded.module {}
8
+ class Error < StandardError;
9
+ end
10
+
11
+ def initialize(app, stats_path: "/healthz", max_time_between_requests: 3600)
12
+ @app = app
13
+ @count = 0
14
+ @my_stats = {}
15
+ @up_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
16
+ @last_success_time = nil
17
+ @stats_path = stats_path
18
+ @max_time_between_requests = max_time_between_requests
19
+ end
20
+
21
+ def call(env)
22
+ if env.fetch("PATH_INFO") == @stats_path
23
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
24
+ [return_status(now), {'Content-Type' => 'application/json'}, [body_out(now)]]
25
+ else
26
+ @count += 1
27
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
28
+ result = @app.call env
29
+ end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
30
+ elapsed_time = end_time - start_time
31
+ response_code = result.first.to_i
32
+ request_category = response_code / 100
33
+ if !(@my_stats.key? request_category)
34
+ @my_stats[request_category] = RequestAccumulator.new
35
+ end
36
+ @my_stats[request_category] << elapsed_time
37
+ if response_code < 400
38
+ @last_success_time = end_time
39
+ end
40
+ result
41
+ end
42
+ end
43
+
44
+ def return_status(now)
45
+ if healthy?(now)
46
+ 200
47
+ else
48
+ 503
49
+ end
50
+ end
51
+
52
+ def status(now)
53
+ if healthy?(now)
54
+ "healthy"
55
+ else
56
+ "unhealthy"
57
+ end
58
+ end
59
+
60
+ def healthy?(now)
61
+ !time_since_last_success(now).nil? and (time_since_last_success(now) < @max_time_between_requests)
62
+ end
63
+
64
+ def time_since_last_success(now)
65
+ @last_success_time ? now - @last_success_time : nil
66
+ end
67
+
68
+ def uptime(now)
69
+ now - @up_at
70
+ end
71
+
72
+ def body_out(now)
73
+ result = {
74
+ 'count' => @count,
75
+ 'uptime' => uptime(now),
76
+ 'time_since_last_success' => time_since_last_success(now),
77
+ 'status' => status(now), # TODO
78
+ 'stats' => {},
79
+ }
80
+ @my_stats.each_pair do |status_category, reqest_accumulator|
81
+ result['stats'][status_category.to_s + 'xx'] = reqest_accumulator.to_h
82
+ end
83
+ result.to_json
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,43 @@
1
+ module Rack
2
+ class Healthz
3
+ class RequestAccumulator
4
+
5
+ attr_reader :min_time, :max_time, :count, :std, :mean
6
+
7
+ def initialize
8
+ @count = 0
9
+ @sum_of_times = 0.0
10
+ @sum_of_square_times = 0.0
11
+ @min_time = Float::INFINITY
12
+ @max_time = -Float::INFINITY
13
+ end
14
+
15
+ def <<(elapsed_time)
16
+ @count += 1
17
+ @sum_of_times += elapsed_time
18
+ @sum_of_square_times += elapsed_time ** 2
19
+ @min_time = [@min_time, elapsed_time].min
20
+ @max_time = [@max_time, elapsed_time].max
21
+ @mean = @sum_of_times / @count
22
+ @std = if @count > 1
23
+ Math.sqrt((@sum_of_square_times / @count) - (@sum_of_times / @count) ** 2)
24
+ elsif @count == 1
25
+ 0.0
26
+ else
27
+ nil
28
+ end
29
+ self
30
+ end
31
+
32
+ def to_h
33
+ {
34
+ 'count' => count,
35
+ 'min' => min_time,
36
+ 'max' => max_time,
37
+ 'mean' => mean,
38
+ 'std' => std
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Healthz
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rack/healthz/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-healthz"
8
+ spec.version = Rack::Healthz::VERSION
9
+ spec.authors = ["Andrew Eberbach", "Jon Kwinta"]
10
+ spec.email = ["andrew@ebertech.ca", "jon@ebertech.ca"]
11
+
12
+ spec.summary = %q{A health Endpoint}
13
+ spec.description = %q{A health Endpoint}
14
+
15
+
16
+ # # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ #
21
+ # spec.metadata["homepage_uri"] = spec.homepage
22
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+ # else
25
+ # raise "RubyGems 2.0 or newer is required to protect against " \
26
+ # "public gem pushes."
27
+ # end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 2.0"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rack"
41
+
42
+ spec.add_runtime_dependency "autoloaded"
43
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-healthz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Eberbach
8
+ - Jon Kwinta
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2019-07-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rack
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: autoloaded
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: A health Endpoint
71
+ email:
72
+ - andrew@ebertech.ca
73
+ - jon@ebertech.ca
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - config.ru
86
+ - lib/rack/healthz.rb
87
+ - lib/rack/healthz/request_accumulator.rb
88
+ - lib/rack/healthz/version.rb
89
+ - rack-healthz.gemspec
90
+ homepage:
91
+ licenses: []
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
+ rubyforge_project:
109
+ rubygems_version: 2.5.2.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A health Endpoint
113
+ test_files: []