Commit 1fcd8d97 authored by alinamihaila's avatar alinamihaila

Refactor track_usage_event method

  * Allow to track any value
  * Add documentation
  * Add tests
parent 73bb15b0
...@@ -20,7 +20,7 @@ module Mutations ...@@ -20,7 +20,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid]) alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = set_assignees(alert, args[:assignee_usernames], args[:operation_mode]) 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) prepare_response(result)
end end
......
...@@ -11,7 +11,7 @@ module Mutations ...@@ -11,7 +11,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid]) alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = ::AlertManagement::Alerts::Todo::CreateService.new(alert, current_user).execute 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) prepare_response(result)
end end
......
...@@ -9,7 +9,7 @@ module Mutations ...@@ -9,7 +9,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid]) alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = create_alert_issue(alert, current_user) 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) prepare_response(alert, result)
end end
......
...@@ -13,7 +13,7 @@ module Mutations ...@@ -13,7 +13,7 @@ module Mutations
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid]) alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
result = update_status(alert, args[:status]) 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) prepare_response(result)
end end
......
...@@ -7,7 +7,7 @@ module IncidentManagement ...@@ -7,7 +7,7 @@ module IncidentManagement
def track_incident_action(current_user, target, action) def track_incident_action(current_user, target, action)
return unless target.incident? return unless target.incident?
track_usage_event(:"incident_management_#{action}", current_user) track_usage_event(:"incident_management_#{action}", current_user.id)
end end
# No-op as optionally overridden in implementing classes. # No-op as optionally overridden in implementing classes.
......
...@@ -52,7 +52,7 @@ module Issues ...@@ -52,7 +52,7 @@ module Issues
# don't enqueue immediately to prevent todos removal in case of a mistake # 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? TodosDestroyer::ConfidentialIssueWorker.perform_in(Todo::WAIT_FOR_DELETE, issue.id) if issue.confidential?
create_confidentiality_note(issue) 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 end
added_labels = issue.labels - old_labels added_labels = issue.labels - old_labels
......
...@@ -111,7 +111,7 @@ module Notes ...@@ -111,7 +111,7 @@ module Notes
def track_event(note, user) def track_event(note, user)
return unless note.noteable.is_a?(Issue) && note.noteable.incident? 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 end
end end
...@@ -356,7 +356,7 @@ class TodoService ...@@ -356,7 +356,7 @@ class TodoService
def track_todo_creation(user, issue_type) def track_todo_creation(user, issue_type)
return unless issue_type == 'incident' return unless issue_type == 'incident'
track_usage_event(:incident_management_incident_todo, user) track_usage_event(:incident_management_incident_todo, user.id)
end end
end end
......
...@@ -327,6 +327,20 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF ...@@ -327,6 +327,20 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
end 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 1. Track event using `UsageData` API
Increment unique users count using Redis HLL, for given event name. Increment unique users count using Redis HLL, for given event name.
......
...@@ -97,7 +97,7 @@ module StatusPage ...@@ -97,7 +97,7 @@ module StatusPage
end end
def track_event 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 end
end end
...@@ -102,11 +102,13 @@ module Gitlab ...@@ -102,11 +102,13 @@ module Gitlab
yield.merge(key => Time.current) yield.merge(key => Time.current)
end end
def track_usage_event(metric_name, target) # @param event_name [String] the event name
return unless Feature.enabled?(:"usage_data_#{metric_name}", default_enabled: true) # @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? 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 end
private private
......
...@@ -209,4 +209,50 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -209,4 +209,50 @@ RSpec.describe Gitlab::Utils::UsageData do
end end
end 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 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 'logs an exception 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 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