kubernetes-health 3.10.1 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b3784236c03b3d08c9509f94da9e16cb0fb29f642f678684b20ef03d0b56a23
4
- data.tar.gz: 3dd137f774090002773184febbd91ee0a520e4cd76cb414f74b113f96fe19473
3
+ metadata.gz: 9db3c40048a1127229efee6653df4da36ef6d38ef1b57ab51677073fecdede0d
4
+ data.tar.gz: 12402ee8b8e151cb4c5992d63fc37607f6ae7ae070e6e8fb57862825ec0b7146
5
5
  SHA512:
6
- metadata.gz: b49429f971fad38a11dc3a65cca1553b8e7bc7463f2ca24b3a85d31d59e95506fcf79f6fbbe17cf2efade3e234b29556839171b0a5fec0791adb0456fcb313bc
7
- data.tar.gz: 5d1f22d376003de55156937fb7449ae2f233a5c8f4a0ff1956c72f0f41cd773101ffb1b335d1ba1c33c1ea4d88186e7089c5589428a2ee3c0ebfc3345bee14ef
6
+ metadata.gz: 598d564124cc3ff32b959538f4e3d9d9863c058ece7eadc9dd37fccfbdd4fe1010b503382a24d319d690c87a12bf3a81f8b1c7717ddb3ae954d7c9f242a7db0b
7
+ data.tar.gz: 8c8bd4f687f964583cb2390bd7c43f141173bb8273a47fed82269e4829a36c43a92bcfa681e5c13e104be2488c905f65361714e1ca4e46e6e38a89406e2e210d
data/README.md CHANGED
@@ -16,7 +16,7 @@ This gem open a HTTP port for monitoring your rails app while it is running Migr
16
16
  Add this line to your application's Gemfile:
17
17
 
18
18
  ```ruby
19
- gem 'kubernetes-health', '~> 3.6'
19
+ gem 'kubernetes-health', '~> 3.7'
20
20
  ```
21
21
 
22
22
  ## Enabling puma plugin
@@ -50,7 +50,7 @@ In Kubernetes you need to configure your deployment `readinessProbe` and `livene
50
50
 
51
51
  Setting `failureThreshold` is import to avoid problems when app finish migrates and is starting the web process.
52
52
 
53
- ## Enabling monitoring while `rake db:migrate` runs
53
+ ## Enabling liveness/readiness routes while `rake db:migrate` runs
54
54
 
55
55
  Your Dockerfile's entry script needs to run migrates before start your web app.
56
56
 
@@ -62,9 +62,28 @@ or add in your `application.rb`.
62
62
  # default: false
63
63
  Kubernetes::Health::Config.enable_rack_on_migrate = true
64
64
  ```
65
- The defined port at `config/puma.rb` will be used.
65
+ The defined port at `config/puma.rb` will be used but can be overrided by `KUBERNETES_HEALTH_METRICS_PORT` env var.
66
66
 
67
- ## Enabling monitoring for `sidekiq`
67
+ ## Enabling liveness/readiness routes while any `rake` tasks runs
68
+
69
+ If you need to run another rake tasks than `db:migrate`, like `assets:precompile`, you can enable the monitoring routes by this way:
70
+
71
+ Add a `rake` file enhancing the original task by using `kubernetes_health:rack_on_rake` task. For example:
72
+
73
+ ```
74
+ # File: lib/tasks/kubernetes_health_enable_rack_on_assets_precompile.rake
75
+ Rake::Task['assets:precompile'].enhance(['kubernetes_health:rack_on_rake'])
76
+ ```
77
+
78
+ ```
79
+ # File: lib/tasks/kubernetes_health_enable_rack_on_assets_clobber.rake
80
+ Rake::Task['assets:clobber'].enhance(['kubernetes_health:rack_on_rake'])
81
+ ```
82
+
83
+ I do recomend doing some check to make it only enables in K8S environment.
84
+ The defined port at `config/puma.rb` will be used but can be overrided by `KUBERNETES_HEALTH_METRICS_PORT` env var.
85
+
86
+ ## Enabling liveness/readiness routes for `sidekiq`
68
87
 
69
88
  Add `KUBERNETES_HEALTH_ENABLE_RACK_ON_SIDEKIQ=true` environment variable.
70
89
 
@@ -74,10 +93,11 @@ or add in your `application.rb`.
74
93
  # default: false
75
94
  Kubernetes::Health::Config.enable_rack_on_sidekiq = true
