Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c1ca4be4
Commit
c1ca4be4
authored
Nov 07, 2020
by
ggelatti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Track pkg events using redis HLLCounter
Adds call to track package events using redis (disabled by default).
parent
93842ae9
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
20 deletions
+75
-20
app/models/packages/event.rb
app/models/packages/event.rb
+3
-1
app/services/packages/create_event_service.rb
app/services/packages/create_event_service.rb
+19
-9
config/feature_flags/development/collect_package_events_redis.yml
...eature_flags/development/collect_package_events_redis.yml
+7
-0
lib/tasks/gitlab/packages/events.rake
lib/tasks/gitlab/packages/events.rake
+2
-2
spec/services/packages/create_event_service_spec.rb
spec/services/packages/create_event_service_spec.rb
+44
-8
No files found.
app/models/packages/event.rb
View file @
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
...
...
app/services/packages/create_event_service.rb
View file @
c1ca4be4
...
@@ -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
)
e
vent_scope
=
scope
.
is_a?
(
::
Packages
::
Package
)
?
scope
.
package_type
:
scope
e
nd
::
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
...
...
config/feature_flags/development/collect_package_events_redis.yml
0 → 100644
View file @
c1ca4be4
---
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
lib/tasks/gitlab/packages/events.rake
View file @
c1ca4be4
...
@@ -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"
...
...
spec/services/packages/create_event_service_spec.rb
View file @
c1ca4be4
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment