Commit 4be6cc58 authored by Qingyu Zhao's avatar Qingyu Zhao

Add request cpu time prometheus metrics

parent a40fa70a
...@@ -63,6 +63,19 @@ module Gitlab ...@@ -63,6 +63,19 @@ module Gitlab
def self.monotonic_time def self.monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second) Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)
end end
def self.thread_cpu_time
return unless defined?(Process::CLOCK_THREAD_CPUTIME_ID)
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_second)
end
def self.thread_cpu_duration(start_time)
end_time = thread_cpu_time
return unless start_time && end_time
end_time - start_time
end
end end
end end
end end
...@@ -44,6 +44,10 @@ module Gitlab ...@@ -44,6 +44,10 @@ module Gitlab
duration.in_milliseconds.to_i duration.in_milliseconds.to_i
end end
def thread_cpu_duration
System.thread_cpu_duration(@thread_cputime_start)
end
def allocated_memory def allocated_memory
@memory_after - @memory_before @memory_after - @memory_before
end end
...@@ -53,12 +57,14 @@ module Gitlab ...@@ -53,12 +57,14 @@ module Gitlab
@memory_before = System.memory_usage @memory_before = System.memory_usage
@started_at = System.monotonic_time @started_at = System.monotonic_time
@thread_cputime_start = System.thread_cpu_time
yield yield
ensure ensure
@memory_after = System.memory_usage @memory_after = System.memory_usage
@finished_at = System.monotonic_time @finished_at = System.monotonic_time
self.class.gitlab_transaction_cputime_seconds.observe(labels, thread_cpu_duration)
self.class.gitlab_transaction_duration_seconds.observe(labels, duration) self.class.gitlab_transaction_duration_seconds.observe(labels, duration)
self.class.gitlab_transaction_allocated_memory_bytes.observe(labels, allocated_memory * 1024.0) self.class.gitlab_transaction_allocated_memory_bytes.observe(labels, allocated_memory * 1024.0)
...@@ -142,6 +148,12 @@ module Gitlab ...@@ -142,6 +148,12 @@ module Gitlab
"#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty? "#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty?
end end
define_histogram :gitlab_transaction_cputime_seconds do
docstring 'Transaction thread cputime'
base_labels BASE_LABELS
buckets [0.1, 0.25, 0.5, 1.0, 2.5, 5.0]
end
define_histogram :gitlab_transaction_duration_seconds do define_histogram :gitlab_transaction_duration_seconds do
docstring 'Transaction duration' docstring 'Transaction duration'
base_labels BASE_LABELS base_labels BASE_LABELS
......
...@@ -27,6 +27,14 @@ describe Gitlab::Metrics::Transaction do ...@@ -27,6 +27,14 @@ describe Gitlab::Metrics::Transaction do
end end
end end
describe '#thread_cpu_duration' do
it 'returns the duration of a transaction in seconds' do
transaction.run { }
expect(transaction.thread_cpu_duration).to be > 0
end
end
describe '#allocated_memory' do describe '#allocated_memory' do
it 'returns the allocated memory in bytes' do it 'returns the allocated memory in bytes' do
transaction.run { 'a' * 32 } transaction.run { 'a' * 32 }
......
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