debian-mirror-gitlab/lib/gitlab/usage/metrics/instrumentations/redis_metric.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
1.6 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module Instrumentations
# Usage example
#
# In metric YAML definition:
#
# instrumentation_class: RedisMetric
# options:
# event: pushes
2022-10-11 01:57:18 +05:30
# prefix: source_code
2021-10-27 15:23:28 +05:30
#
class RedisMetric < BaseMetric
2022-10-11 01:57:18 +05:30
include Gitlab::UsageDataCounters::RedisCounter
USAGE_PREFIX = "USAGE_"
2022-11-25 23:54:43 +05:30
OPTIONS_PREFIX_KEY = :prefix
2022-10-11 01:57:18 +05:30
2022-11-25 23:54:43 +05:30
def initialize(metric_definition)
2021-10-27 15:23:28 +05:30
super
2022-11-25 23:54:43 +05:30
validate_options!
end
def validate_options!
2021-10-27 15:23:28 +05:30
raise ArgumentError, "'event' option is required" unless metric_event.present?
2022-11-25 23:54:43 +05:30
raise ArgumentError, "'prefix' option is required" unless options.has_key?(OPTIONS_PREFIX_KEY)
2021-10-27 15:23:28 +05:30
end
def metric_event
options[:event]
end
2022-10-11 01:57:18 +05:30
def prefix
2022-11-25 23:54:43 +05:30
options[OPTIONS_PREFIX_KEY]
2021-10-27 15:23:28 +05:30
end
2022-10-11 01:57:18 +05:30
def include_usage_prefix?
options.fetch(:include_usage_prefix, true)
2021-10-27 15:23:28 +05:30
end
def value
redis_usage_data do
2022-10-11 01:57:18 +05:30
total_count(redis_key)
2021-10-27 15:23:28 +05:30
end
end
def suggested_name
Gitlab::Usage::Metrics::NameSuggestion.for(:redis)
end
2022-10-11 01:57:18 +05:30
private
def redis_key
2022-11-25 23:54:43 +05:30
key = metric_event.dup
key.prepend("#{prefix}_") if prefix
2022-10-11 01:57:18 +05:30
key.prepend(USAGE_PREFIX) if include_usage_prefix?
2022-11-25 23:54:43 +05:30
key.upcase
2022-10-11 01:57:18 +05:30
end
2021-10-27 15:23:28 +05:30
end
end
end
end
end