Sha256: 0951b32127b9c870c98ae62400ebb4d7206c1c1d732b07557e2cb3d63cf0728c

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require 'barnes/instruments/puma_stats_value'
module Barnes
  module Instruments
    class PumaInstrument
      def initialize(sample_rate=nil)
        @debug = ENV["BARNES_DEBUG_PUMA_STATS"]
        @puma_has_stats = (defined?(::Puma) && ::Puma.respond_to?(:stats))
      end

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

      def start!(state)
        require 'multi_json'
      end

      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/
        raise e unless e.message =~ /stats/
        return {}
      end

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
barnes-0.0.8 lib/barnes/instruments/puma_instrument.rb
barnes-0.0.7 lib/barnes/instruments/puma_instrument.rb