Commit c7eddac1 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'am-adjust-redis-hll-track_event-to-accept-hash' into 'master'

Allow hash options to track_event [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!49828
parents e3680b9c db69d91b
...@@ -33,7 +33,7 @@ module RedisTracking ...@@ -33,7 +33,7 @@ module RedisTracking
return unless metric_feature_enabled?(feature, feature_default_enabled) return unless metric_feature_enabled?(feature, feature_default_enabled)
return unless visitor_id return unless visitor_id
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(visitor_id, event_name) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: visitor_id)
end end
def trackable_request? def trackable_request?
......
...@@ -5,7 +5,7 @@ module Packages ...@@ -5,7 +5,7 @@ module Packages
def execute def execute
if Feature.enabled?(:collect_package_events_redis) if Feature.enabled?(:collect_package_events_redis)
::Packages::Event.unique_counters_for(event_scope, event_name, originator_type).each do |event_name| ::Packages::Event.unique_counters_for(event_scope, event_name, originator_type).each do |event_name|
::Gitlab::UsageDataCounters::HLLRedisCounter.track_event(current_user.id, event_name) ::Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: current_user.id)
end end
::Packages::Event.counters_for(event_scope, event_name, originator_type).each do |event_name| ::Packages::Event.counters_for(event_scope, event_name, originator_type).each do |event_name|
......
...@@ -474,19 +474,19 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF ...@@ -474,19 +474,19 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
api.trackRedisHllUserEvent('my_already_defined_event_name'), api.trackRedisHllUserEvent('my_already_defined_event_name'),
``` ```
1. Track event using base module `Gitlab::UsageDataCounters::HLLRedisCounter.track_event(values, event_name)`. 1. Track event using base module `Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values:)`.
Arguments: Arguments:
- `values`: One value or array of values we count. For example: user_id, visitor_id, user_ids.
- `event_name`: event name. - `event_name`: event name.
- `values`: One value or array of values we count. For example: user_id, visitor_id, user_ids.
1. Track event on context level using base module `Gitlab::UsageDataCounters::HLLRedisCounter.track_event_in_context(entity_id, event_name, context)`. 1. Track event on context level using base module `Gitlab::UsageDataCounters::HLLRedisCounter.track_event_in_context(event_name, values:, context:)`.
Arguments: Arguments:
- `entity_id`: value we count. For example: user_id, visitor_id.
- `event_name`: event name. - `event_name`: event name.
- `values`: values we count. For example: user_id, visitor_id.
- `context`: context value. Allowed values are `default`, `free`, `bronze`, `silver`, `gold`, `starter`, `premium`, `ultimate` - `context`: context value. Allowed values are `default`, `free`, `bronze`, `silver`, `gold`, `starter`, `premium`, `ultimate`
1. Get event data using `Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names:, start_date:, end_date:, context: '')`. 1. Get event data using `Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names:, start_date:, end_date:, context: '')`.
...@@ -503,8 +503,8 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF ...@@ -503,8 +503,8 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
Trigger events in rails console by using `track_event` method Trigger events in rails console by using `track_event` method
```ruby ```ruby
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(1, 'g_compliance_audit_events') Gitlab::UsageDataCounters::HLLRedisCounter.track_event('g_compliance_audit_events', values: 1)
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(2, 'g_compliance_audit_events') Gitlab::UsageDataCounters::HLLRedisCounter.track_event('g_compliance_audit_events', values: [2, 3])
``` ```
Next, get the unique events for the current week. Next, get the unique events for the current week.
...@@ -595,7 +595,7 @@ redis_usage_data { ::Gitlab::UsageCounters::PodLogs.usage_totals[:total] } ...@@ -595,7 +595,7 @@ redis_usage_data { ::Gitlab::UsageCounters::PodLogs.usage_totals[:total] }
# Define events in common.yml https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/usage_data_counters/known_events/common.yml # Define events in common.yml https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/usage_data_counters/known_events/common.yml
# Tracking events # Tracking events
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(visitor_id, 'expand_vulnerabilities') Gitlab::UsageDataCounters::HLLRedisCounter.track_event('expand_vulnerabilities', values: visitor_id)
# Get unique events for metric # Get unique events for metric
redis_usage_data { Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: 'expand_vulnerabilities', start_date: 28.days.ago, end_date: Date.current) } redis_usage_data { Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: 'expand_vulnerabilities', start_date: 28.days.ago, end_date: Date.current) }
......
...@@ -180,7 +180,7 @@ module EE ...@@ -180,7 +180,7 @@ module EE
return unless ::Feature.enabled?(:usage_data_i_ci_secrets_management_vault_build_created, default_enabled: true) return unless ::Feature.enabled?(:usage_data_i_ci_secrets_management_vault_build_created, default_enabled: true)
return unless ci_secrets_management_available? && secrets? return unless ci_secrets_management_available? && secrets?
::Gitlab::UsageDataCounters::HLLRedisCounter.track_event(user_id, 'i_ci_secrets_management_vault_build_created') ::Gitlab::UsageDataCounters::HLLRedisCounter.track_event('i_ci_secrets_management_vault_build_created', values: user_id)
end end
end end
end end
......
...@@ -63,7 +63,7 @@ RSpec.describe SearchController do ...@@ -63,7 +63,7 @@ RSpec.describe SearchController do
it 'does not track if there is no license available' do it 'does not track if there is no license available' do
stub_licensed_features(elastic_search: false) stub_licensed_features(elastic_search: false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(instance_of(String), target_id) expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(target_id, values: instance_of(String))
get :show, params: request_params, format: :html get :show, params: request_params, format: :html
end end
......
...@@ -52,7 +52,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -52,7 +52,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
expect(kwargs[:key]).to match(/^#{context}\_.*/) expect(kwargs[:key]).to match(/^#{context}\_.*/)
end end
described_class.track_event_in_context(entity, event_name, context) described_class.track_event_in_context(event_name, values: entity, context: context)
end end
end end
end end
...@@ -61,18 +61,18 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -61,18 +61,18 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
it 'is not incrementing the counter' do it 'is not incrementing the counter' do
expect(Gitlab::Redis::HLL).not_to receive(:add) expect(Gitlab::Redis::HLL).not_to receive(:add)
described_class.track_event_in_context(entity1, context_event, '') described_class.track_event_in_context(context_event, values: entity1, context: '')
end end
end end
end end
describe '.unique_events' do describe '.unique_events' do
before do before do
described_class.track_event_in_context([entity1, entity3], context_event, default_context, 2.days.ago) described_class.track_event_in_context(context_event, values: [entity1, entity3], context: default_context, time: 2.days.ago)
described_class.track_event_in_context(entity3, context_event, ultimate_context, 2.days.ago) described_class.track_event_in_context(context_event, values: entity3, context: ultimate_context, time: 2.days.ago)
described_class.track_event_in_context(entity3, context_event, gold_context, 2.days.ago) described_class.track_event_in_context(context_event, values: entity3, context: gold_context, time: 2.days.ago)
described_class.track_event_in_context(entity3, context_event, invalid_context, 2.days.ago) described_class.track_event_in_context(context_event, values: entity3, context: invalid_context, time: 2.days.ago)
described_class.track_event_in_context([entity1, entity2], context_event, '', 2.weeks.ago) described_class.track_event_in_context(context_event, values: [entity1, entity2], context: '', time: 2.weeks.ago)
end end
context 'with correct arguments' do context 'with correct arguments' do
......
...@@ -578,7 +578,7 @@ RSpec.describe Ci::Build do ...@@ -578,7 +578,7 @@ RSpec.describe Ci::Build do
it 'tracks unique users' do it 'tracks unique users' do
ci_build = build(:ci_build, secrets: valid_secrets) ci_build = build(:ci_build, secrets: valid_secrets)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(ci_build.user_id, 'i_ci_secrets_management_vault_build_created') expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with('i_ci_secrets_management_vault_build_created', values: ci_build.user_id)
ci_build.save! ci_build.save!
end end
......
...@@ -28,7 +28,7 @@ RSpec.describe API::AuditEvents do ...@@ -28,7 +28,7 @@ RSpec.describe API::AuditEvents do
it 'tracks 3 separate events' do it 'tracks 3 separate events' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).exactly(3).times expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).exactly(3).times
.with(current_user.id, 'a_compliance_audit_events_api') .with('a_compliance_audit_events_api', values: current_user.id)
subject subject
end end
......
...@@ -570,7 +570,7 @@ module API ...@@ -570,7 +570,7 @@ module API
return unless Feature.enabled?(feature_flag, default_enabled: true) return unless Feature.enabled?(feature_flag, default_enabled: true)
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(values, event_name) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: values)
rescue => error rescue => error
Gitlab::AppLogger.warn("Redis tracking event failed for event: #{event_name}, message: #{error.message}") Gitlab::AppLogger.warn("Redis tracking event failed for event: #{event_name}, message: #{error.message}")
end end
......
...@@ -4,7 +4,7 @@ module Gitlab ...@@ -4,7 +4,7 @@ module Gitlab
module Analytics module Analytics
class UniqueVisits class UniqueVisits
def track_visit(visitor_id, target_id, time = Time.zone.now) def track_visit(visitor_id, target_id, time = Time.zone.now)
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(visitor_id, target_id, time) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(target_id, values: visitor_id, time: time)
end end
# Returns number of unique visitors for given targets in given time frame # Returns number of unique visitors for given targets in given time frame
......
...@@ -53,7 +53,7 @@ module Gitlab ...@@ -53,7 +53,7 @@ module Gitlab
return unless Feature.enabled?(:track_editor_edit_actions, default_enabled: true) return unless Feature.enabled?(:track_editor_edit_actions, default_enabled: true)
return unless author return unless author
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(author.id, action, time) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(action, values: author.id, time: time)
end end
def count_unique(actions, date_from, date_to) def count_unique(actions, date_from, date_to)
......
...@@ -39,20 +39,31 @@ module Gitlab ...@@ -39,20 +39,31 @@ module Gitlab
# #
# Usage: # Usage:
# #
# * Track event: Gitlab::UsageDataCounters::HLLRedisCounter.track_event(user_id, 'g_compliance_dashboard') # * Track event: Gitlab::UsageDataCounters::HLLRedisCounter.track_event('g_compliance_dashboard', values: user_id)
# * Get unique counts per user: Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: 'g_compliance_dashboard', start_date: 28.days.ago, end_date: Date.current) # * Get unique counts per user: Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: 'g_compliance_dashboard', start_date: 28.days.ago, end_date: Date.current)
class << self class << self
include Gitlab::Utils::UsageData include Gitlab::Utils::UsageData
def track_event(value, event_name, time = Time.zone.now) # Track unique events
track(value, event_name, time: time) #
# event_name - The event name.
# values - One or multiple values counted.
# time - Time of the action, set to Time.current.
def track_event(event_name, values:, time: Time.current)
track(values, event_name, time: time)
end end
def track_event_in_context(value, event_name, context, time = Time.zone.now) # Track unique events
#
# event_name - The event name.
# values - One or multiple values counted.
# context - Event context, plan level tracking.
# time - Time of the action, set to Time.current.
def track_event_in_context(event_name, values:, context:, time: Time.zone.now)
return if context.blank? return if context.blank?
return unless context.in?(valid_context_list) return unless context.in?(valid_context_list)
track(value, event_name, context: context, time: time) track(values, event_name, context: context, time: time)
end end
def unique_events(event_names:, start_date:, end_date:, context: '') def unique_events(event_names:, start_date:, end_date:, context: '')
...@@ -114,13 +125,13 @@ module Gitlab ...@@ -114,13 +125,13 @@ module Gitlab
private private
def track(value, event_name, context: '', time: Time.zone.now) def track(values, event_name, context: '', time: Time.zone.now)
return unless Gitlab::CurrentSettings.usage_ping_enabled? return unless Gitlab::CurrentSettings.usage_ping_enabled?
event = event_for(event_name) event = event_for(event_name)
raise UnknownEvent, "Unknown event #{event_name}" unless event.present? raise UnknownEvent, "Unknown event #{event_name}" unless event.present?
Gitlab::Redis::HLL.add(key: redis_key(event, time, context), value: value, expiry: expiry(event)) Gitlab::Redis::HLL.add(key: redis_key(event, time, context), value: values, expiry: expiry(event))
end end
# The array of valid context on which we allow tracking # The array of valid context on which we allow tracking
......
...@@ -148,7 +148,7 @@ module Gitlab ...@@ -148,7 +148,7 @@ module Gitlab
return unless Feature.enabled?(:track_issue_activity_actions, default_enabled: true) return unless Feature.enabled?(:track_issue_activity_actions, default_enabled: true)
return unless author return unless author
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(author.id, action, time) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(action, values: author.id, time: time)
end end
end end
end end
......
...@@ -43,7 +43,7 @@ module Gitlab ...@@ -43,7 +43,7 @@ module Gitlab
return unless Gitlab::UsageDataCounters::HLLRedisCounter.known_event?(transformed_action.to_s) return unless Gitlab::UsageDataCounters::HLLRedisCounter.known_event?(transformed_action.to_s)
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(author_id, transformed_action.to_s, time) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(transformed_action.to_s, values: author_id, time: time)
track_git_write_action(author_id, transformed_action, time) track_git_write_action(author_id, transformed_action, time)
end end
...@@ -73,7 +73,7 @@ module Gitlab ...@@ -73,7 +73,7 @@ module Gitlab
def track_git_write_action(author_id, transformed_action, time) def track_git_write_action(author_id, transformed_action, time)
return unless GIT_WRITE_ACTIONS.include?(transformed_action) return unless GIT_WRITE_ACTIONS.include?(transformed_action)
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(author_id, GIT_WRITE_ACTION, time) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(GIT_WRITE_ACTION, values: author_id, time: time)
end end
end end
end end
......
...@@ -122,7 +122,7 @@ module Gitlab ...@@ -122,7 +122,7 @@ module Gitlab
def track_usage_event(event_name, values) def track_usage_event(event_name, values)
return unless Feature.enabled?(:"usage_data_#{event_name}", default_enabled: true) return unless Feature.enabled?(:"usage_data_#{event_name}", default_enabled: true)
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(values, event_name.to_s) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name.to_s, values: values)
end end
private private
......
...@@ -42,7 +42,7 @@ RSpec.describe RedisTracking do ...@@ -42,7 +42,7 @@ RSpec.describe RedisTracking do
def expect_tracking def expect_tracking
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
.with(instance_of(String), 'g_compliance_approval_rules') .with('g_compliance_approval_rules', values: instance_of(String))
end end
def expect_no_tracking def expect_no_tracking
......
...@@ -205,7 +205,7 @@ RSpec.describe API::Helpers do ...@@ -205,7 +205,7 @@ RSpec.describe API::Helpers do
end end
it 'tracks redis hll event' do it 'tracks redis hll event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(value, event_name) expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(event_name, values: value)
subject.increment_unique_values(event_name, value) subject.increment_unique_values(event_name, value)
end end
......
...@@ -88,7 +88,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -88,7 +88,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
it 'does not track the event' do it 'does not track the event' do
stub_application_setting(usage_ping_enabled: false) stub_application_setting(usage_ping_enabled: false)
described_class.track_event(entity1, weekly_event, Date.current) described_class.track_event(weekly_event, values: entity1, time: Date.current)
expect(Gitlab::Redis::HLL).not_to receive(:add) expect(Gitlab::Redis::HLL).not_to receive(:add)
end end
...@@ -102,20 +102,27 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -102,20 +102,27 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
it 'tracks event when using symbol' do it 'tracks event when using symbol' do
expect(Gitlab::Redis::HLL).to receive(:add) expect(Gitlab::Redis::HLL).to receive(:add)
described_class.track_event(entity1, :g_analytics_contribution) described_class.track_event(:g_analytics_contribution, values: entity1)
end
it 'tracks events with multiple values' do
values = [entity1, entity2]
expect(Gitlab::Redis::HLL).to receive(:add).with(key: /g_{analytics}_contribution/, value: values, expiry: 84.days)
described_class.track_event(:g_analytics_contribution, values: values)
end end
it "raise error if metrics don't have same aggregation" do it "raise error if metrics don't have same aggregation" do
expect { described_class.track_event(entity1, different_aggregation, Date.current) }.to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownAggregation) expect { described_class.track_event(different_aggregation, values: entity1, time: Date.current) }.to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownAggregation)
end end
it 'raise error if metrics of unknown aggregation' do it 'raise error if metrics of unknown aggregation' do
expect { described_class.track_event(entity1, 'unknown', Date.current) }.to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownEvent) expect { described_class.track_event('unknown', values: entity1, time: Date.current) }.to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownEvent)
end end
context 'for weekly events' do context 'for weekly events' do
it 'sets the keys in Redis to expire automatically after the given expiry time' do it 'sets the keys in Redis to expire automatically after the given expiry time' do
described_class.track_event(entity1, "g_analytics_contribution") described_class.track_event("g_analytics_contribution", values: entity1)
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
keys = redis.scan_each(match: "g_{analytics}_contribution-*").to_a keys = redis.scan_each(match: "g_{analytics}_contribution-*").to_a
...@@ -128,7 +135,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -128,7 +135,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
end end
it 'sets the keys in Redis to expire automatically after 6 weeks by default' do it 'sets the keys in Redis to expire automatically after 6 weeks by default' do
described_class.track_event(entity1, "g_compliance_dashboard") described_class.track_event("g_compliance_dashboard", values: entity1)
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
keys = redis.scan_each(match: "g_{compliance}_dashboard-*").to_a keys = redis.scan_each(match: "g_{compliance}_dashboard-*").to_a
...@@ -143,7 +150,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -143,7 +150,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
context 'for daily events' do context 'for daily events' do
it 'sets the keys in Redis to expire after the given expiry time' do it 'sets the keys in Redis to expire after the given expiry time' do
described_class.track_event(entity1, "g_analytics_search") described_class.track_event("g_analytics_search", values: entity1)
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
keys = redis.scan_each(match: "*-g_{analytics}_search").to_a keys = redis.scan_each(match: "*-g_{analytics}_search").to_a
...@@ -156,7 +163,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -156,7 +163,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
end end
it 'sets the keys in Redis to expire after 29 days by default' do it 'sets the keys in Redis to expire after 29 days by default' do
described_class.track_event(entity1, "no_slot") described_class.track_event("no_slot", values: entity1)
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
keys = redis.scan_each(match: "*-{no_slot}").to_a keys = redis.scan_each(match: "*-{no_slot}").to_a
...@@ -173,12 +180,19 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -173,12 +180,19 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
describe '.track_event_in_context' do describe '.track_event_in_context' do
context 'with valid contex' do context 'with valid contex' do
it 'increments conext event counte' do it 'increments context event counter' do
expect(Gitlab::Redis::HLL).to receive(:add) do |kwargs| expect(Gitlab::Redis::HLL).to receive(:add) do |kwargs|
expect(kwargs[:key]).to match(/^#{default_context}\_.*/) expect(kwargs[:key]).to match(/^#{default_context}\_.*/)
end end
described_class.track_event_in_context(entity1, context_event, default_context) described_class.track_event_in_context(context_event, values: entity1, context: default_context)
end
it 'tracks events with multiple values' do
values = [entity1, entity2]
expect(Gitlab::Redis::HLL).to receive(:add).with(key: /g_{analytics}_contribution/, value: values, expiry: 84.days)
described_class.track_event_in_context(:g_analytics_contribution, values: values, context: default_context)
end end
end end
...@@ -186,7 +200,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -186,7 +200,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
it 'does not increment a counter' do it 'does not increment a counter' do
expect(Gitlab::Redis::HLL).not_to receive(:add) expect(Gitlab::Redis::HLL).not_to receive(:add)
described_class.track_event_in_context(entity1, context_event, '') described_class.track_event_in_context(context_event, values: entity1, context: '')
end end
end end
...@@ -194,7 +208,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -194,7 +208,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
it 'does not increment a counter' do it 'does not increment a counter' do
expect(Gitlab::Redis::HLL).not_to receive(:add) expect(Gitlab::Redis::HLL).not_to receive(:add)
described_class.track_event_in_context(entity1, context_event, invalid_context) described_class.track_event_in_context(context_event, values: entity1, context: invalid_context)
end end
end end
end end
...@@ -202,35 +216,35 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -202,35 +216,35 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
describe '.unique_events' do describe '.unique_events' do
before do before do
# events in current week, should not be counted as week is not complete # events in current week, should not be counted as week is not complete
described_class.track_event(entity1, weekly_event, Date.current) described_class.track_event(weekly_event, values: entity1, time: Date.current)
described_class.track_event(entity2, weekly_event, Date.current) described_class.track_event(weekly_event, values: entity2, time: Date.current)
# Events last week # Events last week
described_class.track_event(entity1, weekly_event, 2.days.ago) described_class.track_event(weekly_event, values: entity1, time: 2.days.ago)
described_class.track_event(entity1, weekly_event, 2.days.ago) described_class.track_event(weekly_event, values: entity1, time: 2.days.ago)
described_class.track_event(entity1, no_slot, 2.days.ago) described_class.track_event(no_slot, values: entity1, time: 2.days.ago)
# Events 2 weeks ago # Events 2 weeks ago
described_class.track_event(entity1, weekly_event, 2.weeks.ago) described_class.track_event(weekly_event, values: entity1, time: 2.weeks.ago)
# Events 4 weeks ago # Events 4 weeks ago
described_class.track_event(entity3, weekly_event, 4.weeks.ago) described_class.track_event(weekly_event, values: entity3, time: 4.weeks.ago)
described_class.track_event(entity4, weekly_event, 29.days.ago) described_class.track_event(weekly_event, values: entity4, time: 29.days.ago)
# events in current day should be counted in daily aggregation # events in current day should be counted in daily aggregation
described_class.track_event(entity1, daily_event, Date.current) described_class.track_event(daily_event, values: entity1, time: Date.current)
described_class.track_event(entity2, daily_event, Date.current) described_class.track_event(daily_event, values: entity2, time: Date.current)
# Events last week # Events last week
described_class.track_event(entity1, daily_event, 2.days.ago) described_class.track_event(daily_event, values: entity1, time: 2.days.ago)
described_class.track_event(entity1, daily_event, 2.days.ago) described_class.track_event(daily_event, values: entity1, time: 2.days.ago)
# Events 2 weeks ago # Events 2 weeks ago
described_class.track_event(entity1, daily_event, 14.days.ago) described_class.track_event(daily_event, values: entity1, time: 14.days.ago)
# Events 4 weeks ago # Events 4 weeks ago
described_class.track_event(entity3, daily_event, 28.days.ago) described_class.track_event(daily_event, values: entity3, time: 28.days.ago)
described_class.track_event(entity4, daily_event, 29.days.ago) described_class.track_event(daily_event, values: entity4, time: 29.days.ago)
end end
it 'raise error if metrics are not in the same slot' do it 'raise error if metrics are not in the same slot' do
...@@ -338,10 +352,10 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -338,10 +352,10 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
allow(described_class).to receive(:known_events).and_return(known_events) allow(described_class).to receive(:known_events).and_return(known_events)
allow(described_class).to receive(:categories).and_return(%w(category1 category2)) allow(described_class).to receive(:categories).and_return(%w(category1 category2))
described_class.track_event_in_context([entity1, entity3], 'event_name_1', default_context, 2.days.ago) described_class.track_event_in_context('event_name_1', values: [entity1, entity3], context: default_context, time: 2.days.ago)
described_class.track_event_in_context(entity3, 'event_name_1', default_context, 2.days.ago) described_class.track_event_in_context('event_name_1', values: entity3, context: default_context, time: 2.days.ago)
described_class.track_event_in_context(entity3, 'event_name_1', invalid_context, 2.days.ago) described_class.track_event_in_context('event_name_1', values: entity3, context: invalid_context, time: 2.days.ago)
described_class.track_event_in_context([entity1, entity2], 'event_name_2', '', 2.weeks.ago) described_class.track_event_in_context('event_name_2', values: [entity1, entity2], context: '', time: 2.weeks.ago)
end end
subject(:unique_events) { described_class.unique_events(event_names: event_names, start_date: 4.weeks.ago, end_date: Date.current, context: context) } subject(:unique_events) { described_class.unique_events(event_names: event_names, start_date: 4.weeks.ago, end_date: Date.current, context: context) }
...@@ -379,13 +393,13 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -379,13 +393,13 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
allow(described_class).to receive(:known_events).and_return(known_events) allow(described_class).to receive(:known_events).and_return(known_events)
allow(described_class).to receive(:categories).and_return(%w(category1 category2)) allow(described_class).to receive(:categories).and_return(%w(category1 category2))
described_class.track_event(entity1, 'event1_slot', 2.days.ago) described_class.track_event('event1_slot', values: entity1, time: 2.days.ago)
described_class.track_event(entity2, 'event2_slot', 2.days.ago) described_class.track_event('event2_slot', values: entity2, time: 2.days.ago)
described_class.track_event(entity3, 'event2_slot', 2.weeks.ago) described_class.track_event('event2_slot', values: entity3, time: 2.weeks.ago)
# events in different slots # events in different slots
described_class.track_event(entity2, 'event3', 2.days.ago) described_class.track_event('event3', values: entity2, time: 2.days.ago)
described_class.track_event(entity2, 'event4', 2.days.ago) described_class.track_event('event4', values: entity2, time: 2.days.ago)
end end
it 'returns the number of unique events for all known events' do it 'returns the number of unique events for all known events' do
...@@ -509,23 +523,23 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -509,23 +523,23 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
subject(:aggregated_metrics_data) { described_class.aggregated_metrics_weekly_data } subject(:aggregated_metrics_data) { described_class.aggregated_metrics_weekly_data }
before do before do
described_class.track_event(entity1, 'event1_slot', 2.days.ago) described_class.track_event('event1_slot', values: entity1, time: 2.days.ago)
described_class.track_event(entity2, 'event1_slot', 2.days.ago) described_class.track_event('event1_slot', values: entity2, time: 2.days.ago)
described_class.track_event(entity3, 'event1_slot', 2.days.ago) described_class.track_event('event1_slot', values: entity3, time: 2.days.ago)
described_class.track_event(entity1, 'event2_slot', 2.days.ago) described_class.track_event('event2_slot', values: entity1, time: 2.days.ago)
described_class.track_event(entity2, 'event2_slot', 3.days.ago) described_class.track_event('event2_slot', values: entity2, time: 3.days.ago)
described_class.track_event(entity3, 'event2_slot', 3.days.ago) described_class.track_event('event2_slot', values: entity3, time: 3.days.ago)
described_class.track_event(entity1, 'event3_slot', 3.days.ago) described_class.track_event('event3_slot', values: entity1, time: 3.days.ago)
described_class.track_event(entity2, 'event3_slot', 3.days.ago) described_class.track_event('event3_slot', values: entity2, time: 3.days.ago)
described_class.track_event(entity2, 'event5_slot', 3.days.ago) described_class.track_event('event5_slot', values: entity2, time: 3.days.ago)
# events out of time scope # events out of time scope
described_class.track_event(entity3, 'event2_slot', 8.days.ago) described_class.track_event('event2_slot', values: entity3, time: 8.days.ago)
# events in different slots # events in different slots
described_class.track_event(entity1, 'event4', 2.days.ago) described_class.track_event('event4', values: entity1, time: 2.days.ago)
described_class.track_event(entity2, 'event4', 2.days.ago) described_class.track_event('event4', values: entity2, time: 2.days.ago)
described_class.track_event(entity4, 'event4', 2.days.ago) described_class.track_event('event4', values: entity4, time: 2.days.ago)
end end
it_behaves_like 'aggregated_metrics_data' it_behaves_like 'aggregated_metrics_data'
...@@ -536,23 +550,23 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -536,23 +550,23 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
it_behaves_like 'aggregated_metrics_data' do it_behaves_like 'aggregated_metrics_data' do
before do before do
described_class.track_event(entity1, 'event1_slot', 2.days.ago) described_class.track_event('event1_slot', values: entity1, time: 2.days.ago)
described_class.track_event(entity2, 'event1_slot', 2.days.ago) described_class.track_event('event1_slot', values: entity2, time: 2.days.ago)
described_class.track_event(entity3, 'event1_slot', 2.days.ago) described_class.track_event('event1_slot', values: entity3, time: 2.days.ago)
described_class.track_event(entity1, 'event2_slot', 2.days.ago) described_class.track_event('event2_slot', values: entity1, time: 2.days.ago)
described_class.track_event(entity2, 'event2_slot', 3.days.ago) described_class.track_event('event2_slot', values: entity2, time: 3.days.ago)
described_class.track_event(entity3, 'event2_slot', 3.days.ago) described_class.track_event('event2_slot', values: entity3, time: 3.days.ago)
described_class.track_event(entity1, 'event3_slot', 3.days.ago) described_class.track_event('event3_slot', values: entity1, time: 3.days.ago)
described_class.track_event(entity2, 'event3_slot', 10.days.ago) described_class.track_event('event3_slot', values: entity2, time: 10.days.ago)
described_class.track_event(entity2, 'event5_slot', 4.weeks.ago.advance(days: 1)) described_class.track_event('event5_slot', values: entity2, time: 4.weeks.ago.advance(days: 1))
# events out of time scope # events out of time scope
described_class.track_event(entity1, 'event5_slot', 4.weeks.ago.advance(days: -1)) described_class.track_event('event5_slot', values: entity1, time: 4.weeks.ago.advance(days: -1))
# events in different slots # events in different slots
described_class.track_event(entity1, 'event4', 2.days.ago) described_class.track_event('event4', values: entity1, time: 2.days.ago)
described_class.track_event(entity2, 'event4', 2.days.ago) described_class.track_event('event4', values: entity2, time: 2.days.ago)
described_class.track_event(entity4, 'event4', 2.days.ago) described_class.track_event('event4', values: entity4, time: 2.days.ago)
end end
end end
......
...@@ -347,7 +347,7 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -347,7 +347,7 @@ RSpec.describe Gitlab::Utils::UsageData do
end end
it 'tracks redis hll event' do it 'tracks redis hll event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(value, event_name) expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(event_name, values: value)
described_class.track_usage_event(event_name, value) described_class.track_usage_event(event_name, value)
end end
......
...@@ -70,7 +70,7 @@ RSpec.describe Packages::CreateEventService do ...@@ -70,7 +70,7 @@ RSpec.describe Packages::CreateEventService do
end end
it 'tracks the event' do it 'tracks the event' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(user.id, /package/) expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(/package/, values: user.id)
subject subject
end end
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
RSpec.shared_examples 'tracking unique hll events' do |feature_flag| RSpec.shared_examples 'tracking unique hll events' do |feature_flag|
it 'tracks unique event' do it 'tracks unique event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(expected_type, target_id) expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(target_id, values: expected_type)
request request
end end
......
...@@ -13,7 +13,7 @@ RSpec.shared_examples 'an incident management tracked event' do |event| ...@@ -13,7 +13,7 @@ RSpec.shared_examples 'an incident management tracked event' do |event|
expect(Gitlab::UsageDataCounters::HLLRedisCounter) expect(Gitlab::UsageDataCounters::HLLRedisCounter)
.to receive(:track_event) .to receive(:track_event)
.with(current_user.id, event.to_s) .with(event.to_s, values: current_user.id)
.and_call_original .and_call_original
expect { subject } expect { subject }
......
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