summaryrefslogtreecommitdiff
path: root/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb
blob: ac5c907465ecdb131007d6ab2875368e4b718851 (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
39
40
41
42
43
44
45
46
47
48
# 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
      # rubocop:disable Gitlab/ModuleWithInstanceVariables
      def enqueue_stats_job(request_id)
        return unless Feature.enabled?(:performance_bar_stats)

        @client.sadd(GitlabPerformanceBarStatsWorker::STATS_KEY, request_id)

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

        # stats key should be periodically processed and deleted by
        # GitlabPerformanceBarStatsWorker but if it doesn't happen for
        # some reason, we set expiration for the stats key to avoid
        # keeping millions of request ids which would be already expired
        # anyway
        # rubocop:disable Gitlab/ModuleWithInstanceVariables
        @client.expire(
          GitlabPerformanceBarStatsWorker::STATS_KEY,
          GitlabPerformanceBarStatsWorker::STATS_KEY_EXPIRE
        )

        GitlabPerformanceBarStatsWorker.perform_in(
          GitlabPerformanceBarStatsWorker::WORKER_DELAY,
          uuid
        )
      end
      # rubocop:enable Gitlab/ModuleWithInstanceVariables
    end
  end
end