summaryrefslogtreecommitdiff
path: root/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb
blob: bf8d4b202b607db98b7f319ffa99a2d291dd0efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

# Adapted from https://github.com/peek/peek/blob/master/lib/peek/adapters/redis.rb
module Gitlab
  module PerformanceBar
    module RedisAdapterWhenPeekEnabled
      def save(request_id)
        return unless ::Gitlab::PerformanceBar.enabled_for_request?
        return if request_id.blank?

        super

        enqueue_stats_job(request_id)
      end

      # schedules a job which parses peek profile data and adds them
      # to a structured log
      def enqueue_stats_job(request_id)
        return unless gather_stats?

        @client.sadd(GitlabPerformanceBarStatsWorker::STATS_KEY, request_id) # rubocop:disable Gitlab/ModuleWithInstanceVariables

        return unless uuid = Gitlab::ExclusiveLease.new(
          GitlabPerformanceBarStatsWorker::LEASE_KEY,
          timeout: GitlabPerformanceBarStatsWorker::LEASE_TIMEOUT
        ).try_obtain

        GitlabPerformanceBarStatsWorker.perform_in(GitlabPerformanceBarStatsWorker::WORKER_DELAY, uuid)
      end

      def gather_stats?
        return unless Feature.enabled?(:performance_bar_stats)

        Gitlab.com? || !Rails.env.production?
      end
    end
  end
end