2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-08 11:54:52 -04:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Subscribers
|
|
|
|
# Class for tracking the total time spent in Rails cache calls
|
2016-06-29 11:03:20 -04:00
|
|
|
# http://guides.rubyonrails.org/active_support_instrumentation.html
|
2016-04-08 11:54:52 -04:00
|
|
|
class RailsCache < ActiveSupport::Subscriber
|
|
|
|
attach_to :active_support
|
|
|
|
|
|
|
|
def cache_read(event)
|
2017-09-05 21:02:08 -04:00
|
|
|
observe(:read, event.duration)
|
2016-06-29 11:03:20 -04:00
|
|
|
|
|
|
|
return unless current_transaction
|
|
|
|
return if event.payload[:super_operation] == :fetch
|
|
|
|
|
|
|
|
if event.payload[:hit]
|
2017-09-06 06:11:53 -04:00
|
|
|
current_transaction.increment(:cache_read_hit_count, 1, false)
|
2016-06-29 11:03:20 -04:00
|
|
|
else
|
2017-10-24 04:57:29 -04:00
|
|
|
metric_cache_misses_total.increment(current_transaction.labels)
|
2017-09-06 06:11:53 -04:00
|
|
|
current_transaction.increment(:cache_read_miss_count, 1, false)
|
2016-06-29 11:03:20 -04:00
|
|
|
end
|
2016-04-08 11:54:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_write(event)
|
2017-09-05 21:02:08 -04:00
|
|
|
observe(:write, event.duration)
|
2016-04-08 11:54:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_delete(event)
|
2017-09-05 21:02:08 -04:00
|
|
|
observe(:delete, event.duration)
|
2016-04-08 11:54:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_exist?(event)
|
2017-09-05 21:02:08 -04:00
|
|
|
observe(:exists, event.duration)
|
2016-04-08 11:54:52 -04:00
|
|
|
end
|
|
|
|
|
2016-06-29 10:05:57 -04:00
|
|
|
def cache_fetch_hit(event)
|
|
|
|
return unless current_transaction
|
|
|
|
|
2016-06-29 11:03:20 -04:00
|
|
|
current_transaction.increment(:cache_read_hit_count, 1)
|
2016-06-29 10:05:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_generate(event)
|
|
|
|
return unless current_transaction
|
|
|
|
|
2017-10-24 04:57:29 -04:00
|
|
|
metric_cache_misses_total.increment(current_transaction.labels)
|
2016-06-29 11:03:20 -04:00
|
|
|
current_transaction.increment(:cache_read_miss_count, 1)
|
2016-06-29 10:05:57 -04:00
|
|
|
end
|
|
|
|
|
2017-09-05 21:02:08 -04:00
|
|
|
def observe(key, duration)
|
2016-04-08 11:54:52 -04:00
|
|
|
return unless current_transaction
|
|
|
|
|
2017-10-24 04:57:29 -04:00
|
|
|
metric_cache_operation_duration_seconds.observe(current_transaction.labels.merge({ operation: key }), duration / 1000.0)
|
2017-09-05 21:02:08 -04:00
|
|
|
current_transaction.increment(:cache_duration, duration, false)
|
|
|
|
current_transaction.increment(:cache_count, 1, false)
|
2017-09-06 06:11:53 -04:00
|
|
|
current_transaction.increment("cache_#{key}_duration".to_sym, duration, false)
|
|
|
|
current_transaction.increment("cache_#{key}_count".to_sym, 1, false)
|
2016-04-08 11:54:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_transaction
|
|
|
|
Transaction.current
|
|
|
|
end
|
2017-10-24 03:09:13 -04:00
|
|
|
|
|
|
|
def metric_cache_operation_duration_seconds
|
|
|
|
@metric_cache_operation_duration_seconds ||= Gitlab::Metrics.histogram(
|
|
|
|
:gitlab_cache_operation_duration_seconds,
|
|
|
|
'Cache access time',
|
|
|
|
Transaction::BASE_LABELS.merge({ action: nil }),
|
2017-12-12 09:11:34 -05:00
|
|
|
[0.001, 0.01, 0.1, 1, 10]
|
2017-10-24 03:09:13 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def metric_cache_misses_total
|
|
|
|
@metric_cache_misses_total ||= Gitlab::Metrics.counter(
|
|
|
|
:gitlab_cache_misses_total,
|
|
|
|
'Cache read miss',
|
|
|
|
Transaction::BASE_LABELS
|
|
|
|
)
|
|
|
|
end
|
2016-04-08 11:54:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|