debian-mirror-gitlab/lib/gitlab/metrics/subscribers/rails_cache.rb

86 lines
2.6 KiB
Ruby
Raw Normal View History

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