From 51ba5ba927575016aa2b1193029917a78317773d Mon Sep 17 00:00:00 2001
From: marshall007 <mcottrell@gitlab.com>
Date: Tue, 5 Dec 2023 16:23:55 -0500
Subject: [PATCH] Instrument code suggetions auth events to product analytics

---
 .../events/code_suggestions_authenticate.yml  | 22 +++++++++++++++
 ...otal_code_suggestions_authenticate_28d.yml | 27 +++++++++++++++++++
 ...total_code_suggestions_authenticate_7d.yml | 27 +++++++++++++++++++
 ee/lib/api/code_suggestions.rb                |  5 ++++
 4 files changed, 81 insertions(+)
 create mode 100644 config/events/code_suggestions_authenticate.yml
 create mode 100644 config/metrics/counts_28d/count_total_code_suggestions_authenticate_28d.yml
 create mode 100644 config/metrics/counts_7d/count_total_code_suggestions_authenticate_7d.yml

diff --git a/config/events/code_suggestions_authenticate.yml b/config/events/code_suggestions_authenticate.yml
new file mode 100644
index 000000000000..d5e1c0fd34bb
--- /dev/null
+++ b/config/events/code_suggestions_authenticate.yml
@@ -0,0 +1,22 @@
+---
+description: Tracks the number of authentication requests to the `POST /code_suggestions/tokens` API endpoint.
+category: InternalEventTracking
+action: code_suggestions_authenticate
+label_description:
+property_description:
+value_description:
+extra_properties:
+identifiers:
+- user
+product_section: dev
+product_stage: create
+product_group: code_creation
+milestone: "16.7"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/138848
+distributions:
+- ce
+- ee
+tiers:
+- free
+- premium
+- ultimate
diff --git a/config/metrics/counts_28d/count_total_code_suggestions_authenticate_28d.yml b/config/metrics/counts_28d/count_total_code_suggestions_authenticate_28d.yml
new file mode 100644
index 000000000000..3688d770c53d
--- /dev/null
+++ b/config/metrics/counts_28d/count_total_code_suggestions_authenticate_28d.yml
@@ -0,0 +1,27 @@
+---
+key_path: count_total_code_suggestions_authenticate_28d
+description: Tracks the number of authentication requests to the `POST /code_suggestions/tokens` API endpoint in the last 28 days.
+product_section: dev
+product_stage: create
+product_group: code_creation
+performance_indicator_type: []
+value_type: number
+status: active
+milestone: "16.7"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/138848
+time_frame: 28d
+data_source: internal_events
+data_category: optional
+instrumentation_class: TotalCountMetric
+distribution:
+- ce
+- ee
+tier:
+- free
+- premium
+- ultimate
+options:
+  events:
+    - code_suggestions_authenticate
+events:
+  - name: code_suggestions_authenticate
diff --git a/config/metrics/counts_7d/count_total_code_suggestions_authenticate_7d.yml b/config/metrics/counts_7d/count_total_code_suggestions_authenticate_7d.yml
new file mode 100644
index 000000000000..a2fe5a36c9bd
--- /dev/null
+++ b/config/metrics/counts_7d/count_total_code_suggestions_authenticate_7d.yml
@@ -0,0 +1,27 @@
+---
+key_path: count_total_code_suggestions_authenticate_7d
+description: Tracks the number of authentication requests to the `POST /code_suggestions/tokens` API endpoint in the last 7 days.
+product_section: dev
+product_stage: create
+product_group: code_creation
+performance_indicator_type: []
+value_type: number
+status: active
+milestone: "16.7"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/138848
+time_frame: 7d
+data_source: internal_events
+data_category: optional
+instrumentation_class: TotalCountMetric
+distribution:
+- ce
+- ee
+tier:
+- free
+- premium
+- ultimate
+options:
+  events:
+    - code_suggestions_authenticate
+events:
+  - name: code_suggestions_authenticate
diff --git a/ee/lib/api/code_suggestions.rb b/ee/lib/api/code_suggestions.rb
index 69e2a29e68ee..c1d6f7370cd5 100644
--- a/ee/lib/api/code_suggestions.rb
+++ b/ee/lib/api/code_suggestions.rb
@@ -101,6 +101,11 @@ def gitlab_realm
             label: 'code_suggestions'
           )
 
+          Gitlab::InternalEvents.track_event(
+            'code_suggestions_authenticate',
+            user: current_user
+          )
+
           token = Gitlab::Ai::AccessToken.new(current_user, scopes: [:code_suggestions], gitlab_realm: gitlab_realm)
           present token, with: Entities::CodeSuggestionsAccessToken
         end
-- 
GitLab