Commit 0a4e28b7 authored by Sean Arnold's avatar Sean Arnold

Merge branch 'phikai-track-gitlabcli-usage' into 'master'

Track api usage of the gitlab cli

See merge request gitlab-org/gitlab!83464
parents d56557e7 63639224
......@@ -32,6 +32,7 @@ class GraphqlController < ApplicationController
before_action :set_user_last_activity
before_action :track_vs_code_usage
before_action :track_jetbrains_usage
before_action :track_gitlab_cli_usage
before_action :disable_query_limiting
before_action :limit_query_size
......@@ -143,6 +144,11 @@ class GraphqlController < ApplicationController
.track_api_request_when_trackable(user_agent: request.user_agent, user: current_user)
end
def track_gitlab_cli_usage
Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter
.track_api_request_when_trackable(user_agent: request.user_agent, user: current_user)
end
def execute_multiplex
GitlabSchema.multiplex(multiplex_queries, context: context)
end
......
---
name: usage_data_i_code_review_user_gitlab_cli_api_request
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/83464
rollout_issue_url:
milestone: '14.10'
type: development
group: group::code review
default_enabled: true
......@@ -25,6 +25,7 @@ Rails.autoloaders.each do |autoloader|
'cidr' => 'CIDR',
'cli' => 'CLI',
'dn' => 'DN',
'gitlab_cli_activity_unique_counter' => 'GitLabCliActivityUniqueCounter',
'global_id_type' => 'GlobalIDType',
'global_id_compatibility' => 'GlobalIDCompatibility',
'hll' => 'HLL',
......
......@@ -74,6 +74,7 @@
- 'i_code_review_post_merge_submit_revert_modal'
- 'i_code_review_post_merge_submit_cherry_pick_modal'
- 'i_code_review_user_jetbrains_api_request'
- 'i_code_review_user_gitlab_cli_api_request'
- name: code_review_category_monthly_active_users
operator: OR
source: redis
......@@ -146,3 +147,4 @@
events:
- 'i_code_review_user_vs_code_api_request'
- 'i_code_review_user_jetbrains_api_request'
- 'i_code_review_user_gitlab_cli_api_request'
---
key_path: redis_hll_counters.code_review.i_code_review_user_gitlab_cli_api_request_monthly
description: Count of unique users per month who use the GitLab CLI
product_section: dev
product_stage: create
product_group: group::code review
product_category: editor_extension
value_type: number
status: active
milestone: "14.10"
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/83464
time_frame: 28d
data_source: redis_hll
data_category: optional
instrumentation_class: RedisHLLMetric
options:
events:
- i_code_review_user_gitlab_cli_api_request
performance_indicator_type: []
distribution:
- ce
- ee
tier:
- free
- premium
- ultimate
---
key_path: redis_hll_counters.code_review.i_code_review_user_gitlab_cli_api_request_weekly
description: Count of unique users per week who use the GitLab CLI
product_section: dev
product_stage: create
product_group: group::code review
product_category: editor_extension
value_type: number
status: active
milestone: "14.10"
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/83464
time_frame: 7d
data_source: redis_hll
data_category: optional
instrumentation_class: RedisHLLMetric
options:
events:
- i_code_review_user_gitlab_cli_api_request
performance_indicator_type: []
distribution:
- ce
- ee
tier:
- free
- premium
- ultimate
......@@ -80,6 +80,10 @@ module API
Gitlab::UsageDataCounters::JetBrainsPluginActivityUniqueCounter.track_api_request_when_trackable(user_agent: request&.user_agent, user: @current_user)
end
after do
Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter.track_api_request_when_trackable(user_agent: request&.user_agent, user: @current_user)
end
# The locale is set to the current user's locale when `current_user` is loaded
after { Gitlab::I18n.use_default_locale }
......
# frozen_string_literal: true
module Gitlab
module UsageDataCounters
module GitLabCliActivityUniqueCounter
GITLAB_CLI_API_REQUEST_ACTION = 'i_code_review_user_gitlab_cli_api_request'
GITLAB_CLI_USER_AGENT_REGEX = /GitLab\sCLI$/.freeze
class << self
def track_api_request_when_trackable(user_agent:, user:)
user_agent&.match?(GITLAB_CLI_USER_AGENT_REGEX) && track_unique_action_by_user(GITLAB_CLI_API_REQUEST_ACTION, user)
end
private
def track_unique_action_by_user(action, user)
return unless user
track_unique_action(action, user.id)
end
def track_unique_action(action, value)
Gitlab::UsageDataCounters::HLLRedisCounter.track_usage_event(action, value)
end
end
end
end
end
......@@ -132,6 +132,11 @@
category: code_review
aggregation: weekly
feature_flag: usage_data_i_code_review_user_jetbrains_api_request
- name: i_code_review_user_gitlab_cli_api_request
redis_slot: code_review
category: code_review
aggregation: weekly
feature_flag: usage_data_i_code_review_user_gitlab_cli_api_request
- name: i_code_review_user_create_mr_from_issue
redis_slot: code_review
category: code_review
......
......@@ -135,6 +135,16 @@ RSpec.describe GraphqlController do
post :execute
end
it 'calls the track gitlab cli when trackable method' do
agent = 'GLab - GitLab CLI'
request.env['HTTP_USER_AGENT'] = agent
expect(Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter)
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
post :execute
end
it "assigns username in ApplicationContext" do
post :execute
......@@ -220,6 +230,16 @@ RSpec.describe GraphqlController do
subject
end
it 'calls the track gitlab cli when trackable method' do
agent = 'GLab - GitLab CLI'
request.env['HTTP_USER_AGENT'] = agent
expect(Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter)
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
subject
end
end
context 'when user is not logged in' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter, :clean_gitlab_redis_shared_state do # rubocop:disable RSpec/FilePath
let(:user1) { build(:user, id: 1) }
let(:user2) { build(:user, id: 2) }
let(:time) { Time.current }
let(:action) { described_class::GITLAB_CLI_API_REQUEST_ACTION }
let(:user_agent) { { user_agent: 'GLab - GitLab CLI' } }
context 'when tracking a gitlab cli request' do
it_behaves_like 'a request from an extension'
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment