Commit 2e92c887 authored by alinamihaila's avatar alinamihaila

Create instance in add_metric method

parent 51ca20d9
......@@ -400,7 +400,7 @@ For example, we have the following instrumentation class:
You should add it to `usage_data.rb` as follows:
```ruby
boards: add_metric(Gitlab::Usage::Metrics::Instrumentations::CountBoardsMetric.new(time_frame: 'all')),
boards: add_metric('CountBoardsMetric', time_frame: 'all'),
```
### Why batch counting
......
......@@ -10,8 +10,10 @@ module Gitlab
uncached_data.deep_stringify_keys.dig(*key_path.split('.'))
end
def add_metric(metric)
metric.suggested_name
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
......
......@@ -72,8 +72,8 @@ module Gitlab
def license_usage_data
{
recorded_at: recorded_at,
uuid: add_metric(Gitlab::Usage::Metrics::Instrumentations::UuidMetric.new),
hostname: add_metric(Gitlab::Usage::Metrics::Instrumentations::HostnameMetric.new),
uuid: add_metric('UuidMetric'),
hostname: add_metric('HostnameMetric'),
version: alt_usage_data { Gitlab::VERSION },
installation_type: alt_usage_data { installation_type },
active_user_count: count(User.active),
......@@ -93,7 +93,7 @@ module Gitlab
{
counts: {
assignee_lists: count(List.assignee),
boards: add_metric(Gitlab::Usage::Metrics::Instrumentations::CountBoardsMetric.new(time_frame: 'all')),
boards: add_metric('CountBoardsMetric', time_frame: 'all'),
ci_builds: count(::Ci::Build),
ci_internal_pipelines: count(::Ci::Pipeline.internal),
ci_external_pipelines: count(::Ci::Pipeline.external),
......@@ -138,7 +138,7 @@ module Gitlab
in_review_folder: count(::Environment.in_review_folder),
grafana_integrated_projects: count(GrafanaIntegration.enabled),
groups: count(Group),
issues: add_metric(Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric.new(time_frame: 'all')),
issues: add_metric('CountIssuesMetric', time_frame: 'all'),
issues_created_from_gitlab_error_tracking_ui: count(SentryIssue),
issues_with_associated_zoom_link: count(ZoomMeeting.added_to_issue),
issues_using_zoom_quick_actions: distinct_count(ZoomMeeting, :issue_id),
......@@ -257,7 +257,7 @@ module Gitlab
ldap_encrypted_secrets_enabled: alt_usage_data(fallback: nil) { Gitlab::Auth::Ldap::Config.encrypted_secrets.active? },
operating_system: alt_usage_data(fallback: nil) { operating_system },
gitaly_apdex: alt_usage_data { gitaly_apdex },
collected_data_categories: add_metric(Gitlab::Usage::Metrics::Instrumentations::CollectedDataCategoriesMetric.new(time_frame: 'none'))
collected_data_categories: add_metric('CollectedDataCategoriesMetric', time_frame: 'none')
}
}
end
......@@ -646,7 +646,7 @@ module Gitlab
def usage_activity_by_stage_plan(time_period)
time_frame = time_period.present? ? '28d' : 'none'
{
issues: add_metric(Gitlab::Usage::Metrics::Instrumentations::CountUsersCreatingIssuesMetric.new(time_frame: time_frame)),
issues: add_metric('CountUsersCreatingIssuesMetric', time_frame: time_frame),
notes: distinct_count(::Note.where(time_period), :author_id),
projects: distinct_count(::Project.where(time_period), :creator_id),
todos: distinct_count(::Todo.where(time_period), :author_id),
......
......@@ -5,8 +5,10 @@ module Gitlab
SQL_METRIC_DEFAULT = -3
class << self
def add_metric(metric)
metric.instrumentation
def add_metric(metric, time_frame: 'none')
metric_class = "Gitlab::Usage::Metrics::Instrumentations::#{metric}".constantize
metric_class.new(time_frame: time_frame).instrumentation
end
def count(relation, column = nil, batch: true, batch_size: nil, start: nil, finish: nil)
......
......@@ -5,8 +5,10 @@ module Gitlab
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/41091
class UsageDataQueries < UsageData
class << self
def add_metric(metric)
metric.instrumentation
def add_metric(metric, time_frame: 'none')
metric_class = "Gitlab::Usage::Metrics::Instrumentations::#{metric}".constantize
metric_class.new(time_frame: time_frame).instrumentation
end
def count(relation, column = nil, *args, **kwargs)
......
......@@ -44,8 +44,10 @@ module Gitlab
DISTRIBUTED_HLL_FALLBACK = -2
MAX_BUCKET_SIZE = 100
def add_metric(metric)
metric.value
def add_metric(metric, time_frame: 'none')
metric_class = "Gitlab::Usage::Metrics::Instrumentations::#{metric}".constantize
metric_class.new(time_frame: time_frame).value
end
def count(relation, column = nil, batch: true, batch_size: nil, start: nil, finish: nil)
......
......@@ -17,10 +17,10 @@ RSpec.describe Gitlab::Usage::Metrics::NamesSuggestions::Generator do
end
describe '#add_metric' do
let(:metric) { double(:metric, suggested_name: 'counts_issues') }
let(:metric) {'CountIssuesMetric' }
it 'computes the metric value for given metric' do
expect(described_class.add_metric(metric)).to eq('counts_issues')
it 'computes the suggested name for given metric' do
expect(described_class.add_metric(metric)).to eq('count_issues')
end
end
......
......@@ -6,10 +6,10 @@ RSpec.describe Gitlab::UsageDataNonSqlMetrics do
let(:default_count) { Gitlab::UsageDataNonSqlMetrics::SQL_METRIC_DEFAULT }
describe '#add_metric' do
let(:metric) { double(:metric, instrumentation: 1) }
let(:metric) { 'UuidMetric' }
it 'computes the metric value for given metric' do
expect(described_class.add_metric(metric)).to eq(1)
expect(described_class.add_metric(metric)).to eq(Gitlab::CurrentSettings.uuid)
end
end
......
......@@ -8,10 +8,10 @@ RSpec.describe Gitlab::UsageDataQueries do
end
describe '#add_metric' do
let(:metric) { double(:metric, instrumentation: 'SELECT COUNT("users"."id") FROM "users"') }
let(:metric) { 'CountBoardsMetric' }
it 'computes the metric value for given metric' do
expect(described_class.add_metric(metric)).to eq('SELECT COUNT("users"."id") FROM "users"')
it 'builds the query for given metric' do
expect(described_class.add_metric(metric)).to eq('SELECT COUNT("boards"."id") FROM "boards"')
end
end
......
......@@ -6,10 +6,10 @@ RSpec.describe Gitlab::Utils::UsageData do
include Database::DatabaseHelpers
describe '#add_metric' do
let(:metric) { double(:metric, value: 1) }
let(:metric) { 'UuidMetric'}
it 'computes the metric value for given metric' do
expect(described_class.add_metric(metric)).to eq(1)
expect(described_class.add_metric(metric)).to eq(Gitlab::CurrentSettings.uuid)
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