Class: Labkit::Tracing::Redis::RedisInterceptorHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/tracing/redis/redis_interceptor_helper.rb

Overview

RedisInterceptorHelper is a helper for the RedisInterceptor. This is not a public API

Constant Summary collapse

MASK_REDIS_RE =

For optimization, compile this once

/^([\w{}-]+(?:\W+[\w{}-]+(?:\W+[\w{}-]+)?)?)(.?)/.freeze

Class Method Summary collapse

Class Method Details

.call_pipeline_with_tracing(pipeline, client) ⇒ Object



19
20
21
22
23
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 19

def self.call_pipeline_with_tracing(pipeline, client)
  Labkit::Tracing::TracingUtils.with_tracing(operation_name: "redis.call_pipeline", tags: tags_from_pipeline(pipeline, client)) do |_span|
    yield
  end
end

.call_with_tracing(command, client) ⇒ Object



13
14
15
16
17
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 13

def self.call_with_tracing(command, client)
  Labkit::Tracing::TracingUtils.with_tracing(operation_name: "redis.call", tags: tags_from_command(command, client)) do |_span|
    yield
  end
end

.command_is?(command_name, command_symbol) ⇒ Boolean

Returns true if the command is equivalent to the command_symbol symbol

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 90

def self.command_is?(command_name, command_symbol)
  if command_name.is_a?(Symbol)
    command_name == command_symbol
  else
    command_name.to_s.casecmp(command_symbol.to_s).zero?
  end
end

.command_is_sensitive?(command_name) ⇒ Boolean

Returns true if the arguments for the command should be masked

Returns:

  • (Boolean)


85
86
87
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 85

def self.command_is_sensitive?(command_name)
  command_is?(command_name, :auth) || command_is?(command_name, :eval)
end

.command_serialized(command) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 44

def self.command_serialized(command)
  return "" unless command.is_a?(Array)
  return "" if command.empty?

  command_name, *arguments = command
  command_name ||= "nil"

  info = [command_name]
  info << sanitize_argument_for_command(command_name, arguments.first) unless arguments.empty?
  info << "...#{arguments.size - 1} more value(s)" if arguments.size > 1

  info.join(" ")
end

.common_tags_for_client(client) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 25

def self.common_tags_for_client(client)
  {
    "component" => "redis",
    "span.kind" => "client",
    "redis.scheme" => client.scheme,
    "redis.host" => client.host,
    "redis.port" => client.port,
    "redis.path" => client.path,
  }
end

.sanitize_argument_for_command(command_name, first_argument) ⇒ Object

get_first_argument_for_command returns a masked value representing the first argument from a redis command, taking care of certain sensitive commands



74
75
76
77
78
79
80
81
82
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 74

def self.sanitize_argument_for_command(command_name, first_argument)
  return "*****" if command_is_sensitive?(command_name)

  return "nil" if first_argument.nil?
  return first_argument if first_argument.is_a?(Numeric)
  return "*****" unless first_argument.is_a?(String)

  mask_redis_arg(first_argument)
end

.tags_from_command(command, client) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 36

def self.tags_from_command(command, client)
  tags = common_tags_for_client(client)

  tags["redis.command"] = command_serialized(command)

  tags
end

.tags_from_pipeline(pipeline, client) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/labkit/tracing/redis/redis_interceptor_helper.rb', line 58

def self.tags_from_pipeline(pipeline, client)
  tags = common_tags_for_client(client)

  commands = pipeline.commands

  # Limit to the first 5 commands
  commands.first(5).each_with_index do |command, index|
    tags["redis.command.#{index}"] = command_serialized(command)
  end
  tags["redis.pipeline.commands.length"] = commands.length

  tags
end