2023-01-13 00:05:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Instrumentation for cache efficiency metrics
|
|
|
|
module Gitlab
|
|
|
|
module Cache
|
|
|
|
class Metrics
|
|
|
|
DEFAULT_BUCKETS = [0, 1, 5].freeze
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def initialize(cache_metadata)
|
|
|
|
@cache_metadata = cache_metadata
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Increase cache hit counter
|
|
|
|
#
|
|
|
|
def increment_cache_hit
|
|
|
|
counter.increment(labels.merge(cache_hit: true))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Increase cache miss counter
|
|
|
|
#
|
|
|
|
def increment_cache_miss
|
|
|
|
counter.increment(labels.merge(cache_hit: false))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Measure the duration of cacheable action
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# observe_cache_generation do
|
|
|
|
# cacheable_action
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
def observe_cache_generation(&block)
|
|
|
|
real_start = Gitlab::Metrics::System.monotonic_time
|
|
|
|
|
|
|
|
value = yield
|
|
|
|
|
|
|
|
histogram.observe({}, Gitlab::Metrics::System.monotonic_time - real_start)
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
attr_reader :cache_metadata
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
def counter
|
|
|
|
@counter ||= Gitlab::Metrics.counter(:redis_hit_miss_operations_total, "Hit/miss Redis cache counter")
|
|
|
|
end
|
|
|
|
|
|
|
|
def histogram
|
|
|
|
@histogram ||= Gitlab::Metrics.histogram(
|
|
|
|
:redis_cache_generation_duration_seconds,
|
|
|
|
'Duration of Redis cache generation',
|
|
|
|
labels,
|
|
|
|
DEFAULT_BUCKETS
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def labels
|
|
|
|
@labels ||= {
|
2023-04-23 21:23:45 +05:30
|
|
|
caller_id: cache_metadata.caller_id,
|
|
|
|
cache_identifier: cache_metadata.cache_identifier,
|
|
|
|
feature_category: cache_metadata.feature_category,
|
|
|
|
backing_resource: cache_metadata.backing_resource
|
2023-01-13 00:05:48 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|