Skip to content

Commit 382e23f

Browse files
authored
Add marking and sweeping time as Process stat (#300)
* Add marking and sweeping time as Process stat Introduced in https://bugs.ruby-lang.org/issues/19437 * Do provide marking_time and sweeping_time on Ruby version bellow 3.3
1 parent c069f71 commit 382e23f

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ end
373373
| Counter | `major_gc_ops_total` | Major GC operations by process |
374374
| Counter | `minor_gc_ops_total` | Minor GC operations by process |
375375
| Counter | `allocated_objects_total` | Total number of allocated objects by process |
376+
| Gauge | `marking_time` | Marking time spent (Ruby 3.3 minimum) |
377+
| Gauge | `sweeping_time` | Sweeping time spent (Ruby 3.3 minimum) |
376378

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

lib/prometheus_exporter/instrumentation/process.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def collect_process_stats(metric)
6262
metric[:rss] = rss
6363
end
6464

65+
SWEEPING_AND_MARKING = RUBY_VERSION >= "3.3.0"
66+
6567
def collect_gc_stats(metric)
6668
stat = GC.stat
6769
metric[:heap_live_slots] = stat[:heap_live_slots]
@@ -71,6 +73,10 @@ def collect_gc_stats(metric)
7173
metric[:allocated_objects_total] = stat[:total_allocated_objects]
7274
metric[:malloc_increase_bytes_limit] = stat[:malloc_increase_bytes_limit]
7375
metric[:oldmalloc_increase_bytes_limit] = stat[:oldmalloc_increase_bytes_limit]
76+
if SWEEPING_AND_MARKING
77+
metric[:marking_time] = stat[:marking_time]
78+
metric[:sweeping_time] = stat[:sweeping_time]
79+
end
7480
end
7581

7682
def collect_v8_stats(metric)

lib/prometheus_exporter/server/process_collector.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ProcessCollector < TypeCollector
1616
"Limit before Ruby triggers a GC against current objects (bytes).",
1717
oldmalloc_increase_bytes_limit:
1818
"Limit before Ruby triggers a major GC against old objects (bytes).",
19+
marking_time: "Time spent in GC marking.",
20+
sweeping_time: "Time spent in GC sweeping.",
1921
}
2022

2123
PROCESS_COUNTERS = {

test/server/process_collector_test.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ def base_data
2727
"major_gc_ops_total" => 4000,
2828
"minor_gc_ops_total" => 4001,
2929
"allocated_objects_total" => 4002,
30+
"marking_time" => 4003,
31+
"sweeping_time" => 4004,
3032
}
3133
end
3234

3335
def test_metrics_collection
3436
collector.collect(base_data)
3537

36-
assert_equal 10, collector.metrics.size
38+
assert_equal 12, collector.metrics.size
3739
assert_equal [
3840
'heap_free_slots{pid="1000",hostname="localhost"} 1000',
3941
'heap_live_slots{pid="1000",hostname="localhost"} 1001',
@@ -42,6 +44,8 @@ def test_metrics_collection
4244
'v8_physical_size{pid="1000",hostname="localhost"} 2003',
4345
'v8_heap_count{pid="1000",hostname="localhost"} 2004',
4446
'rss{pid="1000",hostname="localhost"} 3000',
47+
'marking_time{pid="1000",hostname="localhost"} 4003',
48+
'sweeping_time{pid="1000",hostname="localhost"} 4004',
4549
'major_gc_ops_total{pid="1000",hostname="localhost"} 4000',
4650
'minor_gc_ops_total{pid="1000",hostname="localhost"} 4001',
4751
'allocated_objects_total{pid="1000",hostname="localhost"} 4002',
@@ -51,22 +55,22 @@ def test_metrics_collection
5155

5256
def test_metrics_deduplication
5357
collector.collect(base_data)
54-
assert_equal 10, collector.metrics.size
55-
assert_equal 10, collector_metric_lines.size
58+
assert_equal 12, collector.metrics.size
59+
assert_equal 12, collector_metric_lines.size
5660

5761
collector.collect(base_data)
58-
assert_equal 10, collector.metrics.size
59-
assert_equal 10, collector_metric_lines.size
62+
assert_equal 12, collector.metrics.size
63+
assert_equal 12, collector_metric_lines.size
6064

6165
collector.collect(base_data.merge({ "hostname" => "localhost2" }))
62-
assert_equal 10, collector.metrics.size
63-
assert_equal 20, collector_metric_lines.size
66+
assert_equal 12, collector.metrics.size
67+
assert_equal 24, collector_metric_lines.size
6468
end
6569

6670
def test_metrics_expiration
6771
stub_monotonic_clock(0) do
6872
collector.collect(base_data)
69-
assert_equal 10, collector.metrics.size
73+
assert_equal 12, collector.metrics.size
7074
end
7175

7276
stub_monotonic_clock(max_metric_age + 1) { assert_equal 0, collector.metrics.size }

0 commit comments

Comments
 (0)