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

83 lines
2.2 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
2020-10-24 23:57:45 +05:30
unless event.payload[:hit]
current_transaction.increment(:gitlab_cache_misses_total, 1) do
docstring 'Cache read miss'
end
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
2020-10-24 23:57:45 +05:30
current_transaction.increment(:gitlab_transaction_cache_read_hit_count_total, 1)
2016-08-24 12:49:21 +05:30
end
def cache_generate(event)
return unless current_transaction
2020-10-24 23:57:45 +05:30
current_transaction.increment(:gitlab_cache_misses_total, 1) do
docstring 'Cache read miss'
end
current_transaction.increment(:gitlab_transaction_cache_read_miss_count_total, 1)
2016-08-24 12:49:21 +05:30
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
2020-10-24 23:57:45 +05:30
labels = { operation: key }
current_transaction.increment(:gitlab_cache_operations_total, 1, labels) do
docstring 'Cache operations'
label_keys labels.keys
end
metric_cache_operation_duration_seconds.observe(labels, duration / 1000.0)
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-07-07 11:18:12 +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',
2019-10-12 21:52:04 +05:30
{},
[0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0]
2018-03-17 18:26:18 +05:30
)
end
2016-06-02 11:05:42 +05:30
end
end
end
end