Class: StackifyRubyAPM::Spies::RedisSpy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/spies/redis.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



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
49
50
51
52
53
54
55
56
57
58
# File 'lib/stackify_apm/spies/redis.rb', line 12

def install
  ::Redis::Client.class_eval do
    alias_method 'call_without_apm', 'call'

    def call(command, &block)
      name = command[0].upcase.to_s
      type = 'db.redis'
      redis_details = command[1].is_a?(String) ? command[1].split(':') : []
      redis_nspace = !redis_details.blank? ? redis_details[0] : ''
      redis_key = ''

      # Checks CACHEKEY value
      if !redis_details.blank? && redis_details[1]
        # Initially sets the CACHEKEY value
        args = redis_details[1].split('/')
        redis_key = args[0]

        # If command has passed __method__, it will be included in the CACHEKEY value
        # Possible formats:
        # `<namespace:key/method_name/expires_in=300/ttl=60/>`
        # `<namespace:key/expires_in=300/ttl=60/>`
        redis_key = args[0..1].join('/') if args.length > 1 && !args[1].include?('=')
      end

      return call_without_apm(command, &block) if command[0] == :auth

      context = {
        PROVIDER: 'Redis',
        CATEGORY: 'Cache',
        SUBCATEGORY: 'Execute',
        COMPONENT_CATEGORY: 'Cache',
        COMPONENT_DETAIL: 'Execute',
        THREAD_ID: Thread.current.object_id,
        OPERATION: name
      }.tap do |hash|
        hash[:CACHEKEY] = redis_key unless redis_key.empty?
        hash[:CACHENAME] = redis_nspace unless redis_nspace.empty?
      end

      ctx = Span::Context.new(context)

      StackifyRubyAPM.span name, type, context: ctx do
        call_without_apm(command, &block)
      end
    end
  end
end