Module: Gitlab::PerformanceBar

Defined in:
lib/gitlab/performance_bar.rb,
lib/gitlab/performance_bar/with_top_level_warnings.rb,
lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb

Defined Under Namespace

Modules: RedisAdapterWhenPeekEnabled, WithTopLevelWarnings

Constant Summary collapse

ALLOWED_USER_IDS_KEY =
'performance_bar_allowed_user_ids:v2'
EXPIRY_TIME_L1_CACHE =
1.minute
EXPIRY_TIME_L2_CACHE =
5.minutes

Class Method Summary collapse

Class Method Details

.allowed_group_idObject


21
22
23
# File 'lib/gitlab/performance_bar.rb', line 21

def self.allowed_group_id
  Gitlab::CurrentSettings.performance_bar_allowed_group_id
end

.allowed_user_idsObject

rubocop: disable CodeReuse/ActiveRecord


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/performance_bar.rb', line 26

def self.allowed_user_ids
  l1_cache_backend.fetch(ALLOWED_USER_IDS_KEY, expires_in: EXPIRY_TIME_L1_CACHE) do
    l2_cache_backend.fetch(ALLOWED_USER_IDS_KEY, expires_in: EXPIRY_TIME_L2_CACHE) do
      group = Group.find_by_id(allowed_group_id)

      if group
        GroupMembersFinder.new(group).execute.pluck(:user_id)
      else
        []
      end
    end
  end
end

.enabled_for_request?Boolean

Returns:

  • (Boolean)

9
10
11
# File 'lib/gitlab/performance_bar.rb', line 9

def self.enabled_for_request?
  Gitlab::SafeRequestStore[:peek_enabled]
end

.enabled_for_user?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)

13
14
15
16
17
18
19
# File 'lib/gitlab/performance_bar.rb', line 13

def self.enabled_for_user?(user = nil)
  return true if Rails.env.development?
  return true if user&.admin?
  return false unless user && allowed_group_id

  allowed_user_ids.include?(user.id)
end

.expire_allowed_user_ids_cacheObject

rubocop: enable CodeReuse/ActiveRecord


41
42
43
44
# File 'lib/gitlab/performance_bar.rb', line 41

def self.expire_allowed_user_ids_cache
  l1_cache_backend.delete(ALLOWED_USER_IDS_KEY)
  l2_cache_backend.delete(ALLOWED_USER_IDS_KEY)
end

.l1_cache_backendObject


46
47
48
# File 'lib/gitlab/performance_bar.rb', line 46

def self.l1_cache_backend
  Gitlab::ProcessMemoryCache.cache_backend
end

.l2_cache_backendObject


50
51
52
# File 'lib/gitlab/performance_bar.rb', line 50

def self.l2_cache_backend
  Rails.cache
end