summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPawel Chojnacki <pawel@chojnacki.ws>2017-12-20 19:30:58 +0100
committerPawel Chojnacki <pawel@chojnacki.ws>2017-12-21 00:39:52 +0100
commit040167f0724027020f2d63b6e43481fb3e29dbfc (patch)
tree43d4c3aa998a4a8a4471d5ece67b6b9b68ce76f3
parented715b7926c31fddb1c835823d509672663e23e6 (diff)
downloadgitlab-ce-040167f0724027020f2d63b6e43481fb3e29dbfc.tar.gz
Use seconds where possible, and convert to milliseconds for Influxdb consumption
-rw-r--r--changelogs/unreleased/pawel-reduce_cardinality_of_prometheus_metrics.yml5
-rw-r--r--lib/gitlab/metrics/method_call.rb24
-rw-r--r--lib/gitlab/metrics/system.rb8
-rw-r--r--spec/lib/gitlab/metrics/method_call_spec.rb18
-rw-r--r--spec/lib/gitlab/metrics/system_spec.rb4
5 files changed, 31 insertions, 28 deletions
diff --git a/changelogs/unreleased/pawel-reduce_cardinality_of_prometheus_metrics.yml b/changelogs/unreleased/pawel-reduce_cardinality_of_prometheus_metrics.yml
new file mode 100644
index 00000000000..8213b493903
--- /dev/null
+++ b/changelogs/unreleased/pawel-reduce_cardinality_of_prometheus_metrics.yml
@@ -0,0 +1,5 @@
+---
+title: Reduce the number of buckets in gitlab_cache_operation_duration_seconds
+merge_request: 15881
+author:
+type: changed
diff --git a/lib/gitlab/metrics/method_call.rb b/lib/gitlab/metrics/method_call.rb
index bb39b1d5462..f4a916f154d 100644
--- a/lib/gitlab/metrics/method_call.rb
+++ b/lib/gitlab/metrics/method_call.rb
@@ -4,7 +4,7 @@ module Gitlab
class MethodCall
MUTEX = Mutex.new
BASE_LABELS = { module: nil, method: nil }.freeze
- attr_reader :real_time_seconds, :cpu_time, :call_count, :labels
+ attr_reader :real_time, :cpu_time, :call_count, :labels
def self.call_duration_histogram
return @call_duration_histogram if @call_duration_histogram
@@ -27,42 +27,38 @@ module Gitlab
@transaction = transaction
@name = name
@labels = { module: @module_name, method: @method_name }
- @real_time_seconds = 0.0
- @cpu_time = 0
+ @real_time = 0.0
+ @cpu_time = 0.0
@call_count = 0
end
# Measures the real and CPU execution time of the supplied block.
def measure
- start_real_seconds = System.monotonic_time
+ start_real = System.monotonic_time
start_cpu = System.cpu_time
retval = yield
- real_time_seconds = System.monotonic_time - start_real_seconds
+ real_time = System.monotonic_time - start_real
cpu_time = System.cpu_time - start_cpu
- @real_time_seconds += real_time_seconds
+ @real_time += real_time
@cpu_time += cpu_time
@call_count += 1
if call_measurement_enabled? && above_threshold?
- self.class.call_duration_histogram.observe(@transaction.labels.merge(labels), real_time_seconds)
+ self.class.call_duration_histogram.observe(@transaction.labels.merge(labels), real_time)
end
retval
end
- def real_time_milliseconds
- real_time_seconds.in_milliseconds.to_i
- end
-
# Returns a Metric instance of the current method call.
def to_metric
Metric.new(
Instrumentation.series,
{
- duration: real_time_milliseconds,
- cpu_duration: cpu_time,
+ duration: real_time.in_milliseconds.to_i,
+ cpu_duration: cpu_time.in_milliseconds.to_i,
call_count: call_count
},
method: @name
@@ -72,7 +68,7 @@ module Gitlab
# Returns true if the total runtime of this method exceeds the method call
# threshold.
def above_threshold?
- real_time_milliseconds >= Metrics.method_call_threshold
+ real_time.in_milliseconds >= Metrics.method_call_threshold
end
def call_measurement_enabled?
diff --git a/lib/gitlab/metrics/system.rb b/lib/gitlab/metrics/system.rb
index 4852017bf38..e60e245cf89 100644
--- a/lib/gitlab/metrics/system.rb
+++ b/lib/gitlab/metrics/system.rb
@@ -35,19 +35,19 @@ module Gitlab
if Process.const_defined?(:CLOCK_THREAD_CPUTIME_ID)
def self.cpu_time
Process
- .clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :millisecond)
+ .clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_second)
end
else
def self.cpu_time
Process
- .clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :millisecond)
+ .clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :float_second)
end
end
# Returns the current real time in a given precision.
#
- # Returns the time as a Fixnum.
- def self.real_time(precision = :millisecond)
+ # Returns the time as a Float for precision = :float_second.
+ def self.real_time(precision = :float_second)
Process.clock_gettime(Process::CLOCK_REALTIME, precision)
end
diff --git a/spec/lib/gitlab/metrics/method_call_spec.rb b/spec/lib/gitlab/metrics/method_call_spec.rb
index 91a70ba01a0..448246ff513 100644
--- a/spec/lib/gitlab/metrics/method_call_spec.rb
+++ b/spec/lib/gitlab/metrics/method_call_spec.rb
@@ -8,8 +8,7 @@ describe Gitlab::Metrics::MethodCall do
it 'measures the performance of the supplied block' do
method_call.measure { 'foo' }
- expect(method_call.real_time_seconds).to be_a_kind_of(Numeric)
- expect(method_call.real_time_milliseconds).to be_a_kind_of(Numeric)
+ expect(method_call.real_time).to be_a_kind_of(Numeric)
expect(method_call.cpu_time).to be_a_kind_of(Numeric)
expect(method_call.call_count).to eq(1)
end
@@ -65,14 +64,17 @@ describe Gitlab::Metrics::MethodCall do
describe '#to_metric' do
it 'returns a Metric instance' do
+ expect(method_call).to receive(:real_time).and_return(4.0001)
+ expect(method_call).to receive(:cpu_time).and_return(3.0001)
+
method_call.measure { 'foo' }
metric = method_call.to_metric
expect(metric).to be_an_instance_of(Gitlab::Metrics::Metric)
expect(metric.series).to eq('rails_method_calls')
- expect(metric.values[:duration]).to be_a_kind_of(Numeric)
- expect(metric.values[:cpu_duration]).to be_a_kind_of(Numeric)
+ expect(metric.values[:duration]).to eq(4000)
+ expect(metric.values[:cpu_duration]).to eq(3000)
expect(metric.values[:call_count]).to be_an(Integer)
expect(metric.tags).to eq({ method: 'Foo#bar' })
@@ -85,13 +87,13 @@ describe Gitlab::Metrics::MethodCall do
end
it 'returns false when the total call time is not above the threshold' do
- expect(method_call).to receive(:real_time_seconds).and_return(0.009)
+ expect(method_call).to receive(:real_time).and_return(0.009)
expect(method_call.above_threshold?).to eq(false)
end
it 'returns true when the total call time is above the threshold' do
- expect(method_call).to receive(:real_time_seconds).and_return(9)
+ expect(method_call).to receive(:real_time).and_return(9)
expect(method_call.above_threshold?).to eq(true)
end
@@ -132,7 +134,7 @@ describe Gitlab::Metrics::MethodCall do
describe '#real_time' do
context 'without timings' do
it 'returns 0.0' do
- expect(method_call.real_time_seconds).to eq(0.0)
+ expect(method_call.real_time).to eq(0.0)
end
end
@@ -140,7 +142,7 @@ describe Gitlab::Metrics::MethodCall do
it 'returns the total real time' do
method_call.measure { 'foo' }
- expect(method_call.real_time_seconds >= 0.0).to be(true)
+ expect(method_call.real_time >= 0.0).to be(true)
end
end
end
diff --git a/spec/lib/gitlab/metrics/system_spec.rb b/spec/lib/gitlab/metrics/system_spec.rb
index ea3bd00970e..14afcdf5daa 100644
--- a/spec/lib/gitlab/metrics/system_spec.rb
+++ b/spec/lib/gitlab/metrics/system_spec.rb
@@ -29,13 +29,13 @@ describe Gitlab::Metrics::System do
describe '.cpu_time' do
it 'returns a Fixnum' do
- expect(described_class.cpu_time).to be_an(Integer)
+ expect(described_class.cpu_time).to be_an(Float)
end
end
describe '.real_time' do
it 'returns a Fixnum' do
- expect(described_class.real_time).to be_an(Integer)
+ expect(described_class.real_time).to be_an(Float)
end
end