Commit 3f36a20a authored by Sean McGivern's avatar Sean McGivern

Merge branch '245247-check-for-usage-ping-enabled-when-tracking-using-redis-hll' into 'master'

Check for usage ping enabled when tracking using Redis HLL

Closes #245247

See merge request gitlab-org/gitlab!41562
parents 4601aae9 b6cb0e6e
---
title: Check if usage ping enabled for all tracking using Redis HLL
merge_request: 41562
author:
type: added
...@@ -34,6 +34,8 @@ module Gitlab ...@@ -34,6 +34,8 @@ module Gitlab
# * 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
def track_event(entity_id, event_name, time = Time.zone.now) def track_event(entity_id, event_name, time = Time.zone.now)
return unless Gitlab::CurrentSettings.usage_ping_enabled?
event = event_for(event_name) event = event_for(event_name)
raise UnknownEvent.new("Unknown event #{event_name}") unless event.present? raise UnknownEvent.new("Unknown event #{event_name}") unless event.present?
......
...@@ -62,65 +62,81 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s ...@@ -62,65 +62,81 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
end end
describe '.track_event' do describe '.track_event' do
it "raise error if metrics don't have same aggregation" do context 'when usage_ping is disabled' do
expect { described_class.track_event(entity1, different_aggregation, Date.current) } .to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownAggregation) it 'does not track the event' do
end stub_application_setting(usage_ping_enabled: false)
it 'raise error if metrics of unknown aggregation' do described_class.track_event(entity1, weekly_event, Date.current)
expect { described_class.track_event(entity1, 'unknown', Date.current) } .to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownEvent)
expect(Gitlab::Redis::HLL).not_to receive(:add)
end
end end
context 'for weekly events' do context 'when usage_ping is enabled' do
it 'sets the keys in Redis to expire automatically after the given expiry time' do before do
described_class.track_event(entity1, "g_analytics_contribution") stub_application_setting(usage_ping_enabled: true)
end
Gitlab::Redis::SharedState.with do |redis| it "raise error if metrics don't have same aggregation" do
keys = redis.scan_each(match: "g_{analytics}_contribution-*").to_a expect { described_class.track_event(entity1, different_aggregation, Date.current) } .to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownAggregation)
expect(keys).not_to be_empty end
keys.each do |key| it 'raise error if metrics of unknown aggregation' do
expect(redis.ttl(key)).to be_within(5.seconds).of(12.weeks) expect { described_class.track_event(entity1, 'unknown', Date.current) } .to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownEvent)
end
context 'for weekly events' do
it 'sets the keys in Redis to expire automatically after the given expiry time' do
described_class.track_event(entity1, "g_analytics_contribution")
Gitlab::Redis::SharedState.with do |redis|
keys = redis.scan_each(match: "g_{analytics}_contribution-*").to_a
expect(keys).not_to be_empty
keys.each do |key|
expect(redis.ttl(key)).to be_within(5.seconds).of(12.weeks)
end
end end
end 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(entity1, "g_compliance_dashboard")
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
expect(keys).not_to be_empty expect(keys).not_to be_empty
keys.each do |key| keys.each do |key|
expect(redis.ttl(key)).to be_within(5.seconds).of(6.weeks) expect(redis.ttl(key)).to be_within(5.seconds).of(6.weeks)
end
end end
end end
end end
end
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(entity1, "g_analytics_search")
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
expect(keys).not_to be_empty expect(keys).not_to be_empty
keys.each do |key| keys.each do |key|
expect(redis.ttl(key)).to be_within(5.seconds).of(84.days) expect(redis.ttl(key)).to be_within(5.seconds).of(84.days)
end
end end
end 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(entity1, "no_slot")
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
expect(keys).not_to be_empty expect(keys).not_to be_empty
keys.each do |key| keys.each do |key|
expect(redis.ttl(key)).to be_within(5.seconds).of(29.days) expect(redis.ttl(key)).to be_within(5.seconds).of(29.days)
end
end 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