Class: Barnes::Instruments::PumaInstrument

Inherits:
Object
  • Object
show all
Defined in:
lib/barnes/instruments/puma_instrument.rb,
lib/barnes/instruments/puma_stats_value.rb

Defined Under Namespace

Classes: StatValue

Instance Method Summary collapse

Constructor Details

#initialize(sample_rate = nil) ⇒ PumaInstrument

Returns a new instance of PumaInstrument.



7
8
9
10
# File 'lib/barnes/instruments/puma_instrument.rb', line 7

def initialize(sample_rate=nil)
  @debug = ENV["BARNES_DEBUG_PUMA_STATS"]
  @puma_has_stats = (defined?(::Puma) && ::Puma.respond_to?(:stats))
end

Instance Method Details

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/barnes/instruments/puma_instrument.rb', line 32

def instrument!(state, counters, gauges)
  gauges['using.puma'] = 1

  stats = json_stats
  return if stats.empty?

  puts "Puma debug stats from barnes: #{stats}" if @debug

  pool_capacity = StatValue.new(stats, "pool_capacity").value
  max_threads   = StatValue.new(stats, "max_threads").value
  spawned       = StatValue.new(stats, "running").value

  gauges[:'pool.capacity']    = pool_capacity if pool_capacity
  gauges[:'threads.max']      = max_threads   if max_threads
  gauges[:'threads.spawned']  = spawned       if spawned
end

#json_statsObject



22
23
24
25
26
27
28
29
30
# File 'lib/barnes/instruments/puma_instrument.rb', line 22

def json_stats
  return {} unless @puma_has_stats
  MultiJson.load(::Puma.stats || "{}")

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

#start!(state) ⇒ Object



18
19
20
# File 'lib/barnes/instruments/puma_instrument.rb', line 18

def start!(state)
  require 'multi_json'
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/barnes/instruments/puma_instrument.rb', line 12

def valid?
  return false unless defined?(Puma)
  return false unless ENV["DYNO"] && ENV["DYNO"].start_with?("web")
  true
end