Commit c1ca4be4 authored by ggelatti's avatar ggelatti

Track pkg events using redis HLLCounter

Adds call to track package events using redis
(disabled by default).
parent 93842ae9
......@@ -24,7 +24,9 @@ class Packages::Event < ApplicationRecord
enum originator_type: { user: 0, deploy_token: 1, guest: 2 }
def self.event_name(event_scope, originator, event_type)
def self.allowed_event_name(event_scope, event_type, originator)
return unless event_allowed?(event_scope, event_type, originator)
# remove `package` from the event name to avoid issues with HLLRedisCounter class parsing
"i_package_#{event_scope}_#{originator}_#{event_type.gsub(/_packages?/, "")}"
end
......
......@@ -3,20 +3,30 @@
module Packages
class CreateEventService < BaseService
def execute
return unless Feature.enabled?(:collect_package_events, default_enabled: false)
event_scope = scope.is_a?(::Packages::Package) ? scope.package_type : scope
if Feature.enabled?(:collect_package_events_redis) && redis_event_name
::Gitlab::UsageDataCounters::HLLRedisCounter.track_event(current_user.id, redis_event_name)
end
::Packages::Event.create!(
event_type: event_name,
originator: current_user&.id,
originator_type: originator_type,
event_scope: event_scope
)
if Feature.enabled?(:collect_package_events)
::Packages::Event.create!(
event_type: event_name,
originator: current_user&.id,
originator_type: originator_type,
event_scope: event_scope
)
end
end
private
def redis_event_name
@redis_event_name ||= ::Packages::Event.allowed_event_name(event_scope, event_name, originator_type)
end
def event_scope
@event_scope ||= scope.is_a?(::Packages::Package) ? scope.package_type : scope
end
def scope
params[:scope]
end
......
---
name: collect_package_events_redis
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/46846
rollout_issue_url:
group: group::package
type: development
default_enabled: false
......@@ -24,9 +24,9 @@ namespace :gitlab do
def generate_unique_events_list
::Packages::Event::EVENT_SCOPES.keys.each_with_object([]) do |event_scope, events|
event_pairs.each do |event_type, originator|
if ::Packages::Event.event_allowed?(event_scope, event_type, originator)
if name = ::Packages::Event.allowed_event_name(event_scope, event_type, originator)
events << {
"name" => ::Packages::Event.event_name(event_scope, originator, event_type),
"name" => name,
"category" => "#{event_scope}_packages",
"aggregation" => "weekly",
"redis_slot" => "package"
......
......@@ -15,14 +15,18 @@ RSpec.describe Packages::CreateEventService do
subject { described_class.new(nil, user, params).execute }
describe '#execute' do
shared_examples 'package event creation' do |originator_type, expected_scope|
shared_examples 'db package event creation' do |originator_type, expected_scope|
before do
allow(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
end
context 'with feature flag disable' do
before do
stub_feature_flags(collect_package_events: false)
end
it 'returns nil' do
expect(subject).to be nil
it 'does not create an event object' do
expect { subject }.not_to change { Packages::Event.count }
end
end
......@@ -42,29 +46,61 @@ RSpec.describe Packages::CreateEventService do
end
end
shared_examples 'redis package event creation' do |originator_type, expected_scope|
context 'with feature flag disable' do
before do
stub_feature_flags(collect_package_events_redis: false)
end
it 'does not track the event' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
subject
end
end
it 'tracks the event' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(user.id, Packages::Event.allowed_event_name(expected_scope, event_name, originator_type))
subject
end
end
context 'with a user' do
let(:user) { create(:user) }
it_behaves_like 'package event creation', 'user', 'container'
it_behaves_like 'db package event creation', 'user', 'container'
it_behaves_like 'redis package event creation', 'user', 'container'
end
context 'with a deploy token' do
let(:user) { create(:deploy_token) }
it_behaves_like 'package event creation', 'deploy_token', 'container'
it_behaves_like 'db package event creation', 'deploy_token', 'container'
it_behaves_like 'redis package event creation', 'deploy_token', 'container'
end
context 'with no user' do
let(:user) { nil }
it_behaves_like 'package event creation', 'guest', 'container'
it_behaves_like 'db package event creation', 'guest', 'container'
end
context 'with a package as scope' do
let(:user) { nil }
let(:scope) { create(:npm_package) }
it_behaves_like 'package event creation', 'guest', 'npm'
context 'as guest' do
let(:user) { nil }
it_behaves_like 'db package event creation', 'guest', 'npm'
end
context 'with user' do
let(:user) { create(:user) }
it_behaves_like 'db package event creation', 'user', 'npm'
it_behaves_like 'redis package event creation', 'user', 'npm'
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