Sha256: b20262d648a725218b136e9b07d88befd44d2176f04ac38b1e58532aba813761

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Takwimu
  module Instruments
    class PumaBacklog
      def initialize(sample_rate=nil)
      end

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

      def start!(state)
        require 'multi_json'
      end

      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

      # For single worker process use backlog directly
      # for multiple workers sum backlog.
      #
      # https://github.com/puma/puma/pull/1532
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
takwimu-0.1.1 lib/takwimu/instruments/puma_backlog.rb
takwimu-0.1.0 lib/takwimu/instruments/puma_backlog.rb