From d15ba5b7d47cc8ba0f4fe82ef8eae324b7d13233 Mon Sep 17 00:00:00 2001
From: Avielle Wolfe <awolfe@gitlab.com>
Date: Mon, 22 Jul 2024 16:24:10 +0000
Subject: [PATCH] Instrument include:component content load times

We track generic include file load times in the
`config_file_fetch_content_hash` metric. However, we might introduce a
feature that would increase the load times specifically when loading
components, so we need a metric that only tracks component content load
times.

Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/438275?work_item_iid=471843
---
 .../ci/config/external/file/component.rb      |  7 ++++++
 .../ci/config/external/file/component_spec.rb | 22 +++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/lib/gitlab/ci/config/external/file/component.rb b/lib/gitlab/ci/config/external/file/component.rb
index 026e7f6757da5..b34c303785b7b 100644
--- a/lib/gitlab/ci/config/external/file/component.rb
+++ b/lib/gitlab/ci/config/external/file/component.rb
@@ -62,6 +62,13 @@ def validate_content!
 
             attr_reader :path, :version
 
+            def content_result
+              context.logger.instrument(:config_component_fetch_content_hash) do
+                super
+              end
+            end
+            strong_memoize_attr :content_result
+
             def component_result
               ::Ci::Components::FetchService.new(
                 address: location,
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 ec5d38fd6b984..fea3603c1674b 100644
--- a/spec/lib/gitlab/ci/config/external/file/component_spec.rb
+++ b/spec/lib/gitlab/ci/config/external/file/component_spec.rb
@@ -210,4 +210,26 @@
       end
     end
   end
+
+  describe '#load_and_validate_expanded_hash!' do
+    let(:logger) { instance_double(::Gitlab::Ci::Pipeline::Logger, :instrument) }
+
+    let(:context_params) do
+      {
+        project: context_project,
+        sha: 'context_sha',
+        user: user,
+        variables: project_variables,
+        logger: logger
+      }
+    end
+
+    it 'tracks the content load time' do
+      expect(logger).to receive(:instrument).once.ordered.with(:config_component_fetch_content_hash).and_yield
+      expect(logger).to receive(:instrument).once.ordered.with(:config_file_fetch_content_hash).and_yield
+      expect(logger).to receive(:instrument).once.ordered.with(:config_file_expand_content_includes).and_yield
+
+      external_resource.load_and_validate_expanded_hash!
+    end
+  end
 end
-- 
GitLab