diff --git a/config/events/1674504208_API__PackagesHelpers_push_package_by_deploy_token.yml b/config/events/1674504208_API__PackagesHelpers_push_package_by_deploy_token.yml new file mode 100644 index 0000000000000000000000000000000000000000..76624a2389f7ad77270653f761f3ea9fce8ac6f6 --- /dev/null +++ b/config/events/1674504208_API__PackagesHelpers_push_package_by_deploy_token.yml @@ -0,0 +1,26 @@ +--- +description: "Mirrored Service Ping total metric key_path: counts.package_events_i_package_push_package_by_deploy_token" +category: package class +action: push_package_by_deploy_token +label_description: "Mirrored Service Ping total metric key_path: counts.package_events_i_package_push_package_by_deploy_token" +property_description: +value_description: +extra_properties: +identifiers: +- project +- user +- namespace +product_section: dev +product_stage: package +product_group: package_registry +product_category: package_registry +milestone: "15.9" +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/108798 +distributions: +- ce +- ee +tiers: +- free +- premium +- ultimate + diff --git a/lib/api/helpers/packages_helpers.rb b/lib/api/helpers/packages_helpers.rb index 1d35c3169138b6dba1d9ec5cb7a34cf8ed5d683a..46ee1167d324a0d0c13a327e31753ccf4cf9a2cd 100644 --- a/lib/api/helpers/packages_helpers.rb +++ b/lib/api/helpers/packages_helpers.rb @@ -97,12 +97,38 @@ def track_package_event(action, scope, **args) context: [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event_name).to_context], **args ) + + return unless Feature.enabled?(:route_hll_to_snowplow_phase3) + + track_push_package_by_deploy_token_event(action, category, args) end def present_package_file!(package_file, supports_direct_download: true) package_file.package.touch_last_downloaded_at present_carrierwave_file!(package_file.file, supports_direct_download: supports_direct_download) end + + private + + def track_push_package_by_deploy_token_event(action, category, args) + return unless action.to_s == 'push_package' && current_user.is_a?(DeployToken) + + event_name = 'i_package_push_package_by_deploy_token' + key_path = 'counts.package_events_i_package_push_package_by_deploy_token' + service_ping_context = Gitlab::Tracking::ServicePingContext.new( + data_source: :redis, + key_path: key_path + ).to_context + + Gitlab::Tracking.event( + category, + 'push_package_by_deploy_token', + property: event_name, + label: key_path, + context: [service_ping_context], + **args + ) + end end end end diff --git a/spec/lib/api/helpers/packages_helpers_spec.rb b/spec/lib/api/helpers/packages_helpers_spec.rb index de9d139a7b6eb5c0d43b0edeedd117ffcf5f3f31..bbb182071ccb88105ce5ae271c13f4dcf76906ca 100644 --- a/spec/lib/api/helpers/packages_helpers_spec.rb +++ b/spec/lib/api/helpers/packages_helpers_spec.rb @@ -275,7 +275,6 @@ let(:category) { described_class.name } let(:namespace) { project.namespace } let(:user) { project.creator } - let(:feature_flag_name) { nil } let(:label) { 'redis_hll_counters.user_packages.user_packages_total_unique_counts_monthly' } let(:property) { 'i_package_terraform_module_user' } @@ -284,5 +283,32 @@ helper.track_package_event(action, scope, **args) end end + + context 'when using deploy token and action is push package' do + let(:user) { create(:deploy_token, write_registry: true, projects: [project]) } + let(:scope) { :rubygems } + let(:category) { 'API::RubygemPackages' } + let(:namespace) { project.namespace } + let(:label) { 'counts.package_events_i_package_push_package_by_deploy_token' } + let(:property) { 'i_package_push_package_by_deploy_token' } + let(:service_ping_context) do + [Gitlab::Tracking::ServicePingContext.new(data_source: :redis, key_path: 'counts.package_events_i_package_push_package_by_deploy_token').to_h] + end + + it 'logs a snowplow event' do + args = { category: category, namespace: namespace, project: project } + helper.track_package_event('push_package', scope, **args) + + expect_snowplow_event( + category: category, + action: 'push_package_by_deploy_token', + context: service_ping_context, + label: label, + namespace: namespace, + property: property, + project: project + ) + end + end end end