redis_detailed.rb 1.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# frozen_string_literal: true

require 'redis'

module Gitlab
  module Peek
    module RedisInstrumented
      def call(*args, &block)
        start = Time.now
        super(*args, &block)
      ensure
        duration = (Time.now - start)
        add_call_details(duration, args)
      end

      private

      def add_call_details(duration, args)
        # redis-rb passes an array (e.g. [:get, key])
        return unless args.length == 1

        detail_store << {
          cmd: args.first,
          duration: duration,
25
          backtrace: ::Gitlab::Profiler.clean_backtrace(caller)
26 27 28 29 30 31 32 33 34 35 36 37
        }
      end

      def detail_store
        ::Gitlab::SafeRequestStore['redis_call_details'] ||= []
      end
    end
  end
end

module Peek
  module Views
Stan Hu's avatar
Stan Hu committed
38
    class RedisDetailed < DetailedView
39 40
      REDACTED_MARKER = "<redacted>"

41 42 43 44 45 46
      def key
        'redis'
      end

      private

47
      def format_call_details(call)
48
        super.merge(cmd: format_command(call[:cmd]))
49 50 51
      end

      def format_command(cmd)
52 53
        if cmd.length >= 2 && cmd.first =~ /^auth$/i
          cmd[-1] = REDACTED_MARKER
54 55
        # Scrub out the value of the SET calls to avoid binary
        # data or large data from spilling into the view
56 57
        elsif cmd.length >= 3 && cmd.first =~ /set/i
          cmd[2..-1] = REDACTED_MARKER
58 59 60 61 62 63 64 65 66 67 68
        end

        cmd.join(' ')
      end
    end
  end
end

class Redis::Client
  prepend Gitlab::Peek::RedisInstrumented
end