Commit 7ffacaae authored by Douwe Maan's avatar Douwe Maan

Merge branch '63873-process-start-time' into 'master'

Resolve "The process start time is invalid"

Closes #63873

See merge request gitlab-org/gitlab-ce!30195
parents 5608c861 04962880
---
title: Change ruby_process_start_time_seconds metric to unix timestamp instead of
seconds from boot.
merge_request: 30195
author:
type: fixed
...@@ -104,7 +104,7 @@ Some basic Ruby runtime metrics are available: ...@@ -104,7 +104,7 @@ Some basic Ruby runtime metrics are available:
| ruby_process_cpu_seconds_total | Gauge | 12.0 | Total amount of CPU time per process | | ruby_process_cpu_seconds_total | Gauge | 12.0 | Total amount of CPU time per process |
| ruby_process_max_fds | Gauge | 12.0 | Maximum number of open file descriptors per process | | ruby_process_max_fds | Gauge | 12.0 | Maximum number of open file descriptors per process |
| ruby_process_resident_memory_bytes | Gauge | 12.0 | Memory usage by process, measured in bytes | | ruby_process_resident_memory_bytes | Gauge | 12.0 | Memory usage by process, measured in bytes |
| ruby_process_start_time_seconds | Gauge | 12.0 | The elapsed time between system boot and the process started, measured in seconds | | ruby_process_start_time_seconds | Gauge | 12.0 | UNIX timestamp of process start time |
[GC.stat]: https://ruby-doc.org/core-2.3.0/GC.html#method-c-stat [GC.stat]: https://ruby-doc.org/core-2.3.0/GC.html#method-c-stat
......
...@@ -6,6 +6,12 @@ module Gitlab ...@@ -6,6 +6,12 @@ module Gitlab
module Metrics module Metrics
module Samplers module Samplers
class RubySampler < BaseSampler class RubySampler < BaseSampler
def initialize(interval)
metrics[:process_start_time_seconds].set(labels.merge(worker_label), Time.now.to_i)
super
end
def metrics def metrics
@metrics ||= init_metrics @metrics ||= init_metrics
end end
...@@ -47,7 +53,6 @@ module Gitlab ...@@ -47,7 +53,6 @@ module Gitlab
metrics[:file_descriptors].set(labels.merge(worker_label), System.file_descriptor_count) metrics[:file_descriptors].set(labels.merge(worker_label), System.file_descriptor_count)
metrics[:process_cpu_seconds_total].set(labels.merge(worker_label), ::Gitlab::Metrics::System.cpu_time) metrics[:process_cpu_seconds_total].set(labels.merge(worker_label), ::Gitlab::Metrics::System.cpu_time)
metrics[:process_max_fds].set(labels.merge(worker_label), ::Gitlab::Metrics::System.max_open_file_descriptors) metrics[:process_max_fds].set(labels.merge(worker_label), ::Gitlab::Metrics::System.max_open_file_descriptors)
metrics[:process_start_time_seconds].set(labels.merge(worker_label), ::Gitlab::Metrics::System.process_start_time)
set_memory_usage_metrics set_memory_usage_metrics
sample_gc sample_gc
......
...@@ -31,14 +31,6 @@ module Gitlab ...@@ -31,14 +31,6 @@ module Gitlab
match[1].to_i match[1].to_i
end end
def self.process_start_time
fields = File.read('/proc/self/stat').split
# fields[21] is linux proc stat field "(22) starttime".
# The value is expressed in clock ticks, divide by clock ticks for seconds.
( fields[21].to_i || 0 ) / clk_tck
end
else else
def self.memory_usage def self.memory_usage
0.0 0.0
...@@ -51,10 +43,6 @@ module Gitlab ...@@ -51,10 +43,6 @@ module Gitlab
def self.max_open_file_descriptors def self.max_open_file_descriptors
0 0
end end
def self.process_start_time
0
end
end end
def self.cpu_time def self.cpu_time
......
...@@ -8,12 +8,19 @@ describe Gitlab::Metrics::Samplers::RubySampler do ...@@ -8,12 +8,19 @@ describe Gitlab::Metrics::Samplers::RubySampler do
allow(Gitlab::Metrics::NullMetric).to receive(:instance).and_return(null_metric) allow(Gitlab::Metrics::NullMetric).to receive(:instance).and_return(null_metric)
end end
describe '#initialize' do
it 'sets process_start_time_seconds' do
Timecop.freeze do
expect(sampler.metrics[:process_start_time_seconds].get).to eq(Time.now.to_i)
end
end
end
describe '#sample' do describe '#sample' do
it 'samples various statistics' do it 'samples various statistics' do
expect(Gitlab::Metrics::System).to receive(:cpu_time) expect(Gitlab::Metrics::System).to receive(:cpu_time)
expect(Gitlab::Metrics::System).to receive(:file_descriptor_count) expect(Gitlab::Metrics::System).to receive(:file_descriptor_count)
expect(Gitlab::Metrics::System).to receive(:memory_usage) expect(Gitlab::Metrics::System).to receive(:memory_usage)
expect(Gitlab::Metrics::System).to receive(:process_start_time)
expect(Gitlab::Metrics::System).to receive(:max_open_file_descriptors) expect(Gitlab::Metrics::System).to receive(:max_open_file_descriptors)
expect(sampler).to receive(:sample_gc) expect(sampler).to receive(:sample_gc)
...@@ -44,13 +51,6 @@ describe Gitlab::Metrics::Samplers::RubySampler do ...@@ -44,13 +51,6 @@ describe Gitlab::Metrics::Samplers::RubySampler do
sampler.sample sampler.sample
end end
it 'adds a metric containing the process start time' do
expect(Gitlab::Metrics::System).to receive(:process_start_time).and_return(12345)
expect(sampler.metrics[:process_start_time_seconds]).to receive(:set).with({}, 12345)
sampler.sample
end
it 'adds a metric containing the process max file descriptors' do it 'adds a metric containing the process max file descriptors' do
expect(Gitlab::Metrics::System).to receive(:max_open_file_descriptors).and_return(1024) expect(Gitlab::Metrics::System).to receive(:max_open_file_descriptors).and_return(1024)
expect(sampler.metrics[:process_max_fds]).to receive(:set).with({}, 1024) expect(sampler.metrics[:process_max_fds]).to receive(:set).with({}, 1024)
......
...@@ -19,12 +19,6 @@ describe Gitlab::Metrics::System do ...@@ -19,12 +19,6 @@ describe Gitlab::Metrics::System do
expect(described_class.max_open_file_descriptors).to be > 0 expect(described_class.max_open_file_descriptors).to be > 0
end end
end end
describe '.process_start_time' do
it 'returns the process start time' do
expect(described_class.process_start_time).to be > 0
end
end
else else
describe '.memory_usage' do describe '.memory_usage' do
it 'returns 0.0' do it 'returns 0.0' do
...@@ -43,12 +37,6 @@ describe Gitlab::Metrics::System do ...@@ -43,12 +37,6 @@ describe Gitlab::Metrics::System do
expect(described_class.max_open_file_descriptors).to eq(0) expect(described_class.max_open_file_descriptors).to eq(0)
end end
end end
describe 'process_start_time' do
it 'returns 0' do
expect(described_class.process_start_time).to eq(0)
end
end
end end
describe '.cpu_time' do describe '.cpu_time' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment