Skip to content

Commit e3ef17a

Browse files
authored
FEATURE: Add process_cpu_seconds_total metric (#138)
This allows us to track the CPU usage time for each process as part of application performance monitoring efforts.
1 parent ddc5b80 commit e3ef17a

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

lib/internal_metric/process.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class Process < Base
2222
gc_major_by: "Reason the last major GC was triggered",
2323
major_gc_count: "Major GC operations by process",
2424
minor_gc_count: "Minor GC operations by process",
25-
total_allocated_objects: "Total number of allocateds objects by process",
25+
total_allocated_objects: "Total number of allocated objects by process",
2626
job_failures: "Number of scheduled and regular jobs that failed in a process",
27+
process_cpu_seconds_total: "Total CPU time used by the process",
2728
}
2829

2930
attribute :type,
@@ -45,7 +46,8 @@ class Process < Base
4546
:active_record_connections_count,
4647
:active_record_failover_count,
4748
:redis_failover_count,
48-
:job_failures
49+
:job_failures,
50+
:process_cpu_seconds_total
4951

5052
def initialize
5153
@active_record_connections_count = {}

lib/reporter/process.rb

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def rss
5656
end
5757
end
5858

59+
def process_cpu_seconds_total
60+
::Process.clock_gettime(::Process::CLOCK_PROCESS_CPUTIME_ID)
61+
end
62+
5963
def collect_scheduler_stats(metric)
6064
metric.deferred_jobs_queued = Scheduler::Defer.length
6165

@@ -80,6 +84,7 @@ def collect_process_stats(metric)
8084
metric.pid = pid
8185
metric.rss = rss
8286
metric.thread_count = Thread.list.count
87+
metric.process_cpu_seconds_total = process_cpu_seconds_total
8388
end
8489

8590
def collect_gc_stats(metric)

spec/lib/reporter/process_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
expect(metric.v8_physical_size).to be > 0
2323
expect(metric.pid).to be > 0
2424
expect(metric.thread_count).to be > 0
25+
expect(metric.process_cpu_seconds_total).to be > 0
2526

2627
# macos does not support these metrics
2728
expect(metric.rss).to be > 0 unless RbConfig::CONFIG["arch"] =~ /darwin/

0 commit comments

Comments
 (0)