From b04f2fede2f8b532fd193499d0504c95af9e4ea6 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Fri, 31 Jan 2025 17:21:13 +0100 Subject: [PATCH 1/2] Instrument the creation of CI runners Changelog: other --- .../ci/runners/create_runner_service.rb | 19 ++++++-- .../ci/runners/register_runner_service.rb | 21 ++++++--- config/events/create_ci_runner.yml | 23 +++++++++ ...nct_namespace_id_from_create_ci_runner.yml | 24 ++++++++++ ...ce_id_from_create_ci_runner_with_token.yml | 27 +++++++++++ ...tinct_project_id_from_create_ci_runner.yml | 24 ++++++++++ ...ct_id_from_create_ci_runner_with_token.yml | 27 +++++++++++ ...distinct_user_id_from_create_ci_runner.yml | 24 ++++++++++ ...er_id_from_create_ci_runner_with_token.yml | 26 ++++++++++ ...nt_total_ci_runners_created_with_token.yml | 26 ++++++++++ .../ci/runners/create_runner_service_spec.rb | 40 ++++++++++++++++ .../runners/register_runner_service_spec.rb | 47 +++++++++++++++++++ 12 files changed, 317 insertions(+), 11 deletions(-) create mode 100644 config/events/create_ci_runner.yml create mode 100644 config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner.yml create mode 100644 config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml create mode 100644 config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner.yml create mode 100644 config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml create mode 100644 config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner.yml create mode 100644 config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner_with_token.yml create mode 100644 config/metrics/counts_all/count_total_ci_runners_created_with_token.yml diff --git a/app/services/ci/runners/create_runner_service.rb b/app/services/ci/runners/create_runner_service.rb index 013cbec7f6a4b5..8fd2a769517d21 100644 --- a/app/services/ci/runners/create_runner_service.rb +++ b/app/services/ci/runners/create_runner_service.rb @@ -31,7 +31,7 @@ def execute runner = ::Ci::Runner.new(params) if runner.save - track_runner_event(runner) + track_runner_events(runner) return ServiceResponse.success(payload: { runner: runner }) end @@ -51,9 +51,7 @@ def normalize_params attr_reader :user, :scope, :params, :strategy - def track_runner_event(runner) - return if params[:maintenance_note].blank? - + def track_runner_events(runner) kwargs = { user: user } case runner.runner_type when 'group_type' @@ -62,11 +60,22 @@ def track_runner_event(runner) kwargs[:project] = @scope end + track_internal_event( + 'create_ci_runner', + **kwargs, + additional_properties: { + label: runner.runner_type, + property: 'authenticated_user' + } + ) + + return if params[:maintenance_note].blank? + track_internal_event( 'set_runner_maintenance_note', **kwargs, additional_properties: { - label: params[:runner_type] + label: runner.runner_type } ) end diff --git a/app/services/ci/runners/register_runner_service.rb b/app/services/ci/runners/register_runner_service.rb index 58e9a96395e79b..3c72119f6193fd 100644 --- a/app/services/ci/runners/register_runner_service.rb +++ b/app/services/ci/runners/register_runner_service.rb @@ -32,7 +32,7 @@ def execute end end - track_runner_event(runner) + track_runner_events(runner) ServiceResponse.success(payload: { runner: runner }) end @@ -89,9 +89,7 @@ def token_scope end end - def track_runner_event(runner) - return if attributes[:maintenance_note].blank? - + def track_runner_events(runner) kwargs = {} case runner.runner_type when 'group_type' @@ -101,12 +99,23 @@ def track_runner_event(runner) end track_internal_event( - 'set_runner_maintenance_note', + 'create_ci_runner', **kwargs, additional_properties: { - label: runner.runner_type + label: runner.runner_type, + property: 'registration_token' } ) + + if attributes[:maintenance_note].present? + track_internal_event( + 'set_runner_maintenance_note', + **kwargs, + additional_properties: { + label: runner.runner_type + } + ) + end end end end diff --git a/config/events/create_ci_runner.yml b/config/events/create_ci_runner.yml new file mode 100644 index 00000000000000..ebb2e2c786cf5b --- /dev/null +++ b/config/events/create_ci_runner.yml @@ -0,0 +1,23 @@ +--- +description: Tracks the number of CI runners created +internal_events: true +action: create_ci_runner +identifiers: +- project +- namespace +- user +additional_properties: + label: + description: The runner type, which can be `project_type`, `group_type`, or `instance_type` + property: + description: Registration type, which can be `registration_token` or `authenticated_user` +product_group: runner +product_categories: +- fleet_visibility +- runner +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +tiers: +- free +- premium +- ultimate diff --git a/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner.yml b/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner.yml new file mode 100644 index 00000000000000..11d699560a8736 --- /dev/null +++ b/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner.yml @@ -0,0 +1,24 @@ +--- +key_path: redis_hll_counters.count_distinct_namespace_id_from_create_ci_runner +description: Count of unique namespaces where a group runner was created +product_group: runner +product_categories: +- fleet_visibility +- runner +performance_indicator_type: [] +value_type: number +status: active +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +time_frame: +- 28d +- 7d +data_source: internal_events +data_category: optional +tiers: +- free +- premium +- ultimate +events: +- name: create_ci_runner + unique: namespace.id diff --git a/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml b/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml new file mode 100644 index 00000000000000..3bd7c53545f7ab --- /dev/null +++ b/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml @@ -0,0 +1,27 @@ +--- +key_path: redis_hll_counters.count_distinct_namespace_id_from_create_ci_runner_with_token +description: Count of unique groups where a group runner was created +product_group: runner +product_categories: +- fleet_visibility +- runner +performance_indicator_type: [] +value_type: number +status: active +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +time_frame: +- 28d +- 7d +data_source: internal_events +data_category: optional +tiers: +- free +- premium +- ultimate +events: +- name: create_ci_runner + unique: namespace.id + filter: + label: 'group_type' + property: 'registration_token' diff --git a/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner.yml b/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner.yml new file mode 100644 index 00000000000000..64e260210b5270 --- /dev/null +++ b/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner.yml @@ -0,0 +1,24 @@ +--- +key_path: redis_hll_counters.count_distinct_project_id_from_create_ci_runner +description: Count of unique projects where a project runner was created +product_group: runner +product_categories: +- fleet_visibility +- runner +performance_indicator_type: [] +value_type: number +status: active +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +time_frame: +- 28d +- 7d +data_source: internal_events +data_category: optional +tiers: +- free +- premium +- ultimate +events: +- name: create_ci_runner + unique: project.id diff --git a/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml b/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml new file mode 100644 index 00000000000000..56836ce246c125 --- /dev/null +++ b/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml @@ -0,0 +1,27 @@ +--- +key_path: redis_hll_counters.count_distinct_project_id_from_create_ci_runner_with_token +description: Count of unique projects where a project runner was registered using a registration token +product_group: runner +product_categories: +- fleet_visibility +- runner +performance_indicator_type: [] +value_type: number +status: active +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +time_frame: +- 28d +- 7d +data_source: internal_events +data_category: optional +tiers: +- free +- premium +- ultimate +events: +- name: create_ci_runner + unique: project.id + filter: + label: 'project_type' + property: 'registration_token' diff --git a/config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner.yml b/config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner.yml new file mode 100644 index 00000000000000..ef945af0ec9819 --- /dev/null +++ b/config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner.yml @@ -0,0 +1,24 @@ +--- +key_path: redis_hll_counters.count_distinct_user_id_from_create_ci_runner +description: Count of unique users who created a runner +product_group: runner +product_categories: +- fleet_visibility +- runner +performance_indicator_type: [] +value_type: number +status: active +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +time_frame: +- 28d +- 7d +data_source: internal_events +data_category: optional +tiers: +- free +- premium +- ultimate +events: +- name: create_ci_runner + unique: user.id diff --git a/config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner_with_token.yml b/config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner_with_token.yml new file mode 100644 index 00000000000000..d76a86d0136c8e --- /dev/null +++ b/config/metrics/counts_all/count_distinct_user_id_from_create_ci_runner_with_token.yml @@ -0,0 +1,26 @@ +--- +key_path: redis_hll_counters.count_distinct_user_id_from_create_ci_runner_with_token +description: Count of unique users who registered a runner using a runner registration token +product_group: runner +product_categories: +- fleet_visibility +- runner +performance_indicator_type: [] +value_type: number +status: active +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +time_frame: +- 28d +- 7d +data_source: internal_events +data_category: optional +tiers: +- free +- premium +- ultimate +events: +- name: create_ci_runner + unique: user.id + filter: + property: 'registration_token' diff --git a/config/metrics/counts_all/count_total_ci_runners_created_with_token.yml b/config/metrics/counts_all/count_total_ci_runners_created_with_token.yml new file mode 100644 index 00000000000000..91897d83fd3af9 --- /dev/null +++ b/config/metrics/counts_all/count_total_ci_runners_created_with_token.yml @@ -0,0 +1,26 @@ +--- +key_path: counts.count_total_ci_runners_created_with_token +description: Count of runners created using a runner registration token +product_group: runner +product_categories: +- fleet_visibility +- runner +performance_indicator_type: [] +value_type: number +status: active +milestone: '17.9' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/179876 +time_frame: +- 28d +- 7d +- all +data_source: internal_events +data_category: optional +tiers: +- free +- premium +- ultimate +events: +- name: create_ci_runner + filter: + property: 'registration_token' diff --git a/spec/services/ci/runners/create_runner_service_spec.rb b/spec/services/ci/runners/create_runner_service_spec.rb index b9a95efa44653e..a1b07f08de3697 100644 --- a/spec/services/ci/runners/create_runner_service_spec.rb +++ b/spec/services/ci/runners/create_runner_service_spec.rb @@ -132,6 +132,10 @@ end it { is_expected.to be_error } + + it 'does not track runner creation' do + expect { execute }.not_to trigger_internal_events('create_ci_runner') + end end shared_examples 'it can return an error' do @@ -175,6 +179,18 @@ it_behaves_like 'it can create a runner' it_behaves_like 'it can return an error' + it 'tracks internal events', :clean_gitlab_redis_shared_state do + expect { execute } + .to trigger_internal_events('create_ci_runner') + .with(user: current_user, additional_properties: { + label: expected_type, + property: 'authenticated_user' + }).and increment_usage_metrics( + 'redis_hll_counters.count_distinct_user_id_from_create_ci_runner_monthly', + 'redis_hll_counters.count_distinct_user_id_from_create_ci_runner_weekly' + ) + end + it 'does not track runner creation with maintenance note' do expect { execute }.not_to trigger_internal_events('set_runner_maintenance_note') end @@ -230,6 +246,18 @@ it_behaves_like 'it can create a runner' + it 'tracks internal events', :clean_gitlab_redis_shared_state do + expect { execute } + .to trigger_internal_events('create_ci_runner') + .with(namespace: group, user: current_user, additional_properties: { + label: expected_type, + property: 'authenticated_user' + }).and increment_usage_metrics( + 'redis_hll_counters.count_distinct_namespace_id_from_create_ci_runner_monthly', + 'redis_hll_counters.count_distinct_namespace_id_from_create_ci_runner_weekly' + ) + end + it 'populates sharding_key_id correctly' do expect(runner.sharding_key_id).to eq(group.id) end @@ -314,6 +342,18 @@ end it_behaves_like 'it can create a runner' + + it 'tracks internal events', :clean_gitlab_redis_shared_state do + expect { execute } + .to trigger_internal_events('create_ci_runner') + .with(project: project, user: current_user, additional_properties: { + label: expected_type, + property: 'authenticated_user' + }).and increment_usage_metrics( + 'redis_hll_counters.count_distinct_project_id_from_create_ci_runner_monthly', + 'redis_hll_counters.count_distinct_project_id_from_create_ci_runner_weekly' + ) + end end end diff --git a/spec/services/ci/runners/register_runner_service_spec.rb b/spec/services/ci/runners/register_runner_service_spec.rb index 2cc6155e971382..f8e9f5283228dd 100644 --- a/spec/services/ci/runners/register_runner_service_spec.rb +++ b/spec/services/ci/runners/register_runner_service_spec.rb @@ -23,6 +23,10 @@ expect(execute.message).to eq 'runner registration disallowed' expect(execute.reason).to eq :runner_registration_disallowed end + + it 'does not track runner creation' do + expect { execute }.not_to trigger_internal_events('create_ci_runner') + end end context 'when no token is provided' do @@ -33,6 +37,10 @@ expect(execute.message).to eq 'invalid token supplied' expect(execute.http_status).to eq :forbidden end + + it 'does not track runner creation' do + expect { execute }.not_to trigger_internal_events('create_ci_runner') + end end context 'when invalid token is provided' do @@ -43,6 +51,10 @@ expect(execute.message).to eq 'invalid token supplied' expect(execute.http_status).to eq :forbidden end + + it 'does not track runner creation' do + expect { execute }.not_to trigger_internal_events('create_ci_runner') + end end context 'when valid token is provided' do @@ -61,6 +73,17 @@ expect(runner).to be_instance_type end + it 'tracks internal events', :clean_gitlab_redis_shared_state do + expect { execute } + .to trigger_internal_events('create_ci_runner') + .with(additional_properties: { + label: 'instance_type', + property: 'registration_token' + }).and increment_usage_metrics( + 'counts.count_total_ci_runners_created_with_token' + ) + end + context 'when registering instance runners is disallowed' do before do stub_application_setting( @@ -160,6 +183,18 @@ expect { execute }.not_to trigger_internal_events('set_runner_maintenance_note') end + it 'tracks internal events', :clean_gitlab_redis_shared_state do + expect { execute } + .to trigger_internal_events('create_ci_runner') + .with(project: project, additional_properties: { + label: 'project_type', + property: 'registration_token' + }).and increment_usage_metrics( + 'redis_hll_counters.count_distinct_project_id_from_create_ci_runner_monthly', + 'redis_hll_counters.count_distinct_project_id_from_create_ci_runner_weekly' + ) + end + context 'when maintenance note is specified' do let(:args) { { maintenance_note: 'a note' } } @@ -259,6 +294,18 @@ expect { execute }.not_to trigger_internal_events('set_runner_maintenance_note') end + it 'tracks internal events', :clean_gitlab_redis_shared_state do + expect { execute } + .to trigger_internal_events('create_ci_runner') + .with(namespace: group, additional_properties: { + label: 'group_type', + property: 'registration_token' + }).and increment_usage_metrics( + 'redis_hll_counters.count_distinct_namespace_id_from_create_ci_runner_monthly', + 'redis_hll_counters.count_distinct_namespace_id_from_create_ci_runner_weekly' + ) + end + context 'when maintenance note is specified' do let(:args) { { maintenance_note: 'a note' } } -- GitLab From 95a3b4a890f0e1161f0568671a63d058f90e61ba Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Mon, 3 Feb 2025 09:42:10 +0100 Subject: [PATCH 2/2] Address MR review comments --- app/services/ci/runners/create_runner_service.rb | 1 + app/services/ci/runners/register_runner_service.rb | 1 + ...t_distinct_namespace_id_from_create_ci_runner_with_token.yml | 2 +- ...unt_distinct_project_id_from_create_ci_runner_with_token.yml | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/services/ci/runners/create_runner_service.rb b/app/services/ci/runners/create_runner_service.rb index 8fd2a769517d21..83d5f4a2d4facd 100644 --- a/app/services/ci/runners/create_runner_service.rb +++ b/app/services/ci/runners/create_runner_service.rb @@ -53,6 +53,7 @@ def normalize_params def track_runner_events(runner) kwargs = { user: user } + case runner.runner_type when 'group_type' kwargs[:namespace] = @scope diff --git a/app/services/ci/runners/register_runner_service.rb b/app/services/ci/runners/register_runner_service.rb index 3c72119f6193fd..2c35dc60ded990 100644 --- a/app/services/ci/runners/register_runner_service.rb +++ b/app/services/ci/runners/register_runner_service.rb @@ -91,6 +91,7 @@ def token_scope def track_runner_events(runner) kwargs = {} + case runner.runner_type when 'group_type' kwargs[:namespace] = token_scope diff --git a/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml b/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml index 3bd7c53545f7ab..87264caeee5b92 100644 --- a/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml +++ b/config/metrics/counts_all/count_distinct_namespace_id_from_create_ci_runner_with_token.yml @@ -1,6 +1,6 @@ --- key_path: redis_hll_counters.count_distinct_namespace_id_from_create_ci_runner_with_token -description: Count of unique groups where a group runner was created +description: Count of unique groups where a group runner was created using a runner registration token product_group: runner product_categories: - fleet_visibility diff --git a/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml b/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml index 56836ce246c125..7b389d6782e62c 100644 --- a/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml +++ b/config/metrics/counts_all/count_distinct_project_id_from_create_ci_runner_with_token.yml @@ -1,6 +1,6 @@ --- key_path: redis_hll_counters.count_distinct_project_id_from_create_ci_runner_with_token -description: Count of unique projects where a project runner was registered using a registration token +description: Count of unique projects where a project runner was created using a runner registration token product_group: runner product_categories: - fleet_visibility -- GitLab