Commit 984c628e authored by Gabriel Mazetto's avatar Gabriel Mazetto

Merge branch '235954-add-helper-for-tracking-redis-hll-events' into 'master'

Add helper for tracking Redis HLL events in controllers

See merge request gitlab-org/gitlab!39796
parents 2e6ee582 1efae6b6
# frozen_string_literal: true
# Example:
#
# # In controller include module
# # Track event for index action
#
# include RedisTracking
#
# track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score', feature: :my_feature
module RedisTracking
extend ActiveSupport::Concern
class_methods do
def track_redis_hll_event(*controller_actions, name:, feature:)
after_action only: controller_actions, if: -> { request.format.html? && request.headers['DNT'] != '1' } do
track_unique_redis_hll_event(name, feature)
end
end
end
private
def track_unique_redis_hll_event(event_name, feature)
return unless metric_feature_enabled?(feature)
return unless Gitlab::CurrentSettings.usage_ping_enabled?
return unless visitor_id
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(visitor_id, event_name)
end
def metric_feature_enabled?(feature)
Feature.enabled?(feature)
end
def visitor_id
return cookies[:visitor_id] if cookies[:visitor_id].present?
return unless current_user
uuid = SecureRandom.uuid
cookies[:visitor_id] = { value: uuid, expires: 24.months }
uuid
end
end
# frozen_string_literal: true
require "spec_helper"
RSpec.describe RedisTracking do
let(:event_name) { 'g_compliance_dashboard' }
let(:feature) { 'g_compliance_dashboard_feature' }
let(:user) { create(:user) }
controller(ApplicationController) do
include RedisTracking
skip_before_action :authenticate_user!, only: :show
track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score', feature: :g_compliance_dashboard_feature
def index
render html: 'index'
end
def new
render html: 'new'
end
def show
render html: 'show'
end
end
context 'with feature disabled' do
it 'does not track the event' do
stub_feature_flags(feature => false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
get :index
end
end
context 'with usage ping disabled' do
it 'does not track the event' do
stub_feature_flags(feature => true)
allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
get :index
end
end
context 'with feature enabled and usage ping enabled' do
before do
stub_feature_flags(feature => true)
allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(true)
end
context 'when user is logged in' do
it 'tracks the event' do
sign_in(user)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
get :index
end
end
context 'when user is not logged in and there is a visitor_id' do
let(:visitor_id) { SecureRandom.uuid }
before do
routes.draw { get 'show' => 'anonymous#show' }
end
it 'tracks the event' do
cookies[:visitor_id] = { value: visitor_id, expires: 24.months }
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
get :show
end
end
context 'when user is not logged in and there is no visitor_id' do
it 'does not tracks the event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
get :index
end
end
context 'for untracked action' do
it 'does not tracks the event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
get :new
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment