class PrometheusExporter::Instrumentation::Process

Public Class Methods

new(metric_labels) click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 45
def initialize(metric_labels)
  @metric_labels = metric_labels
end
start(client: nil, type: "ruby", frequency: 30, labels: nil) click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 8
def self.start(client: nil, type: "ruby", frequency: 30, labels: nil)

  metric_labels =
    if labels && type
      labels.merge(type: type)
    elsif labels
      labels
    else
      { type: type }
    end

  process_collector = new(metric_labels)
  client ||= PrometheusExporter::Client.default

  stop if @thread

  @thread = Thread.new do
    while true
      begin
        metric = process_collector.collect
        client.send_json metric
      rescue => e
        client.logger.error("Prometheus Exporter Failed To Collect Process Stats #{e}")
      ensure
        sleep frequency
      end
    end
  end
end
stop() click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 38
def self.stop
  if t = @thread
    t.kill
    @thread = nil
  end
end

Public Instance Methods

collect() click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 49
def collect
  metric = {}
  metric[:type] = "process"
  metric[:metric_labels] = @metric_labels
  metric[:hostname] = ::PrometheusExporter.hostname
  collect_gc_stats(metric)
  collect_v8_stats(metric)
  collect_process_stats(metric)
  metric
end
collect_gc_stats(metric) click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 75
def collect_gc_stats(metric)
  stat = GC.stat
  metric[:heap_live_slots] = stat[:heap_live_slots]
  metric[:heap_free_slots] = stat[:heap_free_slots]
  metric[:major_gc_ops_total] = stat[:major_gc_count]
  metric[:minor_gc_ops_total] = stat[:minor_gc_count]
  metric[:allocated_objects_total] = stat[:total_allocated_objects]
end
collect_process_stats(metric) click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 69
def collect_process_stats(metric)
  metric[:pid] = pid
  metric[:rss] = rss

end
collect_v8_stats(metric) click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 84
def collect_v8_stats(metric)
  return if !defined? MiniRacer

  metric[:v8_heap_count] = metric[:v8_heap_size] = 0
  metric[:v8_heap_size] = metric[:v8_physical_size] = 0
  metric[:v8_used_heap_size] = 0

  ObjectSpace.each_object(MiniRacer::Context) do |context|
    stats = context.heap_stats
    if stats
      metric[:v8_heap_count] += 1
      metric[:v8_heap_size] += stats[:total_heap_size].to_i
      metric[:v8_used_heap_size] += stats[:used_heap_size].to_i
      metric[:v8_physical_size] += stats[:total_physical_size].to_i
    end
  end
end
pid() click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 60
def pid
  @pid = ::Process.pid
end
rss() click to toggle source
# File lib/prometheus_exporter/instrumentation/process.rb, line 64
def rss
  @pagesize ||= `getconf PAGESIZE`.to_i rescue 4096
  File.read("/proc/#{pid}/statm").split(' ')[1].to_i * @pagesize rescue 0
end