Class: Kubernetes::Health::RackOnSidekiq

Inherits:
Object
  • Object
show all
Defined in:
lib/kubernetes/health/rack_on_sidekiq.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kubernetes/health/rack_on_sidekiq.rb', line 8

def call(env)
  req = ::Rack::Request.new(env)
  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_metrics
    http_code = 200

    sidekiq_metrics = generate_sidekiq_metrics

    if ::Kubernetes::Health::Config.response_format == 'json'
      content = sidekiq_metrics.to_json
    else
      prometheus_registry.get(:sidekiq_capacity).set(sidekiq_metrics[:sidekiq_capacity])
      prometheus_registry.get(:sidekiq_busy).set(sidekiq_metrics[:sidekiq_busy])
      prometheus_registry.get(:sidekiq_usage).set(sidekiq_metrics[:sidekiq_usage])
      content = Prometheus::Client::Formats::Text.marshal(prometheus_registry)
    end
  else
    http_code = 404
  end
  ::Kubernetes::Health::Config.request_log_callback.call(req, http_code, content)

  [http_code, type, [content]]
end

#generate_sidekiq_metricsObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kubernetes/health/rack_on_sidekiq.rb', line 44

def generate_sidekiq_metrics
  sidekiq_info = Sidekiq::ProcessSet.new.to_a.filter { |p| p.identity == SidekiqOptionsResolver[:identity] }

  stats = {
    sidekiq_capacity: SidekiqOptionsResolver[:concurrency],
    sidekiq_busy: sidekiq_info.size.zero? ? 0 : sidekiq_info[0]['busy']
  }

  stats[:sidekiq_usage] = (stats[:sidekiq_busy] / stats[:sidekiq_capacity].to_f).round(2)
  stats
end

#prometheus_registryObject



34
35
36
37
38
39
40
41
42
# File 'lib/kubernetes/health/rack_on_sidekiq.rb', line 34

def prometheus_registry
  return @prometheus_registry unless @prometheus_registry.nil?

  @prometheus_registry = Prometheus::Client.registry
  @prometheus_registry.gauge(:sidekiq_capacity, docstring: 'Sidekiq Threads Number')
  @prometheus_registry.gauge(:sidekiq_busy, docstring: 'Sidekiq Busy Threads')
  @prometheus_registry.gauge(:sidekiq_usage, docstring: 'Result of sidekiq_busy/sidekiq_capacity')
  @prometheus_registry
end