kubernetes-health 3.1.0 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -19
- data/kubernetes-health.gemspec +3 -3
- data/lib/kubernetes/health/config.rb +21 -0
- data/lib/kubernetes/health/rack_on_migrate.rb +1 -1
- data/lib/kubernetes/health/version.rb +1 -1
- data/lib/puma/kubernetes/app.rb +40 -19
- data/lib/puma/kubernetes/parser.rb +1 -0
- metadata +18 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b50f5907432e2efafb49e0541ff792e1bfb086e0b5ced6f2c21308a20cf5e1e0
|
4
|
+
data.tar.gz: 2ca4a7a600c71caf1704272c95bbb7d2c354d0bdd32e1eee778f10b39043ea90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d633dc94b6e18c839cdb0658efc6fbcb3d924b73dc8fb23a14bdb4b6babcdd856d5ee6f0eccd2cd4bae24272b97678d5fab4fee4154493c1a11a196a37532a57
|
7
|
+
data.tar.gz: c49e6472031a756436d3f17d6c3b1f2a736c3dcbdd1c63c13c0f980e200eff31d217bb44d48f8901eaecc90d75daef74c9a2261c0816e245977afa2755dd4c64
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# Kubernetes::Health
|
2
2
|
|
3
|
-
This gem allows kubernetes monitoring your app while it is running migrates and after it
|
3
|
+
This gem allows kubernetes monitoring your app while it is running migrates and after it starts.
|
4
4
|
|
5
5
|
# Features
|
6
6
|
- add routes `/_readiness`, `/_liveness` on rails stack.
|
7
7
|
- add routes `/_readiness`, `/_liveness` and `/_metrics` as a puma plugin.
|
8
|
-
- metrics are prometheus compatible (code copied from `puma-metrics` gem).
|
8
|
+
- metrics are prometheus compatible (code copied from `puma-metrics` gem) or json.
|
9
9
|
- allow custom checks for `/_readiness` and `/_liveness`.
|
10
10
|
- add routes `/_readiness` and `/_liveness` while `rake db:migrate` runs. (optional)
|
11
11
|
- add support to avoid parallel running of `rake db:migrate` while keep kubernetes waiting. (optional)
|
@@ -15,7 +15,7 @@ This gem allows kubernetes monitoring your app while it is running migrates and
|
|
15
15
|
Add this line to your application's Gemfile:
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
gem 'kubernetes-health', '~> 3.
|
18
|
+
gem 'kubernetes-health', '~> 3.4'
|
19
19
|
```
|
20
20
|
|
21
21
|
## Enabling puma plugin
|
@@ -48,18 +48,6 @@ In Kubernetes you need to configure your deployment `readinessProbe` and `livene
|
|
48
48
|
```
|
49
49
|
|
50
50
|
Setting `failureThreshold` is import to avoid problems when app finish migrates and is starting the web process.
|
51
|
-
|
52
|
-
## Enabling prometheus metrics
|
53
|
-
|
54
|
-
```
|
55
|
-
template:
|
56
|
-
metadata:
|
57
|
-
annotations:
|
58
|
-
prometheus.io/path: '/_metrics'
|
59
|
-
prometheus.io/port: '9393'
|
60
|
-
prometheus.io/scrape: 'true'
|
61
|
-
```
|
62
|
-
|
63
51
|
## Enabling monitoring while `rake db:migrate` runs
|
64
52
|
|
65
53
|
Your Dockerfile's entry script needs to run migrates before start your web app.
|
@@ -106,18 +94,19 @@ Kubernetes::Health::Config.unlock = lambda {
|
|
106
94
|
|
107
95
|
It only works for routes in rails stack, they are not executed while `rake db:migrate` runs.
|
108
96
|
|
109
|
-
I prefer do nothing else on `liveness` to avoid unnecessary `CrashLoopBackOff` status. `params` is optional.
|
97
|
+
I prefer do nothing else on `liveness` to avoid unnecessary `CrashLoopBackOff` status. `params` is optional (request params).
|
110
98
|
|
111
99
|
```
|
112
|
-
Kubernetes::Health::Config.live_if = lambda {
|
100
|
+
Kubernetes::Health::Config.live_if = lambda { |params|
|
113
101
|
true
|
114
102
|
}
|
115
103
|
|
116
104
|
```
|
117
|
-
Ex. Check if PostgreSQL is reachable on `readiness
|
105
|
+
Ex. Check if PostgreSQL is reachable on `readiness` indicating that credentials are setup right and keeps cache to avoid doing it a lot. `params` is optional (request params).
|
118
106
|
```
|
119
107
|
Kubernetes::Health::Config.ready_if = lambda { |params|
|
120
|
-
|
108
|
+
return $kubernetes_health_test_db_connection if $kubernetes_health_test_db_connection
|
109
|
+
$kubernetes_health_test_db_connection = ActiveRecord::Base.connection.execute("SELECT 1").cmd_tuples == 1
|
121
110
|
}
|
122
111
|
```
|
123
112
|
|
@@ -127,3 +116,19 @@ Kubernetes::Health::Config.route_liveness = '/liveness'
|
|
127
116
|
Kubernetes::Health::Config.route_readiness = '/readiness'
|
128
117
|
Kubernetes::Health::Config.route_metrics = '/metrics'
|
129
118
|
```
|
119
|
+
|
120
|
+
## Response format
|
121
|
+
If you are using `https://github.com/zalando-incubator/kube-metrics-adapter` you will want to use `json` format.
|
122
|
+
|
123
|
+
Default is `prometheus`.
|
124
|
+
```
|
125
|
+
Kubernetes::Health::Config.response_format = 'json'
|
126
|
+
```
|
127
|
+
|
128
|
+
## Customizing requests logs
|
129
|
+
|
130
|
+
```
|
131
|
+
Kubernetes::Health::Config.request_log_callback = lambda { |req, http_code|
|
132
|
+
Rails.logger.debug "Kubernetes Health: Rack on Migrate - Request: Path: #{req.path_info} / Params: #{req.params} / HTTP Code: #{http_code}" rescue nil
|
133
|
+
}
|
134
|
+
```
|
data/kubernetes-health.gemspec
CHANGED
@@ -26,9 +26,9 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
27
|
spec.require_paths = ["lib"]
|
28
28
|
|
29
|
-
spec.add_development_dependency "bundler"
|
30
|
-
spec.add_development_dependency "rake"
|
31
|
-
spec.add_development_dependency "rspec"
|
29
|
+
spec.add_development_dependency "bundler"
|
30
|
+
spec.add_development_dependency "rake"
|
31
|
+
spec.add_development_dependency "rspec"
|
32
32
|
spec.add_dependency "rack"
|
33
33
|
spec.add_dependency "rails"
|
34
34
|
spec.add_runtime_dependency 'prometheus-client', '~> 0.9.0'
|
@@ -8,6 +8,12 @@ module Kubernetes
|
|
8
8
|
@@route_liveness = '/_liveness'
|
9
9
|
@@route_readiness = '/_readiness'
|
10
10
|
@@route_metrics = '/_metrics'
|
11
|
+
@@response_format = 'prometheus'
|
12
|
+
|
13
|
+
@@request_log_callback = lambda { |req, http_code, content|
|
14
|
+
Rails.logger.debug "Kubernetes Health: Rack on Migrate - Request: Path: #{req.path_info} / Params: #{req.params} / HTTP Code: #{http_code}\n#{content}" rescue nil
|
15
|
+
}
|
16
|
+
|
11
17
|
@@lock_or_wait = lambda { ActiveRecord::Base.connection.execute 'select pg_advisory_lock(123456789123456789);' }
|
12
18
|
@@unlock = lambda { ActiveRecord::Base.connection.execute 'select pg_advisory_unlock(123456789123456789);' }
|
13
19
|
|
@@ -19,6 +25,14 @@ module Kubernetes
|
|
19
25
|
@@lock_or_wait = value
|
20
26
|
end
|
21
27
|
|
28
|
+
def self.request_log_callback
|
29
|
+
@@request_log_callback
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.request_log_callback=(value)
|
33
|
+
@@request_log_callback = value
|
34
|
+
end
|
35
|
+
|
22
36
|
def self.unlock
|
23
37
|
@@unlock
|
24
38
|
end
|
@@ -83,6 +97,13 @@ module Kubernetes
|
|
83
97
|
@@ready_if = value
|
84
98
|
end
|
85
99
|
|
100
|
+
def self.response_format
|
101
|
+
@@response_format
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.response_format=(value)
|
105
|
+
@@response_format = value
|
106
|
+
end
|
86
107
|
end
|
87
108
|
end
|
88
109
|
end
|
@@ -11,7 +11,7 @@ module Kubernetes
|
|
11
11
|
else
|
12
12
|
http_code = 404
|
13
13
|
end
|
14
|
-
|
14
|
+
::Kubernetes::Health::Config.request_log_callback.call(req, http_code)
|
15
15
|
[http_code, {}, []]
|
16
16
|
end
|
17
17
|
end
|
data/lib/puma/kubernetes/app.rb
CHANGED
@@ -13,26 +13,47 @@ module Puma
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def call(_env)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
16
|
+
begin
|
17
|
+
req = ::Rack::Request.new(_env)
|
18
|
+
type = {}
|
19
|
+
content = []
|
20
|
+
type = ::Kubernetes::Health::Config.response_format == 'json' ? { 'Content-Type' => 'application/json' } : { 'Content-Type' => 'text/plain' }
|
21
|
+
case req.path_info
|
22
|
+
when ::Kubernetes::Health::Config.route_liveness
|
23
|
+
i_am_live = ::Kubernetes::Health::Config.live_if.arity == 0 ? ::Kubernetes::Health::Config.live_if.call : ::Kubernetes::Health::Config.live_if.call(req.params)
|
24
|
+
http_code = i_am_live ? 200 : 503
|
25
|
+
when ::Kubernetes::Health::Config.route_readiness
|
26
|
+
i_am_ready = ::Kubernetes::Health::Config.ready_if.arity == 0 ? ::Kubernetes::Health::Config.ready_if.call : ::Kubernetes::Health::Config.ready_if.call(req.params)
|
27
|
+
http_code = i_am_ready ? 200 : 503
|
28
|
+
when ::Kubernetes::Health::Config.route_metrics
|
29
|
+
http_code = 200
|
30
|
+
if ::Kubernetes::Health::Config.response_format == 'json'
|
31
|
+
content = include_puma_key_prefix(include_usage(JSON.parse(@launcher.stats))).to_json
|
32
|
+
else
|
33
|
+
@parser.parse include_usage(JSON.parse(@launcher.stats))
|
34
|
+
content = Prometheus::Client::Formats::Text.marshal(Prometheus::Client.registry)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
http_code = 404
|
38
|
+
end
|
39
|
+
rescue
|
40
|
+
http_code = 500
|
41
|
+
content = []
|
33
42
|
end
|
34
|
-
|
35
|
-
[http_code, type, content]
|
43
|
+
::Kubernetes::Health::Config.request_log_callback.call(req, http_code, content)
|
44
|
+
[http_code, type, [content]]
|
45
|
+
end
|
46
|
+
|
47
|
+
def include_usage(stats)
|
48
|
+
stats['usage'] = (1 - stats['pool_capacity'].to_f / stats['max_threads']).round(2)
|
49
|
+
stats
|
50
|
+
end
|
51
|
+
def include_puma_key_prefix(stats)
|
52
|
+
result = {}
|
53
|
+
stats.each do |k,v|
|
54
|
+
result["puma_#{k}"] = v
|
55
|
+
end
|
56
|
+
result
|
36
57
|
end
|
37
58
|
end
|
38
59
|
end
|
@@ -29,6 +29,7 @@ module Puma
|
|
29
29
|
registry.gauge(:puma_pool_capacity, 'Number of allocatable worker threads', index: 0)
|
30
30
|
registry.gauge(:puma_max_threads, 'Maximum number of worker threads', index: 0)
|
31
31
|
registry.gauge(:puma_workers, 'Number of configured workers').set({}, 1)
|
32
|
+
registry.gauge(:puma_usage, 'Result of (1 - puma_pool_capacity/puma_max_threads)', index: 0)
|
32
33
|
end
|
33
34
|
|
34
35
|
def registry
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kubernetes-health
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wagner Caixeta
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rack
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -145,7 +145,7 @@ files:
|
|
145
145
|
homepage: https://github.com/platbr/kubernetes-health
|
146
146
|
licenses: []
|
147
147
|
metadata: {}
|
148
|
-
post_install_message:
|
148
|
+
post_install_message:
|
149
149
|
rdoc_options: []
|
150
150
|
require_paths:
|
151
151
|
- lib
|
@@ -160,8 +160,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
160
|
- !ruby/object:Gem::Version
|
161
161
|
version: '0'
|
162
162
|
requirements: []
|
163
|
-
rubygems_version: 3.
|
164
|
-
signing_key:
|
163
|
+
rubygems_version: 3.2.17
|
164
|
+
signing_key:
|
165
165
|
specification_version: 4
|
166
166
|
summary: This gem allows kubernetes monitoring your app while it is running migrates
|
167
167
|
and after it started.
|