From 4acbec5a23ca1ea65017e7dd722f0ddf89a50342 Mon Sep 17 00:00:00 2001
From: Kasia Misirli <kmisirli@gitlab.com>
Date: Wed, 30 Aug 2023 16:47:52 +0000
Subject: [PATCH] Add ci catalog component usage tracking

---
 ...157_count_cicd_component_usage_monthly.yml | 23 ++++++++++++
 ...4157_count_cicd_component_usage_weekly.yml | 23 ++++++++++++
 .../ci/config/external/file/component.rb      |  2 ++
 .../ci/config/external/file/component_spec.rb | 35 +++++++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 config/metrics/counts_28d/20230810124157_count_cicd_component_usage_monthly.yml
 create mode 100644 config/metrics/counts_7d/20230810124157_count_cicd_component_usage_weekly.yml

diff --git a/config/metrics/counts_28d/20230810124157_count_cicd_component_usage_monthly.yml b/config/metrics/counts_28d/20230810124157_count_cicd_component_usage_monthly.yml
new file mode 100644
index 000000000000..d860dddef12a
--- /dev/null
+++ b/config/metrics/counts_28d/20230810124157_count_cicd_component_usage_monthly.yml
@@ -0,0 +1,23 @@
+---
+key_path: redis_hll_counters.pipeline_authoring.count_cicd_component_usage_monthly
+description: Monthly count of CI/CD component usage
+product_section: ops
+product_stage: verify
+product_group: pipeline_authoring
+value_type: number
+status: active
+milestone: "16.4"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/129016
+time_frame: 28d
+data_source: redis_hll
+data_category: optional
+instrumentation_class: RedisHLLMetric
+performance_indicator_type: []
+distribution:
+- ce
+tier:
+- premium
+- ultimate
+options:
+  events:
+    - cicd_component_usage
diff --git a/config/metrics/counts_7d/20230810124157_count_cicd_component_usage_weekly.yml b/config/metrics/counts_7d/20230810124157_count_cicd_component_usage_weekly.yml
new file mode 100644
index 000000000000..e8c528dde670
--- /dev/null
+++ b/config/metrics/counts_7d/20230810124157_count_cicd_component_usage_weekly.yml
@@ -0,0 +1,23 @@
+---
+key_path: redis_hll_counters.pipeline_authoring.count_cicd_component_usage_weekly
+description: Weekly count of CI/CD component usage
+product_section: ops
+product_stage: verify
+product_group: pipeline_authoring
+value_type: number
+status: active
+milestone: "16.4"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/129016
+time_frame: 7d
+data_source: redis_hll
+data_category: optional
+instrumentation_class: RedisHLLMetric
+performance_indicator_type: []
+distribution:
+- ce
+tier:
+- premium
+- ultimate
+options:
+  events:
+    - cicd_component_usage
diff --git a/lib/gitlab/ci/config/external/file/component.rb b/lib/gitlab/ci/config/external/file/component.rb
index 15cc0783b86f..de6de1bb7a8e 100644
--- a/lib/gitlab/ci/config/external/file/component.rb
+++ b/lib/gitlab/ci/config/external/file/component.rb
@@ -18,6 +18,8 @@ def initialize(params, context)
             def content
               return unless component_result.success?
 
+              ::Gitlab::UsageDataCounters::HLLRedisCounter.track_event('cicd_component_usage', values: context.user.id)
+
               component_result.payload.fetch(:content)
             end
             strong_memoize_attr :content
diff --git a/spec/lib/gitlab/ci/config/external/file/component_spec.rb b/spec/lib/gitlab/ci/config/external/file/component_spec.rb
index 487690296b51..0f7b811b5dff 100644
--- a/spec/lib/gitlab/ci/config/external/file/component_spec.rb
+++ b/spec/lib/gitlab/ci/config/external/file/component_spec.rb
@@ -120,6 +120,41 @@
     end
   end
 
+  describe '#content' do
+    context 'when component is valid' do
+      let(:content) do
+        <<~COMPONENT
+        job:
+        script: echo
+        COMPONENT
+      end
+
+      let(:response) do
+        ServiceResponse.success(payload: {
+          content: content,
+          path: instance_double(::Gitlab::Ci::Components::InstancePath, project: project, sha: '12345')
+        })
+      end
+
+      it 'tracks the event' do
+        expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with('cicd_component_usage',
+          values: external_resource.context.user.id)
+
+        external_resource.content
+      end
+    end
+
+    context 'when component is invalid' do
+      let(:content) { 'the-content' }
+
+      it 'does not track the event' do
+        expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
+
+        external_resource.content
+      end
+    end
+  end
+
   describe '#metadata' do
     subject(:metadata) { external_resource.metadata }
 
-- 
GitLab