Skip to content
Snippets Groups Projects
Verified Commit 0fc109d6 authored by Max Fan's avatar Max Fan :two: Committed by GitLab
Browse files

Creating new event based metric for Ci Builds

This is due to current metrics being too slow and db intensive

Changelog: other
parent af5ac026
No related branches found
No related tags found
2 merge requests!144312Change service start (cut-off) date for code suggestions to March 15th,!141421Creating new event based metric for Ci Builds
......@@ -221,6 +221,7 @@ class Build < Ci::Processable
end
after_commit :track_ci_secrets_management_id_tokens_usage, on: :create, if: :id_tokens?
after_commit :track_ci_build_created_event, on: :create
class << self
# This is needed for url_for to work,
......@@ -1244,6 +1245,16 @@ def track_ci_secrets_management_id_tokens_usage
)
end
def track_ci_build_created_event
return unless Feature.enabled?(:track_ci_build_created_internal_event, project, type: :gitlab_com_derisk)
if user
Gitlab::InternalEvents.track_event('create_ci_build', project: project, user: user)
else
Gitlab::InternalEvents.track_event('create_ci_build', project: project)
end
end
def partition_id_prefix_in_16_bit_encode
"#{partition_id.to_s(16)}_"
end
......
---
description: Ci Build created
category: InternalEventTracking
action: create_ci_build
identifiers:
- project
- namespace
- user
product_section: ci
product_stage: verify
product_group: pipeline_execution
milestone: '16.9'
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/141421
distributions:
- ce
- ee
tiers:
- free
- premium
- ultimate
---
name: track_ci_build_created_internal_event
feature_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/429063
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/141421
rollout_issue_url: https://gitlab.com/gitlab-com/gl-infra/production/-/issues/17409
milestone: '16.9'
group: group::pipeline execution
type: gitlab_com_derisk
default_enabled: false
---
key_path: redis_hll_counters.count_distinct_user_id_from_create_ci_build_monthly
description: Monthly count of unique users triggering jobs
product_section: ci
product_stage: verify
product_group: pipeline_execution
performance_indicator_type: []
value_type: number
status: active
milestone: '16.9'
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/141421
time_frame: 28d
data_source: internal_events
data_category: optional
distribution:
- ce
- ee
tier:
- free
- premium
- ultimate
options:
events:
- create_ci_build
events:
- name: create_ci_build
unique: user.id
---
key_path: counts.count_total_create_ci_build_monthly
description: Monthly count of ci builds created across all projects and project types
product_section: ci
product_stage: verify
product_group: pipeline_execution
performance_indicator_type: []
value_type: number
status: active
milestone: '16.9'
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/141421
time_frame: 28d
data_source: internal_events
data_category: optional
distribution:
- ce
- ee
tier:
- free
- premium
- ultimate
options:
events:
- create_ci_build
events:
- name: create_ci_build
---
key_path: counts.count_total_create_ci_build
description: Total count of ci builds across all project and project types
product_section: ci
product_stage: verify
product_group: pipeline_execution
performance_indicator_type: []
value_type: number
status: active
milestone: '16.9'
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/141421
time_frame: all
data_source: internal_events
data_category: optional
distribution:
- ce
- ee
tier:
- free
- premium
- ultimate
options:
events:
- create_ci_build
events:
- name: create_ci_build
......@@ -819,6 +819,10 @@
context 'on create' do
let(:ci_build) { build(:ci_build, secrets: valid_secrets, user: user) }
before do
allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).and_call_original
end
it 'tracks RedisHLL event with user_id' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
.with('i_ci_secrets_management_vault_build_created', values: user.id)
......@@ -850,9 +854,9 @@
stub_feature_flags(usage_data_i_ci_secrets_management_vault_build_created: false)
end
it 'does not track RedisHLL event' do
# Events FF are checked inside track_event, so need to verify it on the next level
expect(::Gitlab::Redis::HLL).not_to receive(:add)
it 'does not track RedisHLL secrets management event' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
.with('i_ci_secrets_management_id_tokens_build_created', values: user.id)
ci_build.save!
end
......@@ -889,6 +893,7 @@
context 'on create' do
it 'does not track RedisHLL event' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
.with('i_ci_secrets_management_id_tokens_build_created', values: user.id)
ci_build.save!
end
......
......@@ -5,7 +5,7 @@
RSpec.describe Ci::Minutes::TrackLiveConsumptionService, :saas, feature_category: :continuous_integration do
let(:project) { create(:project, :private, shared_runners_enabled: true, namespace: namespace) }
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:namespace) { create(:namespace, shared_runners_minutes_limit: 100) }
let(:namespace) { create(:namespace_with_plan, plan: :default_plan, shared_runners_minutes_limit: 100) }
let(:user) { create(:user) }
let(:build) { create(:ci_build, :running, project: project, pipeline: pipeline, runner: runner, user: user) }
let(:runner) { create(:ci_runner, :instance) }
......@@ -127,17 +127,18 @@
it_behaves_like 'limit exceeded'
context 'when namespace is on a trial hosted plan' do
before do
create(:gitlab_subscription, :premium, :active_trial, namespace: namespace)
let(:namespace) do
create(:namespace_with_plan,
plan: :premium_plan,
trial_ends_on: Date.current.advance(days: 15),
shared_runners_minutes_limit: 100)
end
it_behaves_like 'limit exceeded'
end
context 'when namespace is on a paid plan' do
before do
create(:gitlab_subscription, :premium, namespace: namespace)
end
let(:namespace) { create(:namespace_with_plan, plan: :premium_plan, shared_runners_minutes_limit: 100) }
it_behaves_like 'limit exceeded'
end
......
......@@ -89,6 +89,47 @@
create(:ci_build, pipeline: pipeline)
end
end
context 'when running after_commit callbacks' do
context 'without user present' do
it 'tracks creation event' do
build = FactoryBot.build(:ci_build)
expect(Gitlab::InternalEvents).to receive(:track_event).with(
'create_ci_build',
project: build.project
)
build.save!
end
end
context 'with user present' do
it 'tracks creation event' do
build = FactoryBot.build(:ci_build, user: create(:user))
expect(Gitlab::InternalEvents).to receive(:track_event).with(
'create_ci_build',
project: build.project,
user: build.user
)
build.save!
end
end
context 'with FF track_ci_build_created_internal_event disabled' do
before do
stub_feature_flags(track_ci_build_created_internal_event: false)
end
it 'does not track creation event' do
expect(Gitlab::InternalEvents).not_to receive(:track_event)
create(:ci_build)
end
end
end
end
describe 'status' do
......@@ -5615,6 +5656,10 @@ def run_job_without_exception
context 'on create' do
let(:ci_build) { FactoryBot.build(:ci_build, user: user, id_tokens: { 'ID_TOKEN_1' => { aud: 'developers' } }) }
before do
allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).and_call_original
end
it 'tracks RedisHLL event with user_id' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
.with('i_ci_secrets_management_id_tokens_build_created', values: user.id)
......@@ -5664,6 +5709,7 @@ def run_job_without_exception
context 'on create' do
it 'does not track RedisHLL event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
.with('i_ci_secrets_management_id_tokens_build_created', values: user.id)
ci_build.save!
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment