Module: Datadog::Tracing::Contrib::Redis::Quantize

Defined in:
lib/datadog/tracing/contrib/redis/quantize.rb

Overview

Quantize contains Redis-specific resource quantization tools.

Constant Summary collapse

PLACEHOLDER =
"?"
TOO_LONG_MARK =
"..."
VALUE_MAX_LEN =
50
CMD_MAX_LEN =
500
AUTH_COMMANDS =
%w[AUTH auth].freeze
CONNECTION_SETUP_COMMANDS =
Set.new(%w[HELLO]).freeze
CLIENT_CONNECTION_SETUP_SUBCOMMANDS =
Set.new(%w[SETINFO SETNAME]).freeze
MULTI_VERB_COMMANDS =
Set.new(
  %w[
    ACL
    CLIENT
    CLUSTER
    COMMAND
    CONFIG
    DEBUG
    LATENCY
    MEMORY
  ]
).freeze

Class Method Summary collapse

Class Method Details

.connection_setup_command?(command_args) ⇒ Boolean

Identifies protocol-level connection bootstrap commands (HELLO, CLIENT SETINFO, CLIENT SETNAME) that redis-client sends as part of its per-connection handshake, so they can be excluded from pipeline resource naming. A HELLO carrying AUTH credentials is excluded from this so authentication remains visible as its own span.

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
# File 'lib/datadog/tracing/contrib/redis/quantize.rb', line 90

def connection_setup_command?(command_args)
  command_args = resolve_command_args(command_args)
  return false unless command_args.is_a?(Array) && !command_args.empty?
  return false if hello_auth_command?(command_args)

  verb = command_args.first.to_s.upcase
  return true if CONNECTION_SETUP_COMMANDS.include?(verb)

  verb == "CLIENT" && CLIENT_CONNECTION_SETUP_SUBCOMMANDS.include?(command_args[1].to_s.upcase)
end

.format_arg(arg) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/datadog/tracing/contrib/redis/quantize.rb', line 36

def format_arg(arg)
  str = Core::Utils.utf8_encode(arg, binary: true, placeholder: PLACEHOLDER)
  Core::Utils.truncate(str, VALUE_MAX_LEN, TOO_LONG_MARK)
rescue => e
  Datadog.logger.debug("non formattable Redis arg #{str}: #{e.class}: #{e.message}")
  PLACEHOLDER
end

.format_command_args(command_args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/datadog/tracing/contrib/redis/quantize.rb', line 44

def format_command_args(command_args)
  command_args = resolve_command_args(command_args)
  return "AUTH ?" if auth_command?(command_args) || hello_auth_command?(command_args)

  verb, *args = command_args.map { |x| format_arg(x) }
  Core::Utils.truncate("#{verb.upcase} #{args.join(" ")}", CMD_MAX_LEN, TOO_LONG_MARK)
end

.get_verb(command_args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/datadog/tracing/contrib/redis/quantize.rb', line 52

def get_verb(command_args)
  return unless command_args.is_a?(Array)

  return get_verb(command_args.first) if command_args.first.is_a?(Array)

  return "AUTH" if hello_auth_command?(command_args)

  verb = command_args.first.to_s.upcase
  return verb unless MULTI_VERB_COMMANDS.include?(verb) && command_args[1]

  "#{verb} #{command_args[1]}"
end