Class: Takwimu::Instruments::PumaBacklog

Inherits:
Object
  • Object
show all
Defined in:
lib/takwimu/instruments/puma_backlog.rb

Instance Method Summary collapse

Constructor Details

#initialize(sample_rate = nil) ⇒ PumaBacklog

Returns a new instance of PumaBacklog.



6
7
# File 'lib/takwimu/instruments/puma_backlog.rb', line 6

def initialize(sample_rate=nil)
end

Instance Method Details

#instrument!(state, counters, gauges, timers) ⇒ Object

For single worker process use backlog directly for multiple workers sum backlog.

github.com/puma/puma/pull/1532



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/takwimu/instruments/puma_backlog.rb', line 33

def instrument!(state, counters, gauges, timers)
  stats = self.json_stats
  return if stats.empty?

  backlog = stats["backlog"]
  if backlog.nil?
    backlog = stats["worker_status"].map do |worker_status|
      worker_status["last_status"]["backlog"]
    end.reduce(0, :+)
  end

  gauges[:'backlog.requests'] = backlog
end

#json_statsObject



19
20
21
22
23
24
25
26
27
# File 'lib/takwimu/instruments/puma_backlog.rb', line 19

def json_stats
  MultiJson.load(Puma.stats || "{}")

# Puma loader has not been initialized yet
rescue NoMethodError => e
  raise e unless e.message =~ /nil/
  raise e unless e.message =~ /stats/
  return {}
end

#start!(state) ⇒ Object



15
16
17
# File 'lib/takwimu/instruments/puma_backlog.rb', line 15

def start!(state)
  require 'multi_json'
end

#valid?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/takwimu/instruments/puma_backlog.rb', line 9

def valid?
  defined?(Puma) &&
    Puma.respond_to?(:stats) &&
    ENV["DYNO"] && ENV["DYNO"].start_with?("web")
end