From 58cefb43c4e1ae7dca325f84aed092aac4d654ac Mon Sep 17 00:00:00 2001
From: Eduardo Bonet <ebonet@gitlab.com>
Date: Wed, 1 Feb 2023 14:43:52 +0100
Subject: [PATCH 1/7] Adds initial metrics for Ml Experiment Tracking

Early metrics track number of entities create, but no events

Changelog: added
---
 .../20230201120001_count_ml_candidates.yml    | 23 +++++++++++++++++++
 .../20230201120002_count_ml_experiments.yml   | 23 +++++++++++++++++++
 ...0003_count_projects_with_ml_candidates.yml | 23 +++++++++++++++++++
 ...1120004_count_users_with_ml_candidates.yml | 23 +++++++++++++++++++
 ...005_count_projects_with_ml_experiments.yml | 23 +++++++++++++++++++
 .../count_ml_candidates_metric.rb             | 15 ++++++++++++
 .../count_ml_experiments_metric.rb            | 15 ++++++++++++
 ...ount_projects_with_ml_candidates_metric.rb | 15 ++++++++++++
 ...unt_projects_with_ml_experiments_metric.rb | 15 ++++++++++++
 .../count_users_with_ml_candidates_metric.rb  | 15 ++++++++++++
 .../count_ml_candidates_metric_spec.rb        | 12 ++++++++++
 .../count_ml_experiments_metric_spec.rb       | 12 ++++++++++
 ...projects_with_ml_candidates_metric_spec.rb | 18 +++++++++++++++
 ...rojects_with_ml_experiments_metric_spec.rb | 15 ++++++++++++
 ...nt_users_with_ml_candidates_metric_spec.rb | 14 +++++++++++
 15 files changed, 261 insertions(+)
 create mode 100644 config/metrics/counts_all/20230201120001_count_ml_candidates.yml
 create mode 100644 config/metrics/counts_all/20230201120002_count_ml_experiments.yml
 create mode 100644 config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
 create mode 100644 config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
 create mode 100644 config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
 create mode 100644 lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric.rb
 create mode 100644 lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric.rb
 create mode 100644 lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb
 create mode 100644 lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric.rb
 create mode 100644 lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric.rb
 create mode 100644 spec/lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric_spec.rb
 create mode 100644 spec/lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric_spec.rb
 create mode 100644 spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
 create mode 100644 spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
 create mode 100644 spec/lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric_spec.rb

diff --git a/config/metrics/counts_all/20230201120001_count_ml_candidates.yml b/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
new file mode 100644
index 0000000000000000..005862f1bfdc9c5a
--- /dev/null
+++ b/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
@@ -0,0 +1,23 @@
+---
+key_path: counts.count_ml_candidates
+description: Count of created Ml Candidates
+product_section: dev
+product_stage: mlops
+product_group: mlops
+product_category: mlops
+value_type: number
+status: active
+milestone: "15.9"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
+time_frame: all
+data_source: database
+data_category: optional
+instrumentation_class: CountMlCandidates
+performance_indicator_type: []
+distribution:
+- ce
+- ee
+tier:
+- free
+- premium
+- ultimate
diff --git a/config/metrics/counts_all/20230201120002_count_ml_experiments.yml b/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
new file mode 100644
index 0000000000000000..8a6f6100ea38def4
--- /dev/null
+++ b/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
@@ -0,0 +1,23 @@
+---
+key_path: counts.count_ml_experiments
+description: Count of created Ml Experiments
+product_section: dev
+product_stage: mlops
+product_group: mlops
+product_category: mlops
+value_type: number
+status: active
+milestone: "15.9"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
+time_frame: all
+data_source: database
+data_category: optional
+instrumentation_class: CountMlExperiments
+performance_indicator_type: []
+distribution:
+- ce
+- ee
+tier:
+- free
+- premium
+- ultimate
diff --git a/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml b/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
new file mode 100644
index 0000000000000000..fd839af4eabf6884
--- /dev/null
+++ b/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
@@ -0,0 +1,23 @@
+---
+key_path: counts.count_projects_with_ml_candidates
+description: Count of unique projects that contain at least one Ml Candidate
+product_section: dev
+product_stage: mlops
+product_group: mlops
+product_category: mlops
+value_type: number
+status: active
+milestone: "15.9"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
+time_frame: all
+data_source: database
+data_category: optional
+instrumentation_class: CountProjectsWithMlCandidates
+performance_indicator_type: []
+distribution:
+- ce
+- ee
+tier:
+- free
+- premium
+- ultimate
diff --git a/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml b/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
new file mode 100644
index 0000000000000000..8781e738ef70bda8
--- /dev/null
+++ b/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
@@ -0,0 +1,23 @@
+---
+key_path: counts.count_users_with_ml_candidates
+description: Count of unique users that have created at least one Ml Candidate
+product_section: dev
+product_stage: mlops
+product_group: mlops
+product_category: mlops
+value_type: number
+status: active
+milestone: "15.9"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
+time_frame: all
+data_source: database
+data_category: optional
+instrumentation_class: CountUsersWithMlCandidates
+performance_indicator_type: []
+distribution:
+- ce
+- ee
+tier:
+- free
+- premium
+- ultimate
diff --git a/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml b/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
new file mode 100644
index 0000000000000000..57556c71ba31553f
--- /dev/null
+++ b/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
@@ -0,0 +1,23 @@
+---
+key_path: counts.count_projects_with_ml_experiments
+description: Count of unique projects that contain at least one Ml Experiment
+product_section: dev
+product_stage: mlops
+product_group: mlops
+product_category: mlops
+value_type: number
+status: active
+milestone: "15.9"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
+time_frame: all
+data_source: database
+data_category: optional
+instrumentation_class: CountProjectsWithMlExperiments
+performance_indicator_type: []
+distribution:
+- ce
+- ee
+tier:
+- free
+- premium
+- ultimate
diff --git a/lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric.rb b/lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric.rb
new file mode 100644
index 0000000000000000..e6e547c155c6e931
--- /dev/null
+++ b/lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Usage
+    module Metrics
+      module Instrumentations
+        class CountMlCandidatesMetric < DatabaseMetric
+          operation :count
+
+          relation { Ml::Candidate }
+        end
+      end
+    end
+  end
+end
diff --git a/lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric.rb b/lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric.rb
new file mode 100644
index 0000000000000000..3e589f28df1d19a8
--- /dev/null
+++ b/lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Usage
+    module Metrics
+      module Instrumentations
+        class CountMlExperimentsMetric < DatabaseMetric
+          operation :count
+
+          relation { Ml::Experiment }
+        end
+      end
+    end
+  end
+end
diff --git a/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb b/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb
new file mode 100644
index 0000000000000000..bdaeb87561de567d
--- /dev/null
+++ b/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Usage
+    module Metrics
+      module Instrumentations
+        class CountProjectsWithMlCandidatesMetric < DatabaseMetric
+          operation :distinct_count, column: 'ml_experiments.project_id'
+
+          relation { Ml::Candidate.joins(:experiment) }
+        end
+      end
+    end
+  end
+end
diff --git a/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric.rb b/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric.rb
new file mode 100644
index 0000000000000000..d575163b6b593aa6
--- /dev/null
+++ b/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Usage
+    module Metrics
+      module Instrumentations
+        class CountProjectsWithMlExperimentsMetric < DatabaseMetric
+          operation :distinct_count, column: :project_id
+
+          relation { Ml::Experiment }
+        end
+      end
+    end
+  end
+end
diff --git a/lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric.rb b/lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric.rb
new file mode 100644
index 0000000000000000..0ec7173e09692b68
--- /dev/null
+++ b/lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Usage
+    module Metrics
+      module Instrumentations
+        class CountUsersWithMlCandidatesMetric < DatabaseMetric
+          operation :distinct_count, column: :user_id
+
+          relation { Ml::Candidate }
+        end
+      end
+    end
+  end
+end
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric_spec.rb
new file mode 100644
index 0000000000000000..7d7788737df030d3
--- /dev/null
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_ml_candidates_metric_spec.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountMlCandidatesMetric, feature_category: :mlops do
+  let_it_be(:candidate) { create(:ml_candidates) }
+
+  let(:expected_value) { 1 }
+  let(:expected_query) { 'SELECT COUNT("ml_candidates"."id") FROM "ml_candidates"' }
+
+  it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' }
+end
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric_spec.rb
new file mode 100644
index 0000000000000000..887496ce39f229d2
--- /dev/null
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_ml_experiments_metric_spec.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountMlExperimentsMetric, feature_category: :mlops do
+  let_it_be(:candidate) { create(:ml_experiments) }
+
+  let(:expected_value) { 1 }
+  let(:expected_query) { 'SELECT COUNT("ml_experiments"."id") FROM "ml_experiments"' }
+
+  it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' }
+end
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
new file mode 100644
index 0000000000000000..36da073d22fa63ff
--- /dev/null
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountProjectsWithMlCandidatesMetric,
+feature_category: :mlops do
+  let_it_be(:project_without_candidates) { create(:project, :repository) }
+  let_it_be(:candidate) { create(:ml_candidates) }
+  let_it_be(:another_candidate) { create(:ml_candidates, experiment: candidate.experiment) }
+
+  let(:expected_value) { 1 }
+  let(:expected_query) do
+    'SELECT COUNT(DISTINCT "ml_experiments"."project_id") FROM "ml_candidates" INNER JOIN "ml_experiments" ON ' \
+      '"ml_experiments"."id" = "ml_candidates"."experiment_id"'
+  end
+
+  it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' }
+end
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
new file mode 100644
index 0000000000000000..043d3fbc13613082
--- /dev/null
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountProjectsWithMlExperimentsMetric,
+feature_category: :mlops do
+  let_it_be(:project_without_experiment) { create(:project, :repository) }
+  let_it_be(:experiment) { create(:ml_experiments) }
+  let_it_be(:another_experiment) { create(:ml_experiments, project: experiment.project) }
+
+  let(:expected_value) { 1 }
+  let(:expected_query) { 'SELECT COUNT(DISTINCT "ml_experiments"."project_id") FROM "ml_experiments"' }
+
+  it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' }
+end
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric_spec.rb
new file mode 100644
index 0000000000000000..b25d61d0bd283de7
--- /dev/null
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_users_with_ml_candidates_metric_spec.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountUsersWithMlCandidatesMetric, feature_category: :mlops do
+  let_it_be(:user_without_candidates) { create(:user) }
+  let_it_be(:candidate) { create(:ml_candidates) }
+  let_it_be(:another_candidate) { create(:ml_candidates, user: candidate.user) }
+
+  let(:expected_value) { 1 }
+  let(:expected_query) { 'SELECT COUNT(DISTINCT "ml_candidates"."user_id") FROM "ml_candidates"' }
+
+  it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' }
+end
-- 
GitLab


