Commit 54735029 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '275942-track-pkg-events-with-redis' into 'master'

Track package events using redis

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