Class: Puma::Kubernetes::App

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/kubernetes/app.rb

Instance Method Summary collapse

Constructor Details

#initialize(launcher) ⇒ App

Returns a new instance of App.



9
10
11
12
13
# File 'lib/puma/kubernetes/app.rb', line 9

def initialize(launcher)
  @launcher = launcher
  clustered = (@launcher.options[:workers] || 0) > 0
  @parser = Parser.new clustered
end

Instance Method Details

#call(_env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puma/kubernetes/app.rb', line 15

def call(_env)
  begin
    req = ::Rack::Request.new(_env)
    type = {}
    content = ''
    type = ::Kubernetes::Health::Config.response_format == 'json' ? { 'Content-Type' => 'application/json' } : { 'Content-Type' => 'text/plain' }
    case req.path_info
    when ::Kubernetes::Health::Config.route_liveness
      i_am_live = ::Kubernetes::Health::Config.live_if.arity == 0 ? ::Kubernetes::Health::Config.live_if.call : ::Kubernetes::Health::Config.live_if.call(req.params)
      http_code = i_am_live ? 200 : 503
    when ::Kubernetes::Health::Config.route_readiness
      i_am_ready = ::Kubernetes::Health::Config.ready_if.arity == 0 ? ::Kubernetes::Health::Config.ready_if.call : ::Kubernetes::Health::Config.ready_if.call(req.params)
      http_code = i_am_ready ? 200 : 503
    when ::Kubernetes::Health::Config.route_metrics
      http_code = 200
      if ::Kubernetes::Health::Config.response_format == 'json'
        content = include_puma_key_prefix(include_usage(JSON.parse(@launcher.stats))).to_json
      else
        @parser.parse include_usage(JSON.parse(@launcher.stats))
        content = Prometheus::Client::Formats::Text.marshal(Prometheus::Client.registry)
      end
    else
      http_code = 404
    end
  rescue
    http_code = 500
    content = ''
  end
  ::Kubernetes::Health::Config.request_log_callback.call(req, http_code, content)
  [http_code, type, [content]]
end

#include_puma_key_prefix(stats) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/puma/kubernetes/app.rb', line 51

def include_puma_key_prefix(stats)
  result = {}
  stats.each do |k,v|
    result["puma_#{k}"] = v
  end
  result
end

#include_usage(stats) ⇒ Object



47
48
49
50
# File 'lib/puma/kubernetes/app.rb', line 47

def include_usage(stats)
  stats['usage'] = (1 - stats['pool_capacity'].to_f / stats['max_threads']).round(2)
  stats
end