Sha256: 3ed763e80d95806952cf2d6c9108a29d954cc4ccc8ff051481b85b32fc902190

Contents?: true

Size: 1.91 KB

Versions: 10

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

begin
  require 'raindrops'
rescue LoadError
  # No raindrops available, dont do anything
end

module PrometheusExporter::Instrumentation
  # collects stats from unicorn
  class Unicorn
    def self.start(pid_file:, listener_address:, client: nil, frequency: 30)
      unicorn_collector = new(pid_file: pid_file, listener_address: listener_address)
      client ||= PrometheusExporter::Client.default
      Thread.new do
        loop do
          begin
            metric = unicorn_collector.collect
            client.send_json metric
          rescue StandardError => e
            STDERR.puts("Prometheus Exporter Failed To Collect Unicorn Stats #{e}")
          ensure
            sleep frequency
          end
        end
      end
    end

    def initialize(pid_file:, listener_address:)
      @pid_file = pid_file
      @listener_address = listener_address
      @tcp = listener_address =~ /\A.+:\d+\z/
    end

    def collect
      metric = {}
      metric[:type] = 'unicorn'
      collect_unicorn_stats(metric)
      metric
    end

    def collect_unicorn_stats(metric)
      stats = listener_address_stats

      metric[:active_workers_total] = stats.active
      metric[:request_backlog_total] = stats.queued
      metric[:workers_total] = worker_process_count
    end

    private

    def worker_process_count
      return nil unless File.exist?(@pid_file)
      pid = File.read(@pid_file).to_i

      return nil if pid < 1

      # find all processes whose parent is the unicorn master
      # but we're actually only interested in the number of processes (= lines of output)
      result = `pgrep -P #{pid} -f unicorn -a`
      result.lines.count
    end

    def listener_address_stats
      if @tcp
        Raindrops::Linux.tcp_listener_stats([@listener_address])[@listener_address]
      else
        Raindrops::Linux.unix_listener_stats([@listener_address])[@listener_address]
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
prometheus_exporter-0.8.0 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.7.0 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.6.0 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.5.3 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.5.2 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.5.1 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.5.0 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.4.17 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.4.16 lib/prometheus_exporter/instrumentation/unicorn.rb
prometheus_exporter-0.4.15 lib/prometheus_exporter/instrumentation/unicorn.rb