Skip to content

Add options arguments to Metric YAML defintion for filtering data in instrumentation class

We could have an extra attribute to Usage Data metrics definitions.

In this attribute we can have informations necessary for collecting the data for Usage Ping

An example could be for data we collect from redis.

Metric definition for i_code_review_mr_diffs_weekly metric

---
key_path: redis_hll_counters.code_review.i_code_review_mr_diffs_weekly
description: Count of unique merge requests per week with diffs viewed
product_section: dev
product_stage: create
product_group: group::code review
product_category: code_review
value_type: number
status: data_available
time_frame: 7d
data_source: redis_hll
instrumentation_class: Gitlab::Usage::Metrics::Instrumentations::UsersCreatingMergerequestsDiffs
distribution:
- ce
- ee
tier:
 - free
 - premium
 - ultimate

Instrumentation class

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        class UsersCreatingMergerequestsDiffs < RedisHLLMetric
          event_names :i_code_review_mr_diffs
        end
      end
    end
  end
end

The event names could be set in the Metric defintion itself, and later used when collecting the data. This way we will hove only one class to instrumment all redis hll metrics

# i_code_review_mr_diffs_weekly.yml
...
options: 
 events: [i_code_review_mr_diffs]
# base_metric.rb
module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        class BaseMetric
          include Gitlab::Utils::UsageData
 
          attr_reader :time_constraints
          attr_reader :extra

          def initialize(time_constraints:, options: {})
            @time_constraints = time_constraints
            @options = options
          end


          class << self
            def value(&block)
              @metric_value = block
            end

            attr_reader :metric_value
          end

          def value
            alt_usage_data do
              self.class.metric_value.call
            end
          end
        end
      end
    end
  end
end

# redis_hll_metric.rb

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        class RedisHLLMetric < BaseMetric
          def metric_events
            options[:events]
          end
          
          def value
            redis_usage_data do
              Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(**redis_hll_time_constraints.merge(event_names: self.class.mentric_events))
            end
          end
        end
      end
    end
  end
end

Note: Untested code

Edited by Alina Mihaila