Commit 4ab63e61 authored by Sean McGivern's avatar Sean McGivern

Merge branch '235459-add-usage-data-tracking-for-ui-events' into 'master'

Add Usage data tracking using Redis HLL for UI events

See merge request gitlab-org/gitlab!41301
parents f06b1ad6 271061a1
---
name: usage_data_api
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/41301
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/235459
group: group::telemetry
type: development
default_enabled: false
......@@ -312,6 +312,28 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
end
```
1. Track event using `UsageData` API
Increment unique users count using Redis HLL, for given event name.
In order to be able to increment the values the related feature `usage_data<event_name>` should be enabled.
```plaintext
POST /usage_data/increment_unique_users
```
| Attribute | Type | Required | Description |
| :-------- | :--- | :------- | :---------- |
| `event` | string | yes | The event name it should be tracked |
Response
Return 200 if tracking failed for any reason.
- `401 Unauthorized` if user is not authenticated
- `400 Bad request` if event parameter is missing
- `200` if event was tracked or any errors
1. Track event using base module `Gitlab::UsageDataCounters::HLLRedisCounter.track_event(entity_id, event_name)`.
Arguments:
......
......@@ -235,6 +235,7 @@ module API
mount ::API::Templates
mount ::API::Todos
mount ::API::Triggers
mount ::API::UsageData
mount ::API::UserCounts
mount ::API::Users
mount ::API::Variables
......
# frozen_string_literal: true
module API
class UsageData < Grape::API::Instance
before { authenticate! }
namespace 'usage_data' do
before do
not_found! unless Feature.enabled?(:usage_data_api)
end
desc 'Track usage data events' do
detail 'This feature was introduced in GitLab 13.4.'
end
params do
requires :event, type: String, desc: 'The event name that should be tracked'
end
post 'increment_unique_users' do
event_name = params[:event]
increment_unique_values(event_name, current_user.id)
status :ok
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::UsageData do
let_it_be(:user) { create(:user) }
describe 'POST /usage_data/increment_unique_users' do
let(:endpoint) { '/usage_data/increment_unique_users' }
let(:known_event) { 'g_compliance_dashboard' }
let(:unknown_event) { 'unknown' }
context 'usage_data_api feature not enabled' do
it 'returns not_found' do
stub_feature_flags(usage_data_api: false)
post api(endpoint, user), params: { event: known_event }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'without authentication' do
it 'returns 401 response' do
post api(endpoint), params: { event: known_event }
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'with authentication' do
before do
stub_feature_flags(usage_data_api: true)
stub_feature_flags("usage_data_#{known_event}" => true)
stub_application_setting(usage_ping_enabled: true)
end
context 'when event is missing from params' do
it 'returns bad request' do
post api(endpoint, user), params: {}
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'with correct params' do
it 'returns status ok' do
expect(Gitlab::Redis::HLL).to receive(:add)
post api(endpoint, user), params: { event: known_event }
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'with unknown event' do
it 'returns status ok' do
expect(Gitlab::Redis::HLL).not_to receive(:add)
post api(endpoint, user), params: { event: unknown_event }
expect(response).to have_gitlab_http_status(:ok)
end
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