Module: Redis::Instrumentation

Defined in:
lib/redis/instrumentation.rb

Constant Summary collapse

COMMON_TAGS =
{
  'span.kind' => 'client',
  'component' => 'ruby-redis',
  'db.type' => 'redis',
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.db_statement_lengthObject

Returns the value of attribute db_statement_length.



17
18
19
# File 'lib/redis/instrumentation.rb', line 17

def db_statement_length
  @db_statement_length
end

.tracerObject

Returns the value of attribute tracer.



16
17
18
# File 'lib/redis/instrumentation.rb', line 16

def tracer
  @tracer
end

Class Method Details

.instrument(tracer: OpenTracing.global_tracer, db_statement_length: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/redis/instrumentation.rb', line 19

def instrument(tracer: OpenTracing.global_tracer,
               db_statement_length: nil)
  begin
    require 'redis'
  rescue LoadError => e
    return
  end

  @tracer = tracer
  @db_statement_length = db_statement_length

  patch_client if !@patched_client
  @patched_client = true
end

.patch_clientObject



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/redis/instrumentation.rb', line 34

def patch_client
  ::Redis::Client.class_eval do
    alias_method :call_original, :call
    alias_method :call_pipeline_original, :call_pipeline

    def call(command, trace: true, &block)
      tags = ::Redis::Instrumentation::COMMON_TAGS.dup
      statement = command.join(' ')
      statement = statement.to_s[0, ::Redis::Instrumentation::db_statement_length] if ::Redis::Instrumentation::db_statement_length
      tags['db.statement'] = statement
      tags['db.instance'] = db
      tags['peer.address'] = "redis://#{host}:#{port}"

      # command[0] is usually the actual command name
      scope = ::Redis::Instrumentation.tracer.start_active_span("redis.#{command[0]}", tags: tags)

      call_original(command, &block)
    rescue => e
      if scope
        scope.span.record_exception(e)
      end
      raise e
    ensure
      scope.close if scope
    end

    def call_pipeline(pipeline)
      commands = pipeline.commands
      tags = ::Redis::Instrumentation::COMMON_TAGS.dup
      statement = commands.empty? ? "" : commands.map{ |arr| arr.join(' ') }.join(', ')
      statement = statement.to_s[0, ::Redis::Instrumentation::db_statement_length] if ::Redis::Instrumentation::db_statement_length
      tags['db.statement'] = statement
      tags['db.instance'] = db
      tags['peer.address'] = "redis://#{host}:#{port}"

      scope = ::Redis::Instrumentation.tracer.start_active_span("redis.pipelined", tags: tags)

      call_pipeline_original(pipeline)
    rescue => e
      if scope
        scope.span.set_tag("error", true)
        scope.span.log_kv("error.kind": e.class.name, message: e.message, "error.object": e)
      end
      raise e
    ensure
      scope.close if scope
    end
  end
end