Commit 142bf826 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'add-request-cputime-prometheus-metrics' into 'master'

Add request cpu time prometheus metrics

See merge request gitlab-org/gitlab!18584
parents ec3ee923 4be6cc58
......@@ -63,6 +63,19 @@ module Gitlab
def self.monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)
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
......@@ -44,6 +44,10 @@ module Gitlab
duration.in_milliseconds.to_i
end
def thread_cpu_duration
System.thread_cpu_duration(@thread_cputime_start)
end
def allocated_memory
@memory_after - @memory_before
end
......@@ -53,12 +57,14 @@ module Gitlab
@memory_before = System.memory_usage
@started_at = System.monotonic_time
@thread_cputime_start = System.thread_cpu_time
yield
ensure
@memory_after = System.memory_usage
@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_allocated_memory_bytes.observe(labels, allocated_memory * 1024.0)
......@@ -142,6 +148,12 @@ module Gitlab
"#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty?
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
docstring 'Transaction duration'
base_labels BASE_LABELS
......
......@@ -27,6 +27,14 @@ describe Gitlab::Metrics::Transaction do
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
it 'returns the allocated memory in bytes' do
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