diff options
Diffstat (limited to 'lib/gitlab/usage/metrics')
11 files changed, 125 insertions, 39 deletions
diff --git a/lib/gitlab/usage/metrics/aggregates.rb b/lib/gitlab/usage/metrics/aggregates.rb new file mode 100644 index 00000000000..a32c413dba8 --- /dev/null +++ b/lib/gitlab/usage/metrics/aggregates.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Gitlab + module Usage + module Metrics + module Aggregates + UNION_OF_AGGREGATED_METRICS = 'OR' + INTERSECTION_OF_AGGREGATED_METRICS = 'AND' + ALLOWED_METRICS_AGGREGATIONS = [UNION_OF_AGGREGATED_METRICS, INTERSECTION_OF_AGGREGATED_METRICS].freeze + AGGREGATED_METRICS_PATH = Rails.root.join('config/metrics/aggregates/*.yml') + AggregatedMetricError = Class.new(StandardError) + UnknownAggregationOperator = Class.new(AggregatedMetricError) + UnknownAggregationSource = Class.new(AggregatedMetricError) + DisallowedAggregationTimeFrame = Class.new(AggregatedMetricError) + + DATABASE_SOURCE = 'database' + REDIS_SOURCE = 'redis' + + SOURCES = { + DATABASE_SOURCE => Sources::PostgresHll, + REDIS_SOURCE => Sources::RedisHll + }.freeze + end + end + end +end diff --git a/lib/gitlab/usage/metrics/aggregates/aggregate.rb b/lib/gitlab/usage/metrics/aggregates/aggregate.rb index 3ec06fba5d1..2545a505984 100644 --- a/lib/gitlab/usage/metrics/aggregates/aggregate.rb +++ b/lib/gitlab/usage/metrics/aggregates/aggregate.rb @@ -4,23 +4,6 @@ module Gitlab module Usage module Metrics module Aggregates - UNION_OF_AGGREGATED_METRICS = 'OR' - INTERSECTION_OF_AGGREGATED_METRICS = 'AND' - ALLOWED_METRICS_AGGREGATIONS = [UNION_OF_AGGREGATED_METRICS, INTERSECTION_OF_AGGREGATED_METRICS].freeze - AGGREGATED_METRICS_PATH = Rails.root.join('config/metrics/aggregates/*.yml') - AggregatedMetricError = Class.new(StandardError) - UnknownAggregationOperator = Class.new(AggregatedMetricError) - UnknownAggregationSource = Class.new(AggregatedMetricError) - DisallowedAggregationTimeFrame = Class.new(AggregatedMetricError) - - DATABASE_SOURCE = 'database' - REDIS_SOURCE = 'redis' - - SOURCES = { - DATABASE_SOURCE => Sources::PostgresHll, - REDIS_SOURCE => Sources::RedisHll - }.freeze - class Aggregate include Gitlab::Usage::TimeFrame diff --git a/lib/gitlab/usage/metrics/aggregates/sources.rb b/lib/gitlab/usage/metrics/aggregates/sources.rb new file mode 100644 index 00000000000..f782a64f3b5 --- /dev/null +++ b/lib/gitlab/usage/metrics/aggregates/sources.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Gitlab + module Usage + module Metrics + module Aggregates + module Sources + UnionNotAvailable = Class.new(AggregatedMetricError) + end + end + end + end +end diff --git a/lib/gitlab/usage/metrics/aggregates/sources/redis_hll.rb b/lib/gitlab/usage/metrics/aggregates/sources/redis_hll.rb index 009b8e62543..1bdf3a7f9d8 100644 --- a/lib/gitlab/usage/metrics/aggregates/sources/redis_hll.rb +++ b/lib/gitlab/usage/metrics/aggregates/sources/redis_hll.rb @@ -5,8 +5,6 @@ module Gitlab module Metrics module Aggregates module Sources - UnionNotAvailable = Class.new(AggregatedMetricError) - class RedisHll extend Calculations::Intersection def self.calculate_metrics_union(metric_names:, start_date:, end_date:, recorded_at: nil) diff --git a/lib/gitlab/usage/metrics/instrumentations/base_metric.rb b/lib/gitlab/usage/metrics/instrumentations/base_metric.rb index 7b5bee3f8bd..a264f9484f3 100644 --- a/lib/gitlab/usage/metrics/instrumentations/base_metric.rb +++ b/lib/gitlab/usage/metrics/instrumentations/base_metric.rb @@ -15,6 +15,10 @@ module Gitlab @time_frame = time_frame @options = options end + + def instrumentation + value + end end end end diff --git a/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric.rb b/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric.rb index dd1f9948815..ee51180973c 100644 --- a/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric.rb +++ b/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric.rb @@ -5,8 +5,8 @@ module Gitlab module Metrics module Instrumentations class CollectedDataCategoriesMetric < GenericMetric - def value - ::ServicePing::PermitDataCategoriesService.new.execute + value do + ::ServicePing::PermitDataCategoriesService.new.execute.to_a end end end diff --git a/lib/gitlab/usage/metrics/instrumentations/database_metric.rb b/lib/gitlab/usage/metrics/instrumentations/database_metric.rb index 7b3a545185b..d7fc798ebe2 100644 --- a/lib/gitlab/usage/metrics/instrumentations/database_metric.rb +++ b/lib/gitlab/usage/metrics/instrumentations/database_metric.rb @@ -33,16 +33,17 @@ module Gitlab @metric_relation = block end - def operation(symbol, column: nil) + def operation(symbol, column: nil, &block) @metric_operation = symbol @column = column + @metric_operation_block = block if block_given? end def cache_start_and_finish_as(cache_key) @cache_key = cache_key end - attr_reader :metric_operation, :metric_relation, :metric_start, :metric_finish, :column, :cache_key + attr_reader :metric_operation, :metric_relation, :metric_start, :metric_finish, :metric_operation_block, :column, :cache_key end def value @@ -52,13 +53,18 @@ module Gitlab .call(relation, self.class.column, start: start, - finish: finish) + finish: finish, + &self.class.metric_operation_block) end def to_sql Gitlab::Usage::Metrics::Query.for(self.class.metric_operation, relation, self.class.column) end + def instrumentation + to_sql + end + def suggested_name Gitlab::Usage::Metrics::NameSuggestion.for( self.class.metric_operation, diff --git a/lib/gitlab/usage/metrics/instrumentations/generic_metric.rb b/lib/gitlab/usage/metrics/instrumentations/generic_metric.rb index 1849773e33d..0f4b903b99c 100644 --- a/lib/gitlab/usage/metrics/instrumentations/generic_metric.rb +++ b/lib/gitlab/usage/metrics/instrumentations/generic_metric.rb @@ -12,27 +12,35 @@ module Gitlab # Gitlab::CurrentSettings.uuid # end # end + FALLBACK = -1 + class << self - attr_reader :metric_operation - @metric_operation = :alt + attr_reader :metric_value + + def fallback(custom_fallback = FALLBACK) + return @metric_fallback if defined?(@metric_fallback) + + @metric_fallback = custom_fallback + end def value(&block) @metric_value = block end + end - attr_reader :metric_value + def initialize(time_frame: 'none', options: {}) + @time_frame = time_frame + @options = options end def value - alt_usage_data do + alt_usage_data(fallback: self.class.fallback) do self.class.metric_value.call end end def suggested_name - Gitlab::Usage::Metrics::NameSuggestion.for( - self.class.metric_operation - ) + Gitlab::Usage::Metrics::NameSuggestion.for(:alt) end end end diff --git a/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric.rb b/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric.rb index a36e612a1cb..bb27cca1bb9 100644 --- a/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric.rb +++ b/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric.rb @@ -12,11 +12,6 @@ module Gitlab # events: # - g_analytics_valuestream # end - class << self - attr_reader :metric_operation - @metric_operation = :redis - end - def initialize(time_frame:, options: {}) super @@ -36,9 +31,7 @@ module Gitlab end def suggested_name - Gitlab::Usage::Metrics::NameSuggestion.for( - self.class.metric_operation - ) + Gitlab::Usage::Metrics::NameSuggestion.for(:redis) end private diff --git a/lib/gitlab/usage/metrics/instrumentations/redis_metric.rb b/lib/gitlab/usage/metrics/instrumentations/redis_metric.rb new file mode 100644 index 00000000000..a25bad2436b --- /dev/null +++ b/lib/gitlab/usage/metrics/instrumentations/redis_metric.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module Gitlab + module Usage + module Metrics + module Instrumentations + # Usage example + # + # In metric YAML definition: + # + # instrumentation_class: RedisMetric + # options: + # event: pushes + # counter_class: SourceCodeCounter + # + class RedisMetric < BaseMetric + def initialize(time_frame:, options: {}) + super + + raise ArgumentError, "'event' option is required" unless metric_event.present? + raise ArgumentError, "'counter class' option is required" unless counter_class.present? + end + + def metric_event + options[:event] + end + + def counter_class_name + options[:counter_class] + end + + def counter_class + "Gitlab::UsageDataCounters::#{counter_class_name}".constantize + end + + def value + redis_usage_data do + counter_class.read(metric_event) + end + end + + def suggested_name + Gitlab::Usage::Metrics::NameSuggestion.for(:redis) + end + end + end + end + end +end diff --git a/lib/gitlab/usage/metrics/names_suggestions/generator.rb b/lib/gitlab/usage/metrics/names_suggestions/generator.rb index a669b43f395..b47dc5689d4 100644 --- a/lib/gitlab/usage/metrics/names_suggestions/generator.rb +++ b/lib/gitlab/usage/metrics/names_suggestions/generator.rb @@ -10,6 +10,12 @@ module Gitlab uncached_data.deep_stringify_keys.dig(*key_path.split('.')) end + def add_metric(metric, time_frame: 'none') + metric_class = "Gitlab::Usage::Metrics::Instrumentations::#{metric}".constantize + + metric_class.new(time_frame: time_frame).suggested_name + end + private def count(relation, column = nil, batch: true, batch_size: nil, start: nil, finish: nil) |