Commit 3cc28601 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Cleanup sampling code and fix bug with samplers running without sleep

parent b6d75b29
...@@ -43,7 +43,7 @@ module Gitlab ...@@ -43,7 +43,7 @@ module Gitlab
if thread if thread
thread.wakeup if thread.alive? thread.wakeup if thread.alive?
thread.join thread.join unless Thread.current == thread
@thread = nil @thread = nil
end end
end end
......
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
def self.call_real_duration_histogram def self.call_real_duration_histogram
@call_real_duration_histogram ||= Gitlab::Metrics.histogram(:gitlab_method_call_real_duration_milliseconds, @call_real_duration_histogram ||= Gitlab::Metrics.histogram(:gitlab_method_call_real_duration_milliseconds,
'Method calls real duration', 'Method calls real duration',
{}, {call_name: nil},
[1, 2, 5, 10, 20, 50, 100, 1000]) [1, 2, 5, 10, 20, 50, 100, 1000])
end end
...@@ -18,17 +18,16 @@ module Gitlab ...@@ -18,17 +18,16 @@ module Gitlab
def self.call_cpu_duration_histogram def self.call_cpu_duration_histogram
@call_duration_histogram ||= Gitlab::Metrics.histogram(:gitlab_method_call_cpu_duration_milliseconds, @call_duration_histogram ||= Gitlab::Metrics.histogram(:gitlab_method_call_cpu_duration_milliseconds,
'Method calls cpu duration', 'Method calls cpu duration',
{}, {call_name: nil},
[1, 2, 5, 10, 20, 50, 100, 1000]) [1, 2, 5, 10, 20, 50, 100, 1000])
end end
def initialize(name, tags = {}) def initialize(name)
@name = name @name = name
@real_time = 0 @real_time = 0
@cpu_time = 0 @cpu_time = 0
@call_count = 0 @call_count = 0
@tags = tags
end end
# Measures the real and CPU execution time of the supplied block. # Measures the real and CPU execution time of the supplied block.
...@@ -42,25 +41,13 @@ module Gitlab ...@@ -42,25 +41,13 @@ module Gitlab
@call_count += 1 @call_count += 1
if above_threshold? if above_threshold?
self.class.call_real_duration_histogram.observe(labels, @real_time) self.class.call_real_duration_histogram.observe({ call_name: @name }, @real_time)
self.class.call_cpu_duration_histogram.observe(labels, @cpu_time) self.class.call_cpu_duration_histogram.observe({ call_name: @name }, @cpu_time)
end end
retval retval
end end
def labels
@labels ||= @tags.merge(source_label).merge({ call_name: @name })
end
def source_label
if Sidekiq.server?
{ source: 'sidekiq' }
else
{ source: 'rails' }
end
end
# Returns a Metric instance of the current method call. # Returns a Metric instance of the current method call.
def to_metric def to_metric
Metric.new( Metric.new(
......
require 'logger' require 'logger'
module Gitlab module Gitlab
module Metrics module Metrics
module Samplers module Samplers
...@@ -43,11 +44,14 @@ module Gitlab ...@@ -43,11 +44,14 @@ module Gitlab
private private
attr_reader :running
def start_working def start_working
@running = true @running = true
sleep(sleep_interval) sleep(sleep_interval)
while running while running
safe_sample safe_sample
sleep(sleep_interval)
end end
end end
......
...@@ -32,7 +32,7 @@ module Gitlab ...@@ -32,7 +32,7 @@ module Gitlab
def init_metrics def init_metrics
metrics = {} metrics = {}
metrics[:sampler_duration] = Gitlab::Metrics.histogram(with_prefix(:sampler_duration, :seconds), 'Sampler time', source_label) metrics[:sampler_duration] = Gitlab::Metrics.histogram(with_prefix(:sampler_duration, :seconds), 'Sampler time', {})
metrics[:total_time] = Gitlab::Metrics.gauge(with_prefix(:gc, :time_total), 'Total GC time', labels, :livesum) metrics[:total_time] = Gitlab::Metrics.gauge(with_prefix(:gc, :time_total), 'Total GC time', labels, :livesum)
GC.stat.keys.each do |key| GC.stat.keys.each do |key|
metrics[key] = Gitlab::Metrics.gauge(with_prefix(:gc, key), to_doc_string(key), labels, :livesum) metrics[key] = Gitlab::Metrics.gauge(with_prefix(:gc, key), to_doc_string(key), labels, :livesum)
......
...@@ -43,18 +43,18 @@ module Gitlab ...@@ -43,18 +43,18 @@ module Gitlab
def self.metric_transaction_duration_milliseconds def self.metric_transaction_duration_milliseconds
@metrics_transaction_duration_milliseconds ||= Gitlab::Metrics.histogram( @metrics_transaction_duration_milliseconds ||= Gitlab::Metrics.histogram(
:gitlab_transaction_duration_milliseconds, :gitlab_transaction_duration_milliseconds,
'Method duration milliseconds', 'Transaction duration milliseconds',
{}, {},
[1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000] [1, 2, 5, 10, 20, 50, 100, 500, 10000]
) )
end end
def self.metric_transaction_allocated_memory_bytes def self.metric_transaction_allocated_memory_megabytes
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram( @metric_transaction_allocated_memory_megabytes ||= Gitlab::Metrics.histogram(
:gitlab_transaction_allocated_memory_bytes, :gitlab_transaction_allocated_memory_megabytes,
'Method duration milliseconds', 'Transaction allocated memory bytes',
{}, {},
[1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 5000000] [1, 2, 5, 10, 20, 100]
) )
end end
...@@ -69,8 +69,8 @@ module Gitlab ...@@ -69,8 +69,8 @@ module Gitlab
@memory_after = System.memory_usage @memory_after = System.memory_usage
@finished_at = System.monotonic_time @finished_at = System.monotonic_time
self.class.metric_transaction_duration_milliseconds.observe({}, duration) Transaction.metric_transaction_duration_milliseconds.observe({}, duration)
self.class.metric_transaction_allocated_memory_bytes.observe({}, allocated_memory) Transaction.metric_transaction_allocated_memory_megabytes.observe({}, allocated_memory)
Thread.current[THREAD_KEY] = nil Thread.current[THREAD_KEY] = nil
end end
......
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