Module: OpenTelemetry::Instrumentation::Redis::Patches::Client

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

Overview

Module to prepend to Redis::Client for instrumentation

Instance Method Summary collapse

Instance Method Details

#process(commands) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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

def process(commands) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  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'] = config[:peer_service] if config[:peer_service]
  attributes.merge!(OpenTelemetry::Instrumentation::Redis.attributes)

  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

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

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