Commit 6db3151f authored by Pawel Chojnacki's avatar Pawel Chojnacki

Introduce missing Action concept

parent c97dc61a
......@@ -4,26 +4,29 @@ module Gitlab
class MethodCall
attr_reader :real_time, :cpu_time, :call_count
# name - The full name of the method (including namespace) such as
# `User#sign_in`.
#
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_seconds,
'Method calls real duration',
{call_name: nil},
[1, 2, 5, 10, 20, 50, 100, 1000])
{ action: nil, call_name: nil },
[1000, 2000, 5000, 10000, 20000, 50000, 100000, 1000000]
)
end
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_seconds,
'Method calls cpu duration',
{call_name: nil},
[1, 2, 5, 10, 20, 50, 100, 1000])
{ action: nil, call_name: nil },
[1000, 2000, 5000, 10000, 20000, 50000, 100000, 1000000]
)
end
def initialize(name)
# name - The full name of the method (including namespace) such as
# `User#sign_in`.
#
def initialize(name, action)
@action = action
@name = name
@real_time = 0
@cpu_time = 0
......@@ -41,8 +44,8 @@ module Gitlab
@call_count += 1
if above_threshold?
self.class.call_real_duration_histogram.observe({ call_name: @name }, @real_time)
self.class.call_cpu_duration_histogram.observe({ call_name: @name }, @cpu_time)
self.class.call_real_duration_histogram.observe({ call_name: @name, action: @action }, @real_time)
self.class.call_cpu_duration_histogram.observe({ call_name: @name, action: @action }, @cpu_time)
end
retval
......
......@@ -8,14 +8,14 @@ module Gitlab
def self.metric_sql_duration_seconds
@metric_sql_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_sql_duration_seconds,
'SQL duration seconds',
'SQL time',
{},
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
)
end
def sql(event)
self.class.metric_sql_duration_secodnds.observe({}, event.duration/1000.0)
self.class.metric_sql_duration_secodnds.observe({}, event.duration / 1000.0)
return unless current_transaction
current_transaction.increment(:sql_duration, event.duration, false)
......
......@@ -6,54 +6,80 @@ module Gitlab
class RailsCache < ActiveSupport::Subscriber
attach_to :active_support
def self.metric_cache_duration_seconds
@metric_cache_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_cache_duration_seconds,
'Cache access time',
{ action: nil, operation: nil },
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
)
end
def self.metric_cache_read_hit_total
@metric_cache_read_hit_total ||= Gitlab::Metrics.counter(:gitlab_cache_read_hit_total, 'Cache read hit', { action: nil })
end
def self.metric_cache_read_miss_total
@metric_cache_read_miss_total ||= Gitlab::Metrics.counter(:gitlab_cache_read_miss_total, 'Cache read miss', { action: nil })
end
def cache_read(event)
increment(:cache_read, event.duration)
observe(:read, event.duration)
return unless current_transaction
return if event.payload[:super_operation] == :fetch
if event.payload[:hit]
self.class.metric_cache_read_hit_total.increment({ action: action })
current_transaction.increment(:cache_read_hit_count, 1)
else
self.class.metric_cache_read_miss_total.increment({ action: action })
current_transaction.increment(:cache_read_miss_count, 1)
end
end
def cache_write(event)
increment(:cache_write, event.duration)
observe(:write, event.duration)
end
def cache_delete(event)
increment(:cache_delete, event.duration)
observe(:delete, event.duration)
end
def cache_exist?(event)
increment(:cache_exists, event.duration)
observe(:exists, event.duration)
end
def cache_fetch_hit(event)
return unless current_transaction
self.class.metric_cache_read_hit_total.increment({ action: action })
current_transaction.increment(:cache_read_hit_count, 1)
end
def cache_generate(event)
return unless current_transaction
self.class.metric_cache_read_miss_total.increment({ action: action })
current_transaction.increment(:cache_read_miss_count, 1)
end
def increment(key, duration)
def observe(key, duration)
return unless current_transaction
current_transaction.increment(:cache_duration, duration)
current_transaction.increment(:cache_count, 1)
current_transaction.increment("#{key}_duration".to_sym, duration)
current_transaction.increment("#{key}_count".to_sym, 1)
metric_cache_duration_seconds.observe({ operation: key, action: action }, duration / 1000.1)
current_transaction.increment(:cache_duration, duration, false)
current_transaction.increment(:cache_count, 1, false)
current_transaction.increment("#{key}_duration".to_sym, duration, false)
current_transaction.increment("#{key}_count".to_sym, 1, false)
end
private
def action
current_transaction&.action
end
def current_transaction
Transaction.current
end
......
......@@ -43,8 +43,8 @@ module Gitlab
def self.metric_transaction_duration_seconds
@metric_transaction_duration_seconds ||= Gitlab::Metrics.histogram(
:gitlab_transaction_duration_seconds,
'Transaction duration seconds',
{},
'Transaction duration',
{ action: nil },
[0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
)
end
......@@ -53,7 +53,7 @@ module Gitlab
@metric_transaction_allocated_memory_bytes ||= Gitlab::Metrics.histogram(
:gitlab_transaction_allocated_memory_bytes,
'Transaction allocated memory bytes',
{},
{ action: nil },
[500000, 1000000, 2000000, 5000000, 10000000, 20000000, 100000000]
)
end
......@@ -69,8 +69,8 @@ module Gitlab
@memory_after = System.memory_usage
@finished_at = System.monotonic_time
Transaction.metric_transaction_duration_seconds.observe({}, duration * 1000)
Transaction.metric_transaction_allocated_memory_bytes.observe({}, allocated_memory / 2 ^ 20)
Transaction.metric_transaction_duration_seconds.observe({ action: action }, duration * 1000)
Transaction.metric_transaction_allocated_memory_bytes.observe({ action: action }, allocated_memory / 2 ^ 20)
Thread.current[THREAD_KEY] = nil
end
......@@ -94,7 +94,7 @@ module Gitlab
# Returns a MethodCall object for the given name.
def method_call_for(name)
unless method = @methods[name]
@methods[name] = method = MethodCall.new(name)
@methods[name] = method = MethodCall.new(name, action)
end
method
......
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