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