Commit f5f1a283 authored by Mark Chao's avatar Mark Chao

Merge branch '247862-change-track_usage_event-method-to-accept-any-value' into 'master'

Change `track_usage_event` method to accept any value

Closes #247862

See merge request gitlab-org/gitlab!42452
parents 767cfd08 ca6c588d
......@@ -20,7 +20,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = set_assignees(alert, args[:assignee_usernames], args[:operation_mode])
track_usage_event(:incident_management_alert_assigned, current_user)
track_usage_event(:incident_management_alert_assigned, current_user.id)
prepare_response(result)
end
......
......@@ -11,7 +11,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = ::AlertManagement::Alerts::Todo::CreateService.new(alert, current_user).execute
track_usage_event(:incident_management_alert_todo, current_user)
track_usage_event(:incident_management_alert_todo, current_user.id)
prepare_response(result)
end
......
......@@ -9,7 +9,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = create_alert_issue(alert, current_user)
track_usage_event(:incident_management_incident_created, current_user)
track_usage_event(:incident_management_incident_created, current_user.id)
prepare_response(alert, result)
end
......
......@@ -13,7 +13,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = update_status(alert, args[:status])
track_usage_event(:incident_management_alert_status_changed, current_user)
track_usage_event(:incident_management_alert_status_changed, current_user.id)
prepare_response(result)
end
......
......@@ -27,7 +27,7 @@ module Ci
if lsif?(artifact_type)
headers[:ProcessLsif] = true
track_usage_event('i_source_code_code_intelligence', project)
track_usage_event('i_source_code_code_intelligence', project.id)
end
success(headers: headers)
......
......@@ -7,7 +7,7 @@ module IncidentManagement
def track_incident_action(current_user, target, action)
return unless target.incident?
track_usage_event(:"incident_management_#{action}", current_user)
track_usage_event(:"incident_management_#{action}", current_user.id)
end
# No-op as optionally overridden in implementing classes.
......
......@@ -52,7 +52,7 @@ module Issues
# don't enqueue immediately to prevent todos removal in case of a mistake
TodosDestroyer::ConfidentialIssueWorker.perform_in(Todo::WAIT_FOR_DELETE, issue.id) if issue.confidential?
create_confidentiality_note(issue)
track_usage_event(:incident_management_incident_change_confidential, current_user)
track_usage_event(:incident_management_incident_change_confidential, current_user.id)
end
added_labels = issue.labels - old_labels
......
......@@ -111,7 +111,7 @@ module Notes
def track_event(note, user)
return unless note.noteable.is_a?(Issue) && note.noteable.incident?
track_usage_event(:incident_management_incident_comment, user)
track_usage_event(:incident_management_incident_comment, user.id)
end
end
end
......@@ -356,7 +356,7 @@ class TodoService
def track_todo_creation(user, issue_type)
return unless issue_type == 'incident'
track_usage_event(:incident_management_incident_todo, user)
track_usage_event(:incident_management_incident_todo, user.id)
end
end
......
......@@ -327,6 +327,20 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
end
```
1. Track event using `track_usage_event(event_name, values) in services and graphql
Increment unique values count using Redis HLL, for given event name.
Example:
[Track usage event for incident created in service](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/issues/update_service.rb)
[Track usage event for incident created in graphql](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/graphql/mutations/alert_management/update_alert_status.rb)
```ruby
track_usage_event(:incident_management_incident_created, current_user.id)
```
1. Track event using `UsageData` API
Increment unique users count using Redis HLL, for given event name.
......
......@@ -97,7 +97,7 @@ module StatusPage
end
def track_event
track_usage_event(:incident_management_incident_published, user) unless should_unpublish?
track_usage_event(:incident_management_incident_published, user.id) unless should_unpublish?
end
end
end
......@@ -102,11 +102,13 @@ module Gitlab
yield.merge(key => Time.current)
end
def track_usage_event(metric_name, target)
return unless Feature.enabled?(:"usage_data_#{metric_name}", default_enabled: true)
# @param event_name [String] the event name
# @param values [Array|String] the values counted
def track_usage_event(event_name, values)
return unless Feature.enabled?(:"usage_data_#{event_name}", default_enabled: true)
return unless Gitlab::CurrentSettings.usage_ping_enabled?
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(target.id, metric_name.to_s)
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(values, event_name.to_s)
end
private
......
......@@ -209,4 +209,50 @@ RSpec.describe Gitlab::Utils::UsageData do
end
end
end
describe '#track_usage_event' do
let(:value) { '9f302fea-f828-4ca9-aef4-e10bd723c0b3' }
let(:event_name) { 'my_event' }
let(:unknown_event) { 'unknown' }
let(:feature) { "usage_data_#{event_name}" }
context 'with feature enabled' do
before do
stub_feature_flags(feature => true)
end
it 'tracks redis hll event' do
stub_application_setting(usage_ping_enabled: true)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(value, event_name)
described_class.track_usage_event(event_name, value)
end
it 'does not track event when usage ping is not enabled' do
stub_application_setting(usage_ping_enabled: false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
described_class.track_usage_event(event_name, value)
end
it 'raise an error for unknown event' do
stub_application_setting(usage_ping_enabled: true)
expect { described_class.track_usage_event(unknown_event, value) }.to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownEvent)
end
end
context 'with feature disabled' do
before do
stub_feature_flags(feature => false)
end
it 'does not track event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
described_class.track_usage_event(event_name, value)
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