76
95
  ```
77
- The defined port at `config/puma.rb` will be used.
96
+ The defined port at `config/puma.rb` will be used but can be overrided by `KUBERNETES_HEALTH_METRICS_PORT` env var.
78
97
 
79
- ### How `rake db:migrate` and `sidekiq` monitoring works
98
+ ### How `rake` and `sidekiq` monitoring works
80
99
  It will run a RACK server for `/_readiness`, `/_liveness` and `/_metrics`.
100
+ The liveness route will respond using `200` but readiness `503`.
81
101
 
82
102
  ## Avoiding migrations running in parallel and making kubernetes happy.
83
103
  Rails already avoid migrations running in parallel, but it raise exceptions. This gem will just wait for other migrations without exit.
@@ -97,7 +117,7 @@ Kubernetes::Health::Config.enable_lock_on_migrate = true
97
117
  By default it is working for PostgreSQL, but you can customize it using a lambda:
98
118
  ```
99
119
  Kubernetes::Health::Config.lock_or_wait = lambda {
100
- ActiveRecord::Base.connection.execute 'select pg_advisory_lock(123456789123456789);'
120
+ ActiveRecord::Base.connection.execute "SET lock_timeout TO '3600s'; SELECT pg_advisory_lock(123456789123456789);"
101
121
  }
102
122
 
103
123
  Kubernetes::Health::Config.unlock = lambda {
@@ -2,6 +2,6 @@ require 'kubernetes/health/rack_on_sidekiq'
2
2
 
3
3
  if Kubernetes::Health::Config.enable_rack_on_sidekiq && Kubernetes::Health::SidekiqOptionsResolver[:concurrency].positive?
4
4
  Thread.new do
5
- Rack::Handler.default.run Kubernetes::Health::RackOnSidekiq.new
5
+ Rack::Handler::WEBrick.run Kubernetes::Health::RackOnSidekiq.new, { Port: Kubernetes::Health::Config.metrics_port }
6
6
  end
7
7
  end
@@ -1,8 +1,10 @@
1
1
  module Kubernetes
2
2
  module Health
3
3
  class Config
4
+ METRICS_PORT_FROM_PUMA_CONFIG = File.read(File.join(Rails.root, 'config', 'puma.rb')).match(/kubernetes_url\s.+:(?<metrics_port>\d+)/)['metrics_port'] rescue nil
4
5
  @@live_if = lambda { true }
5
6
  @@ready_if = lambda { true }
7
+ @@metrics_port = ENV['KUBERNETES_HEALTH_METRICS_PORT'] || METRICS_PORT_FROM_PUMA_CONFIG || 9393
6
8
  @@enable_lock_on_migrate = [true, 'true'].include? ENV['KUBERNETES_HEALTH_ENABLE_LOCK_ON_MIGRATE']
7
9
  @@enable_rack_on_migrate = [true, 'true'].include? ENV['KUBERNETES_HEALTH_ENABLE_RACK_ON_MIGRATE']
8
10
  @@enable_rack_on_sidekiq = [true, 'true'].include? ENV['KUBERNETES_HEALTH_ENABLE_RACK_ON_SIDEKIQ']
@@ -15,7 +17,7 @@ module Kubernetes
15
17
  Rails.logger.debug "Kubernetes Health - Request: Path: #{req.path_info} / Params: #{req.params} / HTTP Code: #{http_code}\n#{content}" rescue nil
16
18
  }
17
19
 
18
- @@lock_or_wait = lambda { ActiveRecord::Base.connection.execute 'select pg_advisory_lock(123456789123456789);' }
20
+ @@lock_or_wait = lambda { ActiveRecord::Base.connection.execute "SET lock_timeout TO '3600s'; SELECT pg_advisory_lock(123456789123456789);" }
19
21
  @@unlock = lambda { ActiveRecord::Base.connection.execute 'select pg_advisory_unlock(123456789123456789);' }
20
22
 
21
23
  def self.lock_or_wait
@@ -113,6 +115,14 @@ module Kubernetes
113
115
  def self.response_format=(value)
114
116
  @@response_format = value
115
117
  end
118
+
119
+ def self.metrics_port
120
+ @@metrics_port
121
+ end
122
+
123
+ def self.metrics_port=(value)
124
+ @@metrics_port = value
125
+ end
116
126
  end
117
127
  end
118
128
  end
@@ -1,5 +1,5 @@
1
1
  namespace :kubernetes_health do
2
- task :lock_on_migrate do
2
+ task :lock_on_rake do
3
3
  Rails.logger.info "Kubernetes Health: Lock on Migrate - Locking or waiting started."
4
4
  Kubernetes::Health::Config.lock_or_wait.call
5
5
  Rails.logger.info "Kubernetes Health: Lock on Migrate - Locking or waiting finished."
@@ -9,5 +9,4 @@ namespace :kubernetes_health do
9
9
  Rails.logger.info "Kubernetes Health: Lock on Migrate - Unlocking finished."
10
10
  }
11
11
  end
