Create Govern SMAU tracking foundation
Why are we doing this work
In Govern SMAU Metric, we want to add tracking of govern-related activities. The metric should report only unique users and should use user ID
as a tracking value, so that events tracked from frontend via /increment_unique_users
don't create duplicates. There is a PoC which explored the possible implementation.
We want to generate metrics for each page, aggregate metrics for each of the 5 categories, aggregate metrics for each of the 3 groups and one aggregate metric for the entire stage.
In this issue, we want to lay a foundation with a helper method to make it easier for the individual metrics to be consistently tracked.
Relevant links
Implementation plan
-
Create GovernUsageTracking
controller concern
# frozen_string_literal: true
module GovernUsageTracking
include ProductAnalyticsTracking
extend ActiveSupport::Concern
included do
def self.track_govern_activity(page_name, *controller_actions)
track_event(*controller_actions,
name: "users_visiting_#{page_name}",
action: 'user_perform_visit',
label: "redis_hll_counters.govern.users_visiting_#{page_name}_monthly",
destinations: %i[redis_hll snowplow]) { |context| context.current_user&.id }
end
end
end
-
Create concerns to simplify tracking on project and group levels
Group:
# frozen_string_literal: true
module GovernUsageGroupTracking
extend ActiveSupport::Concern
included do
include GovernUsageTracking
end
private
def tracking_namespace_source
group
end
def tracking_project_source
nil
end
end
Project:
# frozen_string_literal: true
module GovernUsageProjectTracking
extend ActiveSupport::Concern
included do
include GovernUsageTracking
end
private
def tracking_namespace_source
project.namespace
end
def tracking_project_source
project
end
end
-
Generate usage metric definition for the aggregated Govern metric:
bin/rails generate gitlab:usage_metric_definition:redis_hll govern users_visiting_govern_pages --ee
Use AggregatedMetric
as instrumentation class and include the existing events to aggregate:
instrumentation_class: AggregatedMetric
options:
aggregate:
operator: OR
attribute: user_id
events:
- i_code_review_merge_request_widget_security_reports_expand
-
Add the metric to the list of known_events:
# Govern stage
- name: users_visiting_govern_pages
aggregation: weekly
Verification steps
- Visit one of the pages or perform any of the actions covered by the new events
- In the
rails console
, check for a non-zero counter value for the metric:Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: '<event-name>', start_date: Date.current.beginning_of_week, end_date: Date.current.next_week)
- You run the service ping worker with
GitlabServicePingWorker.new.perform('triggered_from_cron' => false)
- Check for the
weekly
andmonthly
aggregated metric:RawUsageData.last.payload['redis_hll_counters']['govern']