Commit b099e87c authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '258-collecting-mau-for-gitlab-workflow-vs-code-extension' into 'master'

Track api requests from the the vs code extension [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!52484
parents bcf29d30 33471840
......@@ -20,6 +20,7 @@ class GraphqlController < ApplicationController
before_action :authorize_access_api!
before_action(only: [:execute]) { authenticate_sessionless_user!(:api) }
before_action :set_user_last_activity
before_action :track_vs_code_usage
# Since we deactivate authentication from the main ApplicationController and
# defer it to :authorize_access_api!, we need to override the bypass session
......@@ -64,6 +65,10 @@ class GraphqlController < ApplicationController
Users::ActivityService.new(current_user).execute
end
def track_vs_code_usage
Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter.track_api_request_when_trackable(user_agent: request.user_agent, user: current_user)
end
def execute_multiplex
GitlabSchema.multiplex(multiplex_queries, context: context)
end
......
---
title: Track API requests from the the VS Code extension
merge_request: 52484
author:
type: other
---
name: usage_data_i_code_review_user_vs_code_api_request
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/52484
rollout_issue_url:
milestone: '13.9'
type: development
group: group::code review
default_enabled: true
......@@ -68,6 +68,10 @@ module API
set_peek_enabled_for_current_request
end
after do
Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter.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 }
......
......@@ -149,3 +149,8 @@
category: code_review
aggregation: weekly
feature_flag: usage_data_i_code_review_user_approval_rule_edited
- name: i_code_review_user_vs_code_api_request
redis_slot: code_review
category: code_review
aggregation: weekly
feature_flag: usage_data_i_code_review_user_vs_code_api_request
# frozen_string_literal: true
module Gitlab
module UsageDataCounters
module VSCodeExtensionActivityUniqueCounter
VS_CODE_API_REQUEST_ACTION = 'i_code_review_user_vs_code_api_request'
VS_CODE_USER_AGENT_REGEX = /\Avs-code-gitlab-workflow/.freeze
class << self
def track_api_request_when_trackable(user_agent:, user:)
user_agent&.match?(VS_CODE_USER_AGENT_REGEX) && track_unique_action_by_user(VS_CODE_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
......@@ -66,6 +66,16 @@ RSpec.describe GraphqlController do
expect(assigns(:context)[:is_sessionless_user]).to be false
end
it 'calls the track api when trackable method' do
agent = 'vs-code-gitlab-workflow/3.11.1 VSCode/1.52.1 Node.js/12.14.1 (darwin; x64)'
request.env['HTTP_USER_AGENT'] = agent
expect(Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter)
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
post :execute
end
end
context 'when user uses an API token' do
......@@ -83,6 +93,16 @@ RSpec.describe GraphqlController do
expect(assigns(:context)[:is_sessionless_user]).to be true
end
it 'calls the track api when trackable method' do
agent = 'vs-code-gitlab-workflow/3.11.1 VSCode/1.52.1 Node.js/12.14.1 (darwin; x64)'
request.env['HTTP_USER_AGENT'] = agent
expect(Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter)
.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.shared_examples 'a tracked vs code unique action' do |event|
before do
stub_application_setting(usage_ping_enabled: true)
end
def count_unique(date_from:, date_to:)
Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: action, start_date: date_from, end_date: date_to)
end
it 'tracks when the user agent is from vs code' do
aggregate_failures do
user_agent = { user_agent: 'vs-code-gitlab-workflow/3.11.1 VSCode/1.52.1 Node.js/12.14.1 (darwin; x64)' }
expect(track_action(user: user1, **user_agent)).to be_truthy
expect(track_action(user: user1, **user_agent)).to be_truthy
expect(track_action(user: user2, **user_agent)).to be_truthy
expect(count_unique(date_from: time - 1.week, date_to: time + 1.week)).to eq(2)
end
end
it 'does not track when the user agent is not from vs code' do
aggregate_failures do
user_agent = { user_agent: 'normal_user_agent' }
expect(track_action(user: user1, **user_agent)).to be_falsey
expect(track_action(user: user1, **user_agent)).to be_falsey
expect(track_action(user: user2, **user_agent)).to be_falsey
expect(count_unique(date_from: time - 1.week, date_to: time + 1.week)).to eq(0)
end
end
it 'does not track if user agent is not present' do
expect(track_action(user: nil, user_agent: nil)).to be_nil
end
it 'does not track if user is not present' do
user_agent = { user_agent: 'vs-code-gitlab-workflow/3.11.1 VSCode/1.52.1 Node.js/12.14.1 (darwin; x64)' }
expect(track_action(user: nil, **user_agent)).to be_nil
end
end
RSpec.describe Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter, :clean_gitlab_redis_shared_state do
let(:user1) { build(:user, id: 1) }
let(:user2) { build(:user, id: 2) }
let(:time) { Time.current }
context 'when tracking a vs code api request' do
it_behaves_like 'a tracked vs code unique action' do
let(:action) { described_class::VS_CODE_API_REQUEST_ACTION }
def track_action(params)
described_class.track_api_request_when_trackable(**params)
end
end
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