12
- end
13
- Rake::Task['db:migrate'].enhance(['kubernetes_health:lock_on_migrate'])
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'rack'
2
+ require "kubernetes/health/rack_on_rake"
3
+
4
+ namespace :kubernetes_health do
5
+ task :rack_on_rake do
6
+ Thread.new do
7
+ Rack::Handler::WEBrick.run Kubernetes::Health::RackOnRake.new, { Port: Kubernetes::Health::Config.metrics_port }
8
+ end
9
+ end
10
+ end
@@ -1,6 +1,6 @@
1
1
  module Kubernetes
2
2
  module Health
3
- class RackOnMigrate
3
+ class RackOnRake
4
4
  def call(env)
5
5
  req = ::Rack::Request.new(env)
6
6
  case req.path_info
@@ -11,7 +11,7 @@ module Kubernetes
11
11
  else
12
12
  http_code = 404
13
13
  end
14
- ::Kubernetes::Health::Config.request_log_callback.call(req, http_code)
14
+ ::Kubernetes::Health::Config.request_log_callback.call(req, http_code, '')
15
15
  [http_code, {}, []]
16
16
  end
17
17
  end
@@ -2,8 +2,10 @@ module Kubernetes
2
2
  module Health
3
3
  class Railtie < Rails::Railtie
4
4
  rake_tasks do
5
- load 'kubernetes/health/rack_on_migrate.rake' if Kubernetes::Health::Config.enable_rack_on_migrate
6
- load 'kubernetes/health/lock_on_migrate.rake' if Kubernetes::Health::Config.enable_lock_on_migrate
5
+ load 'kubernetes/health/rack_on_rake.rake'
6
+ load 'kubernetes/health/lock_on_rake.rake'
7
+ Rake::Task['db:migrate'].enhance(['kubernetes_health:rack_on_rake']) if Kubernetes::Health::Config.enable_rack_on_migrate
8
+ Rake::Task['db:migrate'].enhance(['kubernetes_health:lock_on_rake']) if Kubernetes::Health::Config.enable_lock_on_migrate
7
9
  end
8
10
  end
9
11
  end
@@ -1,5 +1,5 @@
1
1
  module Kubernetes
2
2
  module Health
3
- VERSION = '3.10.1'.freeze
3
+ VERSION = '3.12.0'.freeze
4
4
  end
5
5
  end
@@ -54,7 +54,9 @@ module Puma
54
54
  end
55
55
 
56
56
  def generate_extended_puma_stats
57
- puma_stats = @launcher&.stats || {}
57
+ return {} if @launcher.nil?
58
+
59
+ puma_stats = @launcher.stats
58
60
  # On puma <= 4 puma_stats is a String
59
61
  puma_stats = JSON.parse(puma_stats, symbolize_names: true) unless puma_stats.is_a?(Hash)
60
62
  # Including usage stats.
@@ -3,7 +3,7 @@ require 'puma/kubernetes/dsl'
3
3
  Puma::Plugin.create do
4
4
  # rubocop:disable Kubernetes/MethodLength, Kubernetes/AbcSize
5
5
  def start(launcher)
6
- str = launcher.options[:kubernetes_url] || 'tcp://0.0.0.0:9393'
6
+ str = launcher.options[:kubernetes_url] || "tcp://0.0.0.0:#{Kubernetes::Health::Config.metrics_port}"
7
7
 
8
8
  require 'puma/kubernetes/app'
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubernetes-health
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.10.1
4
+ version: 3.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wagner Caixeta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-19 00:00:00.000000000 Z
11
+ date: 2024-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,9 +140,9 @@ files:
140
140
  - lib/kubernetes/health.rb
141
141
  - lib/kubernetes/health/config.rb
142
142
  - lib/kubernetes/health/engine.rb
143
- - lib/kubernetes/health/lock_on_migrate.rake
144
- - lib/kubernetes/health/rack_on_migrate.rake
145
- - lib/kubernetes/health/rack_on_migrate.rb
143
+ - lib/kubernetes/health/lock_on_rake.rake
144
+ - lib/kubernetes/health/rack_on_rake.rake
145
+ - lib/kubernetes/health/rack_on_rake.rb
146
146
  - lib/kubernetes/health/rack_on_sidekiq.rb
147
147
  - lib/kubernetes/health/railtie.rb
148
148
  - lib/kubernetes/health/sidekiq_options_resolver.rb
@@ -1,11 +0,0 @@
1
- require 'rack'
2
- require "kubernetes/health/rack_on_migrate"
3
-
4
- namespace :kubernetes_health do
5
- task :rack_on_migrate do
6
- Thread.new do
7
- Rack::Handler.default.run Kubernetes::Health::RackOnMigrate.new
8
- end
9
- end
10
- end
11
- Rake::Task['db:migrate'].enhance(['kubernetes_health:rack_on_migrate'])