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
36aa8479
Commit
36aa8479
authored
Sep 04, 2020
by
alinamihaila
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add helper method for API
parent
9063dea6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
0 deletions
+84
-0
doc/development/telemetry/usage_ping.md
doc/development/telemetry/usage_ping.md
+23
-0
lib/api/helpers.rb
lib/api/helpers.rb
+13
-0
spec/lib/api/helpers_spec.rb
spec/lib/api/helpers_spec.rb
+48
-0
No files found.
doc/development/telemetry/usage_ping.md
View file @
36aa8479
...
@@ -288,6 +288,29 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
...
@@ -288,6 +288,29 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
end
end
```
```
1.
Track event in API using
`increment_unique_events(event_name, values)`
helper method
In order to be able to track the events usage ping should be enabled and event feature
`usage_data_<event_name>`
should be enabled.
Arguments:
-
`event_name`
: event name.
-
`values`
: values we count unique events
Example usage:
```
ruby
get
':id/registry/repositories'
do
repositories
=
ContainerRepositoriesFinder
.
new
(
user:
current_user
,
subject:
user_group
).
execute
increment_unique_events
(
'i_list_repositories'
,
current_user
.
id
)
present
paginate
(
repositories
),
with:
Entities
::
ContainerRegistry
::
Repository
,
tags:
params
[
:tags
],
tags_count:
params
[
:tags_count
]
end
```
1.
Track event using base module
`Gitlab::UsageDataCounters::HLLRedisCounter.track_event(entity_id, event_name)`
.
1.
Track event using base module
`Gitlab::UsageDataCounters::HLLRedisCounter.track_event(entity_id, event_name)`
.
Arguments:
Arguments:
...
...
lib/api/helpers.rb
View file @
36aa8479
...
@@ -537,6 +537,19 @@ module API
...
@@ -537,6 +537,19 @@ module API
)
)
end
end
def
increment_unique_values
(
event_name
,
values
)
feature_name
=
"usage_data_
#{
event_name
}
"
raise
"Feature
#{
feature_name
}
not enabled"
unless
Feature
.
enabled?
(
feature_name
)
raise
"Usage ping not enabled"
unless
Gitlab
::
CurrentSettings
.
usage_ping_enabled?
raise
"values is empty"
unless
values
.
present?
Gitlab
::
UsageDataCounters
::
HLLRedisCounter
.
track_event
(
values
,
event_name
)
rescue
=>
error
Rails
.
logger
.
warn
(
# rubocop:disable Gitlab/RailsLogger
"Redis tracking event failed for event:
#{
event_name
}
, message:
#{
error
.
message
}
"
)
end
def
with_api_params
(
&
block
)
def
with_api_params
(
&
block
)
yield
({
api:
true
,
request:
request
})
yield
({
api:
true
,
request:
request
})
end
end
...
...
spec/lib/api/helpers_spec.rb
View file @
36aa8479
...
@@ -189,6 +189,54 @@ RSpec.describe API::Helpers do
...
@@ -189,6 +189,54 @@ RSpec.describe API::Helpers do
end
end
end
end
describe
'#increment_unique_values'
do
let
(
:value
)
{
"9f302fea-f828-4ca9-aef4-e10bd723c0b3"
}
let
(
:event_name
)
{
'my_event'
}
let
(
:unknown_event
)
{
'unknown'
}
let
(
:feature
)
{
"usage_data_
#{
event_name
}
"
}
context
'with feature enabled'
do
before
do
stub_feature_flags
(
feature
=>
true
)
end
it
'tracks redis hll event'
do
stub_application_setting
(
usage_ping_enabled:
true
)
expect
(
Gitlab
::
UsageDataCounters
::
HLLRedisCounter
).
to
receive
(
:track_event
).
with
(
value
,
event_name
)
subject
.
increment_unique_values
(
event_name
,
value
)
end
it
'logs an exception if usage ping is not enabled'
do
stub_application_setting
(
usage_ping_enabled:
false
)
expect
(
Rails
.
logger
).
to
receive
(
:warn
).
with
(
"Redis tracking event failed for event:
#{
event_name
}
, message: Usage ping not enabled"
)
subject
.
increment_unique_values
(
event_name
,
value
)
end
it
'logs an exception for unknown event'
do
stub_application_setting
(
usage_ping_enabled:
true
)
expect
(
Rails
.
logger
).
to
receive
(
:warn
).
with
(
"Redis tracking event failed for event:
#{
unknown_event
}
, message: Unknown event
#{
unknown_event
}
"
)
subject
.
increment_unique_values
(
unknown_event
,
value
)
end
end
context
'with feature disabled'
do
before
do
stub_feature_flags
(
feature
=>
false
)
end
it
"logs an exception"
do
expect
(
Rails
.
logger
).
to
receive
(
:warn
).
with
(
"Redis tracking event failed for event:
#{
event_name
}
, message: Feature
#{
feature
}
not enabled"
)
subject
.
increment_unique_values
(
event_name
,
value
)
end
end
end
describe
'#order_options_with_tie_breaker'
do
describe
'#order_options_with_tie_breaker'
do
subject
{
Class
.
new
.
include
(
described_class
).
new
.
order_options_with_tie_breaker
}
subject
{
Class
.
new
.
include
(
described_class
).
new
.
order_options_with_tie_breaker
}
...
...
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