Module: OpenTelemetry::Instrumentation::Redis::Patches::RedisV4Client

Defined in:
lib/opentelemetry/instrumentation/redis/patches/redis_v4_client.rb

Overview

Module to prepend to Redis::Client for instrumentation

Instance Method Summary collapse

Instance Method Details

#process(commands) ⇒ Object



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
# File 'lib/opentelemetry/instrumentation/redis/patches/redis_v4_client.rb', line 16

def process(commands)
  return super unless instrumentation_config[:trace_root_spans] || OpenTelemetry::Trace.current_span.context.valid?

  host = options[:host]
  port = options[:port]

  attributes = {
    'db.system' => 'redis',
    'net.peer.name' => host,
    'net.peer.port' => port
  }

  attributes['db.redis.database_index'] = options[:db] unless options[:db].zero?
  attributes['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
  attributes.merge!(OpenTelemetry::Instrumentation::Redis.attributes)

  unless instrumentation_config[:db_statement] == :omit
    parsed_commands = parse_commands(commands)
    parsed_commands = OpenTelemetry::Common::Utilities.truncate(parsed_commands, MAX_STATEMENT_LENGTH)
    parsed_commands = OpenTelemetry::Common::Utilities.utf8_encode(parsed_commands, binary: true)
    attributes['db.statement'] = parsed_commands
  end

  span_name = if commands.length == 1
                commands[0][0].to_s.upcase
              else
                'PIPELINED'
              end

  instrumentation_tracer.in_span(span_name, attributes: attributes, kind: :client) do |s|
    super.tap do |reply|
      if reply.is_a?(::Redis::CommandError)
        s.record_exception(reply)
        s.status = Trace::Status.error(reply.message)
      end
    end
  end
end