Commit c1943d53 authored by James Fargher's avatar James Fargher

Merge branch '233786-ppi-unique-audit-events-api-2' into 'master'

Add ability to track unique uses of API endpoints

See merge request gitlab-org/gitlab!41689
parents ca7aad60 0917bc7e
---
title: Add ability to track unique uses of API endpoints
merge_request: 41689
author:
type: added
......@@ -7,6 +7,7 @@ module API
before do
authenticated_as_admin!
forbidden! unless ::License.feature_available?(:admin_audit_log)
increment_unique_values('a_compliance_audit_events_api', current_user.id)
end
resources :audit_events do
......
......@@ -92,6 +92,7 @@ module EE
before do
authorize! :admin_group, user_group
check_audit_events_available!(user_group)
increment_unique_values('a_compliance_audit_events_api', current_user.id)
end
desc 'Get a list of audit events in this group.' do
......
......@@ -25,6 +25,7 @@ module EE
before do
authorize! :admin_project, user_project
check_audit_events_available!(user_project)
increment_unique_values('a_compliance_audit_events_api', current_user.id)
end
desc 'Get a list of audit events in this project.' do
......
......@@ -3,6 +3,44 @@
require 'spec_helper'
RSpec.describe API::AuditEvents do
describe 'Unique usage tracking', :clean_gitlab_redis_shared_state do
let_it_be(:current_user) { create(:admin) }
let_it_be(:group) { create(:group, owner_id: current_user) }
let_it_be(:project) { create(:project) }
before do
project.add_user(current_user, :maintainer)
end
context 'after calling all audit_events APIs as a single licensed user' do
before do
stub_feature_flags(usage_data_a_compliance_audit_events_api: true)
stub_licensed_features(admin_audit_log: true)
end
subject do
travel_to 8.days.ago do
get api('/audit_events', current_user)
get api("/groups/#{group.id}/audit_events", current_user)
get api("/projects/#{project.id}/audit_events", current_user)
end
end
it 'tracks 3 separate events' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).exactly(3).times
.with(current_user.id, 'a_compliance_audit_events_api')
subject
end
it 'reports one unique event' do
subject
expect(Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: 'a_compliance_audit_events_api', start_date: 2.months.ago, end_date: Date.current)).to eq(1)
end
end
end
describe 'GET /audit_events' do
let(:url) { "/audit_events" }
......
......@@ -16,6 +16,10 @@
category: compliance
redis_slot: compliance
aggregation: weekly
- name: a_compliance_audit_events_api
category: compliance
redis_slot: compliance
aggregation: weekly
# Analytics category
- name: g_analytics_contribution
category: analytics
......
......@@ -1098,6 +1098,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
'g_compliance_audit_events' => 123,
'i_compliance_credential_inventory' => 123,
'i_compliance_audit_events' => 123,
'a_compliance_audit_events_api' => 123,
'compliance_unique_visits_for_any_target' => 543,
'compliance_unique_visits_for_any_target_monthly' => 987
}
......
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