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
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def cache_read_multi(event)
|
2023-05-27 22:25:52 +05:30
|
|
|
observe(:read_multi, event)
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
return unless current_transaction
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
labels = { store: event.payload[:store].split('::').last }
|
|
|
|
current_transaction.observe(:gitlab_cache_read_multikey_count, event.payload[:key].size, labels) do
|
2023-03-04 22:38:38 +05:30
|
|
|
buckets [10, 50, 100, 1000]
|
|
|
|
docstring 'Number of keys for mget in read_multi/fetch_multi'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def cache_read(event)
|
2023-05-27 22:25:52 +05:30
|
|
|
observe(:read, event)
|
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)
|
2023-05-27 22:25:52 +05:30
|
|
|
observe(:write, event)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def cache_delete(event)
|
2023-05-27 22:25:52 +05:30
|
|
|
observe(:delete, event)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def cache_exist?(event)
|
2023-05-27 22:25:52 +05:30
|
|
|
observe(:exists, event)
|
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
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def observe(key, event)
|
2016-06-02 11:05:42 +05:30
|
|
|
return unless current_transaction
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
labels = { operation: key, store: event.payload[:store].split('::').last }
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
current_transaction.increment(:gitlab_cache_operations_total, 1, labels) do
|
|
|
|
docstring 'Cache operations'
|
|
|
|
label_keys labels.keys
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
metric_cache_operation_duration_seconds.observe(labels, event.duration / 1000.0)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_transaction
|
2021-12-11 22:18:48 +05:30
|
|
|
::Gitlab::Metrics::WebTransaction.current
|
2016-06-02 11:05:42 +05:30
|
|
|
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
|
|
|
{},
|
2023-06-20 00:43:36 +05:30
|
|
|
Gitlab::Instrumentation::Redis::QUERY_TIME_BUCKETS
|
2018-03-17 18:26:18 +05:30
|
|
|
)
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|