Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add marking and sweeping time as Process stat #300

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (Ruby 3.3 minimum) |
| Gauge | `sweeping_time` | Sweeping time spent (Ruby 3.3 minimum) |

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

Expand Down
6 changes: 6 additions & 0 deletions lib/prometheus_exporter/instrumentation/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ def collect_process_stats(metric)

end

SWEEPING_AND_MARKING = RUBY_VERSION >= "3.3.0"

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]
if SWEEPING_AND_MARKING
metric[:marking_time] = stat[:marking_time]
metric[:sweeping_time] = stat[:sweeping_time]
end
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