From ae78871b4d15f384fd1b49ce3c6fec3b7aed1609 Mon Sep 17 00:00:00 2001
From: Eduardo Bonet <ebonet@gitlab.com>
Date: Thu, 2 Feb 2023 17:11:03 +0100
Subject: [PATCH 2/7] Fixes product stage and category

---
 .../metrics/counts_all/20230201120001_count_ml_candidates.yml | 4 ++--
 .../counts_all/20230201120002_count_ml_experiments.yml        | 4 ++--
 .../20230201120003_count_projects_with_ml_candidates.yml      | 4 ++--
 .../20230201120004_count_users_with_ml_candidates.yml         | 4 ++--
 .../20230201120005_count_projects_with_ml_experiments.yml     | 4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/config/metrics/counts_all/20230201120001_count_ml_candidates.yml b/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
index 005862f1bfdc9c5a..698085c20a857a2b 100644
--- a/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
+++ b/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
@@ -2,9 +2,9 @@
 key_path: counts.count_ml_candidates
 description: Count of created Ml Candidates
 product_section: dev
-product_stage: mlops
+product_stage: create
 product_group: mlops
-product_category: mlops
+product_category: MLOps
 value_type: number
 status: active
 milestone: "15.9"
diff --git a/config/metrics/counts_all/20230201120002_count_ml_experiments.yml b/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
index 8a6f6100ea38def4..d125b7098f2f64b1 100644
--- a/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
+++ b/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
@@ -2,9 +2,9 @@
 key_path: counts.count_ml_experiments
 description: Count of created Ml Experiments
 product_section: dev
-product_stage: mlops
+product_stage: create
 product_group: mlops
-product_category: mlops
+product_category: MLOps
 value_type: number
 status: active
 milestone: "15.9"
diff --git a/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml b/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
index fd839af4eabf6884..268a7fca0936518a 100644
--- a/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
+++ b/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
@@ -2,9 +2,9 @@
 key_path: counts.count_projects_with_ml_candidates
 description: Count of unique projects that contain at least one Ml Candidate
 product_section: dev
-product_stage: mlops
+product_stage: create
 product_group: mlops
-product_category: mlops
+product_category: MLOps
 value_type: number
 status: active
 milestone: "15.9"
diff --git a/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml b/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
index 8781e738ef70bda8..13e3304d7e616267 100644
--- a/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
+++ b/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
@@ -2,9 +2,9 @@
 key_path: counts.count_users_with_ml_candidates
 description: Count of unique users that have created at least one Ml Candidate
 product_section: dev
-product_stage: mlops
+product_stage: create
 product_group: mlops
-product_category: mlops
+product_category: MLOps
 value_type: number
 status: active
 milestone: "15.9"
