Commit 57d2b83a authored by Sean McGivern's avatar Sean McGivern

Merge branch '322733-improve-metrics-definition-generator-for-redis-hll-counters' into 'master'

Add a separate metrics definition generator for Redis HLL counters

See merge request gitlab-org/gitlab!56367
parents a469c12f 124e4c4f
......@@ -30,8 +30,10 @@ tracking_files = [
'app/assets/javascripts/tracking.js',
'spec/frontend/tracking_spec.js',
'generator_templates/usage_metric_definition/metric_definition.yml',
'lib/generators/rails/usage_metric_definition_generator.rb',
'spec/lib/generators/usage_metric_definition_generator_spec.rb',
'lib/generators/gitlab/usage_metric_definition_generator.rb',
'lib/generators/gitlab/usage_metric_definition/redis_hll_generator.rb',
'spec/lib/generators/gitlab/usage_metric_definition_generator_spec.rb',
'spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb',
'config/metrics/schema.json'
]
......
......@@ -109,4 +109,12 @@ create ee/config/metrics/counts_7d/issues.yml
The [Redis HLL metrics](index.md#known-events-are-added-automatically-in-usage-data-payload) are added automatically to Usage Ping payload.
A YAML metric definition is required for each metric.
A YAML metric definition is required for each metric. A dedicated generator is provided to create metric definitions for Redis HLL events.
The generator takes `category` and `event` arguments, as the root key will be `redis_hll_counters`, and creates two metric definitions for weekly and monthly timeframes:
```shell
bundle exec rails generate gitlab:usage_metric_definition:redis_hll issues i_closed
create config/metrics/counts_7d/i_closed_weekly.yml
create config/metrics/counts_28d/i_closed_monthly.yml
```
# frozen_string_literal: true
require 'rails/generators'
require_relative '../usage_metric_definition_generator'
module Gitlab
module UsageMetricDefinition
class RedisHllGenerator < Rails::Generators::Base
desc 'Generates a metric definition .yml file with defaults for Redis HLL.'
argument :category, type: :string, desc: "Category name"
argument :event, type: :string, desc: "Event name"
def create_metrics
Gitlab::UsageMetricDefinitionGenerator.start(["#{key_path}_weekly", '--dir', '7d'])
Gitlab::UsageMetricDefinitionGenerator.start(["#{key_path}_monthly", '--dir', '28d'])
end
private
def key_path
"redis_hll_counters.#{category}.#{event}"
end
end
end
end
......@@ -106,7 +106,7 @@ module Gitlab
end
def metric_definitions
@definitions ||= Gitlab::Usage::MetricDefinition.definitions
@definitions ||= Gitlab::Usage::MetricDefinition.definitions(skip_validation: true)
end
def metric_definition_exists?
......
......@@ -49,7 +49,8 @@ module Gitlab
@paths ||= [Rails.root.join('config', 'metrics', '**', '*.yml')]
end
def definitions
def definitions(skip_validation: false)
@skip_validation = skip_validation
@definitions ||= load_all!
end
......@@ -95,7 +96,7 @@ module Gitlab
end
def skip_validation?
!!attributes[:skip_validation]
!!attributes[:skip_validation] || @skip_validation
end
end
end
......
# frozen_string_literal: true
require 'generator_helper'
RSpec.describe Gitlab::UsageMetricDefinition::RedisHllGenerator do
let(:category) { 'test_category' }
let(:event) { 'i_test_event' }
let(:args) { [category, event] }
let(:temp_dir) { Dir.mktmpdir }
# Interpolating to preload the class
# See https://github.com/rspec/rspec-mocks/issues/1079
before do
stub_const("#{Gitlab::UsageMetricDefinitionGenerator}::TOP_LEVEL_DIR", temp_dir)
# Stub Prometheus requests from Gitlab::Utils::UsageData
stub_request(:get, %r{^https?://::1:9090/-/ready})
.to_return(
status: 200,
body: [{}].to_json,
headers: { 'Content-Type' => 'application/json' }
)
stub_request(:get, %r{^https?://::1:9090/api/v1/query\?query=.*})
.to_return(
status: 200,
body: [{}].to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
it 'creates metric definition files' do
described_class.new(args).invoke_all
weekly_metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*i_test_event_weekly.yml')).first
monthly_metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_28d/*i_test_event_monthly.yml')).first
expect(YAML.safe_load(File.read(weekly_metric_definition_path))).to include("key_path" => "redis_hll_counters.test_category.i_test_event_weekly")
expect(YAML.safe_load(File.read(monthly_metric_definition_path))).to include("key_path" => "redis_hll_counters.test_category.i_test_event_monthly")
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment