Skip to content

Commit

Permalink
Add marking and sweeping time as Process stat
Browse files Browse the repository at this point in the history
  • Loading branch information
benoittgt committed Jan 9, 2024
1 parent 83a8979 commit d64ac57
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ end
| Counter | `major_gc_ops_total` | Major GC operations by process |
| Counter | `minor_gc_ops_total` | Minor GC operations by process |
| Counter | `allocated_objects_total` | Total number of allocated objects by process |
| Gauge | `marking_time` | Marking time spent |
| Gauge | `sweeping_time` | Sweeping time spent |

_Metrics marked with * are only collected when `MiniRacer` is defined._

Expand Down
2 changes: 2 additions & 0 deletions lib/prometheus_exporter/instrumentation/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def collect_gc_stats(metric)
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[:marking_time] = stat[:mark_time]
metric[:sweeping_time] = stat[:sweep_time]
end

def collect_v8_stats(metric)
Expand Down
2 changes: 2 additions & 0 deletions lib/prometheus_exporter/server/process_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ProcessCollector < TypeCollector
v8_physical_size: "Physical size consumed by V8 heaps.",
v8_heap_count: "Number of V8 contexts running.",
rss: "Total RSS used by process.",
marking_time: "Time spent in GC marking.",
sweeping_time: "Time spent in GC sweeping.",
}

PROCESS_COUNTERS = {
Expand Down
24 changes: 14 additions & 10 deletions test/server/process_collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ def base_data
"rss" => 3000,
"major_gc_ops_total" => 4000,
"minor_gc_ops_total" => 4001,
"allocated_objects_total" => 4002
"allocated_objects_total" => 4002,
"marking_time" => 4003,
"sweeping_time" => 4004,
}
end

def test_metrics_collection
collector.collect(base_data)

assert_equal 10, collector.metrics.size
assert_equal 12, collector.metrics.size
assert_equal [
'heap_free_slots{pid="1000",hostname="localhost"} 1000',
'heap_live_slots{pid="1000",hostname="localhost"} 1001',
Expand All @@ -42,30 +44,32 @@ def test_metrics_collection
'v8_physical_size{pid="1000",hostname="localhost"} 2003',
'v8_heap_count{pid="1000",hostname="localhost"} 2004',
'rss{pid="1000",hostname="localhost"} 3000',
'marking_time{pid="1000",hostname="localhost"} 4003',
'sweeping_time{pid="1000",hostname="localhost"} 4004',
'major_gc_ops_total{pid="1000",hostname="localhost"} 4000',
'minor_gc_ops_total{pid="1000",hostname="localhost"} 4001',
'allocated_objects_total{pid="1000",hostname="localhost"} 4002'
'allocated_objects_total{pid="1000",hostname="localhost"} 4002',
], collector_metric_lines
end

def test_metrics_deduplication
collector.collect(base_data)
assert_equal 10, collector.metrics.size
assert_equal 10, collector_metric_lines.size
assert_equal 12, collector.metrics.size
assert_equal 12, collector_metric_lines.size

collector.collect(base_data)
assert_equal 10, collector.metrics.size
assert_equal 10, collector_metric_lines.size
assert_equal 12, collector.metrics.size
assert_equal 12, collector_metric_lines.size

collector.collect(base_data.merge({ "hostname" => "localhost2" }))
assert_equal 10, collector.metrics.size
assert_equal 20, collector_metric_lines.size
assert_equal 12, collector.metrics.size
assert_equal 24, collector_metric_lines.size
end

def test_metrics_expiration
stub_monotonic_clock(0) do
collector.collect(base_data)
assert_equal 10, collector.metrics.size
assert_equal 12, collector.metrics.size
end

stub_monotonic_clock(max_metric_age + 1) do
Expand Down

0 comments on commit d64ac57

Please sign in to comment.