diff --git a/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml b/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
index 57556c71ba31553f..0f1836f4ac74ce74 100644
--- a/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
+++ b/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
@@ -2,9 +2,9 @@
 key_path: counts.count_projects_with_ml_experiments
 description: Count of unique projects that contain at least one Ml Experiment
 product_section: dev
-product_stage: mlops
+product_stage: create
 product_group: mlops
-product_category: mlops
+product_category: MLOps
 value_type: number
 status: active
 milestone: "15.9"
-- 
GitLab


From e194ee20c44c7376ff5498fae46ed4744ba3c681 Mon Sep 17 00:00:00 2001
From: Eduardo Bonet <ebonet@gitlab.com>
Date: Fri, 3 Feb 2023 14:33:39 +0100
Subject: [PATCH 3/7] Fixes instrumentation class names

---
 .../metrics/counts_all/20230201120001_count_ml_candidates.yml   | 2 +-
 .../metrics/counts_all/20230201120002_count_ml_experiments.yml  | 2 +-
 .../20230201120003_count_projects_with_ml_candidates.yml        | 2 +-
 .../20230201120004_count_users_with_ml_candidates.yml           | 2 +-
 .../20230201120005_count_projects_with_ml_experiments.yml       | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/config/metrics/counts_all/20230201120001_count_ml_candidates.yml b/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
index 698085c20a857a2b..aec9e18006ea239a 100644
--- a/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
+++ b/config/metrics/counts_all/20230201120001_count_ml_candidates.yml
@@ -12,7 +12,7 @@ introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
 time_frame: all
 data_source: database
 data_category: optional
-instrumentation_class: CountMlCandidates
+instrumentation_class: CountMlCandidatesMetric
 performance_indicator_type: []
 distribution:
 - ce
diff --git a/config/metrics/counts_all/20230201120002_count_ml_experiments.yml b/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
index d125b7098f2f64b1..52c076cf072340ef 100644
--- a/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
+++ b/config/metrics/counts_all/20230201120002_count_ml_experiments.yml
@@ -12,7 +12,7 @@ introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
 time_frame: all
 data_source: database
 data_category: optional
-instrumentation_class: CountMlExperiments
+instrumentation_class: CountMlExperimentsMetric
 performance_indicator_type: []
 distribution:
 - ce
diff --git a/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml b/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
index 268a7fca0936518a..77b0fef30c472565 100644
--- a/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
+++ b/config/metrics/counts_all/20230201120003_count_projects_with_ml_candidates.yml
@@ -12,7 +12,7 @@ introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
 time_frame: all
 data_source: database
 data_category: optional
-instrumentation_class: CountProjectsWithMlCandidates
+instrumentation_class: CountProjectsWithMlCandidatesMetric
 performance_indicator_type: []
 distribution:
 - ce
diff --git a/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml b/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
index 13e3304d7e616267..7cb86824e35204a8 100644
--- a/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
+++ b/config/metrics/counts_all/20230201120004_count_users_with_ml_candidates.yml
@@ -12,7 +12,7 @@ introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
 time_frame: all
 data_source: database
 data_category: optional
-instrumentation_class: CountUsersWithMlCandidates
+instrumentation_class: CountUsersWithMlCandidatesMetric
 performance_indicator_type: []
 distribution:
 - ce
diff --git a/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml b/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
index 0f1836f4ac74ce74..fed72a30697380fd 100644
--- a/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
+++ b/config/metrics/counts_all/20230201120005_count_projects_with_ml_experiments.yml
@@ -12,7 +12,7 @@ introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/110642
 time_frame: all
 data_source: database
 data_category: optional
-instrumentation_class: CountProjectsWithMlExperiments
+instrumentation_class: CountProjectsWithMlExperimentsMetric
 performance_indicator_type: []
 distribution:
 - ce
-- 
GitLab


From da246fb69fc70c24129b83128295b6519e70a2bf Mon Sep 17 00:00:00 2001
From: Eduardo Bonet <ebonet@gitlab.com>
Date: Fri, 3 Feb 2023 17:41:03 +0100
Subject: [PATCH 4/7] Fixes rubocop errors

---
 .../count_projects_with_ml_candidates_metric_spec.rb            | 2 +-
 .../count_projects_with_ml_experiments_metric_spec.rb           | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
index 36da073d22fa63ff..8ca0a30d775bdf8e 100644
--- a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
@@ -3,7 +3,7 @@
 require 'spec_helper'
 
 RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountProjectsWithMlCandidatesMetric,
-feature_category: :mlops do
+               feature_category: :mlops do
   let_it_be(:project_without_candidates) { create(:project, :repository) }
   let_it_be(:candidate) { create(:ml_candidates) }
   let_it_be(:another_candidate) { create(:ml_candidates, experiment: candidate.experiment) }
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
index 043d3fbc13613082..d827127518a00c39 100644
--- a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
@@ -3,7 +3,7 @@
 require 'spec_helper'
 
 RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountProjectsWithMlExperimentsMetric,
-feature_category: :mlops do
+               feature_category: :mlops do
   let_it_be(:project_without_experiment) { create(:project, :repository) }
   let_it_be(:experiment) { create(:ml_experiments) }
   let_it_be(:another_experiment) { create(:ml_experiments, project: experiment.project) }
-- 
GitLab


From 213a3cd369f7a75b7ddd06ddc5ab9b5cf8621259 Mon Sep 17 00:00:00 2001
From: Eduardo Bonet <ebonet@gitlab.com>
Date: Fri, 3 Feb 2023 19:48:44 +0100
Subject: [PATCH 5/7] Fix rubocop warning

---
 .../count_projects_with_ml_candidates_metric_spec.rb            | 2 +-
 .../count_projects_with_ml_experiments_metric_spec.rb           | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
index 8ca0a30d775bdf8e..68c4b5d2ec7b2b34 100644
--- a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
@@ -3,7 +3,7 @@
 require 'spec_helper'
 
 RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountProjectsWithMlCandidatesMetric,
-               feature_category: :mlops do
+  feature_category: :mlops do
   let_it_be(:project_without_candidates) { create(:project, :repository) }
   let_it_be(:candidate) { create(:ml_candidates) }
   let_it_be(:another_candidate) { create(:ml_candidates, experiment: candidate.experiment) }
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
index d827127518a00c39..829245f785b603cd 100644
--- a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_experiments_metric_spec.rb
@@ -3,7 +3,7 @@
 require 'spec_helper'
 
 RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountProjectsWithMlExperimentsMetric,
-               feature_category: :mlops do
+  feature_category: :mlops do
   let_it_be(:project_without_experiment) { create(:project, :repository) }
   let_it_be(:experiment) { create(:ml_experiments) }
   let_it_be(:another_experiment) { create(:ml_experiments, project: experiment.project) }
-- 
GitLab


From 5c4e4f3bc2696c754707ed62c2911a0f370370fc Mon Sep 17 00:00:00 2001
From: Eulyeon Ko <5961404-euko@users.noreply.gitlab.com>
Date: Mon, 6 Feb 2023 10:14:44 +0000
Subject: [PATCH 6/7] Apply 1 suggestion(s) to 1 file(s)

---
 .../count_projects_with_ml_candidates_metric.rb              | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb b/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb
index bdaeb87561de567d..bf1f5534a7008593 100644
--- a/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb
+++ b/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric.rb
@@ -7,7 +7,10 @@ module Instrumentations
         class CountProjectsWithMlCandidatesMetric < DatabaseMetric
           operation :distinct_count, column: 'ml_experiments.project_id'
 
-          relation { Ml::Candidate.joins(:experiment) }
+          relation do
+            Ml::Experiment.where('EXISTS (?)',
+              Ml::Candidate.where("\"ml_experiments\".\"id\" = \"ml_candidates\".\"experiment_id\"").select(1))
+          end
         end
       end
     end
-- 
GitLab


From 0c58e8a7f4a5342413bb838150dd9da186b51583 Mon Sep 17 00:00:00 2001
From: Eduardo Bonet <ebonet@gitlab.com>
Date: Mon, 6 Feb 2023 12:53:08 +0100
Subject: [PATCH 7/7] Fixes failing test

---
 .../count_projects_with_ml_candidates_metric_spec.rb          | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
index 68c4b5d2ec7b2b34..e5026ab6358c6b3d 100644
--- a/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
+++ b/spec/lib/gitlab/usage/metrics/instrumentations/count_projects_with_ml_candidates_metric_spec.rb
@@ -10,8 +10,8 @@
 
   let(:expected_value) { 1 }
   let(:expected_query) do
-    'SELECT COUNT(DISTINCT "ml_experiments"."project_id") FROM "ml_candidates" INNER JOIN "ml_experiments" ON ' \
-      '"ml_experiments"."id" = "ml_candidates"."experiment_id"'
+    'SELECT COUNT(DISTINCT "ml_experiments"."ml_experiments.project_id") FROM "ml_experiments" WHERE ' \
+      '(EXISTS (SELECT 1 FROM "ml_candidates" WHERE ("ml_experiments"."id" = "ml_candidates"."experiment_id")))'
   end
 
   it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' }
-- 
GitLab