Class: PrometheusExporter::Instrumentation::Process

Inherits:
PeriodicStats
  • Object
show all
Defined in:
lib/prometheus_exporter/instrumentation/process.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PeriodicStats

started?, stop, worker_loop

Constructor Details

#initialize(metric_labels) ⇒ Process

Returns a new instance of Process.



27
28
29
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 27

def initialize(metric_labels)
  @metric_labels = metric_labels
end

Class Method Details

.start(client: nil, type: "ruby", frequency: 30, labels: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 6

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

  worker_loop do
    metric = process_collector.collect
    client.send_json metric
  end

  super
end

Instance Method Details

#collectObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 31

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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 65

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]
  metric[:malloc_increase_bytes_limit] = stat[:malloc_increase_bytes_limit]
  metric[:oldmalloc_increase_bytes_limit] = stat[:oldmalloc_increase_bytes_limit]
end

#collect_process_stats(metric) ⇒ Object



60
61
62
63
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 60

def collect_process_stats(metric)
  metric[:pid] = pid
  metric[:rss] = rss
end

#collect_v8_stats(metric) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 76

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

#pidObject



42
43
44
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 42

def pid
  @pid = ::Process.pid
end

#rssObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/prometheus_exporter/instrumentation/process.rb', line 46

def rss
  @pagesize ||=
    begin
      `getconf PAGESIZE`.to_i
    rescue StandardError
      4096
    end
  begin
    File.read("/proc/#{pid}/statm").split(" ")[1].to_i * @pagesize
  rescue StandardError
    0
  end
end