From 954218cafabbfc650c7e2f18e7a89d13a82ee25c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Wielich?= <mwielich@gitlab.com>
Date: Tue, 13 Jun 2023 13:29:54 +0000
Subject: [PATCH] Track code suggestions authentication

Track code suggestions authentication
---
 ...5152832_API__Ml__AiAssist_authenticate.yml | 22 +++++++++++++++++++
 ee/lib/api/code_suggestions.rb                |  7 ++++++
 ee/lib/api/ml/ai_assist.rb                    |  7 ++++++
 ee/spec/requests/api/code_suggestions_spec.rb | 15 +++++++++++++
 ee/spec/requests/api/ml/ai_assist_spec.rb     | 19 +++++++++++++++-
 5 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 config/events/20230605152832_API__Ml__AiAssist_authenticate.yml

diff --git a/config/events/20230605152832_API__Ml__AiAssist_authenticate.yml b/config/events/20230605152832_API__Ml__AiAssist_authenticate.yml
new file mode 100644
index 0000000000000..68335e8ec7591
--- /dev/null
+++ b/config/events/20230605152832_API__Ml__AiAssist_authenticate.yml
@@ -0,0 +1,22 @@
+---
+description: An event triggered when a user is authorized to use Code Suggestions
+category: API::Ml::AiAssist
+action: authenticate
+label_description: code_suggestions
+property_description:
+value_description:
+extra_properties:
+identifiers:
+- user
+product_section: dev
+product_stage: modelops
+product_group: ai_assisted
+milestone: "16.1"
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/122774
+distributions:
+- ee
+tiers:
+- free
+- premium
+- ultimate
+
diff --git a/ee/lib/api/code_suggestions.rb b/ee/lib/api/code_suggestions.rb
index 9697a48e71fd0..f8e6e8d9e62e5 100644
--- a/ee/lib/api/code_suggestions.rb
+++ b/ee/lib/api/code_suggestions.rb
@@ -42,6 +42,13 @@ def access_code_suggestions_when_proxied_to_saas?
         end
         post do
           with_proxy_ai_request do
+            Gitlab::Tracking.event(
+              'API::CodeSuggestions',
+              :authenticate,
+              user: current_user,
+              label: 'code_suggestions'
+            )
+
             token = Gitlab::CodeSuggestions::AccessToken.new(current_user)
             present token, with: Entities::CodeSuggestionsAccessToken
           end
diff --git a/ee/lib/api/ml/ai_assist.rb b/ee/lib/api/ml/ai_assist.rb
index 5cf6693cd146d..6da67b43c33af 100644
--- a/ee/lib/api/ml/ai_assist.rb
+++ b/ee/lib/api/ml/ai_assist.rb
@@ -22,6 +22,13 @@ class AiAssist < ::API::Base
           success EE::API::Entities::Ml::AiAssist
         end
         get 'ai-assist' do
+          Gitlab::Tracking.event(
+            'API::Ml::AiAssist',
+            :authenticate,
+            user: current_user,
+            label: 'code_suggestions'
+          )
+
           response = {
             user_is_allowed: true,
             third_party_ai_features_enabled: current_user.third_party_ai_features_enabled?
diff --git a/ee/spec/requests/api/code_suggestions_spec.rb b/ee/spec/requests/api/code_suggestions_spec.rb
index 6d4ef5f8f73b4..53d79ef8acd44 100644
--- a/ee/spec/requests/api/code_suggestions_spec.rb
+++ b/ee/spec/requests/api/code_suggestions_spec.rb
@@ -13,6 +13,21 @@
 
       expect(json_response).to include(**body)
     end
+
+    it "records Snowplow events" do
+      post_api
+
+      if case_name == 'successful'
+        expect_snowplow_event(
+          category: described_class.name,
+          action: :authenticate,
+          user: current_user,
+          label: 'code_suggestions'
+        )
+      else
+        expect_no_snowplow_event
+      end
+    end
   end
 
   shared_examples 'a successful response' do
diff --git a/ee/spec/requests/api/ml/ai_assist_spec.rb b/ee/spec/requests/api/ml/ai_assist_spec.rb
index 156067563571b..23e8677895709 100644
--- a/ee/spec/requests/api/ml/ai_assist_spec.rb
+++ b/ee/spec/requests/api/ml/ai_assist_spec.rb
@@ -15,15 +15,32 @@
   end
 
   shared_examples 'a response' do |result, body = nil|
-    it "returns #{result} response", :aggregate_failures do
+    before do
       allowed_group
+    end
 
+    it "returns #{result} response", :aggregate_failures do
       get_api
 
       expect(response).to have_gitlab_http_status(result)
 
       expect(json_response).to eq(body) if body
     end
+
+    it "records Snowplow events" do
+      get_api
+
+      if result == :ok
+        expect_snowplow_event(
+          category: described_class.name,
+          action: :authenticate,
+          user: current_user,
+          label: 'code_suggestions'
+        )
+      else
+        expect_no_snowplow_event
+      end
+    end
   end
 
   describe 'GET /ml/ai-assist user_is_allowed' do
-- 
GitLab