Module: MetrixWire::Instrument::Redis

Defined in:
lib/metrixwire/instrument/redis.rb

Overview

Best-effort patch of the redis gem → cache custom spans. The redis gem funnels commands through the client's call/call_v, so we time those and tag them meta.kind='cache' with hit known for GET-style reads. Guarded: skips silently if the gem/const isn't present.

Defined Under Namespace

Modules: Patch

Constant Summary collapse

READS =
%w[get mget hget hmget hgetall exists].freeze

Class Method Summary collapse

Class Method Details

.command_name(command) ⇒ Object



50
51
52
53
54
55
# File 'lib/metrixwire/instrument/redis.rb', line 50

def command_name(command)
  c = command.is_a?(Array) ? command.first : command
  c.to_s.downcase
rescue StandardError
  "cmd"
end

.installObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/metrixwire/instrument/redis.rb', line 14

def install
  target = redis_client_class
  return if target.nil?
  return if target.instance_variable_get(:@__metrixwire_patched)

  target.prepend(Patch)
  target.instance_variable_set(:@__metrixwire_patched, true)
rescue StandardError
  nil
end

.record(op, duration_ms, result) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/metrixwire/instrument/redis.rb', line 57

def record(op, duration_ms, result)
  meta = { kind: "cache", op: op }
  meta[:hit] = !result.nil? if READS.include?(op)
  Helpers.add_span("custom", "cache #{op}", duration_ms, meta: meta, source_location: nil)
rescue StandardError
  nil
end

.redis_client_classObject

redis-rb >= 5 uses RedisClient; older versions ship Redis::Client.



26
27
28
29
30
31
32
33
34
# File 'lib/metrixwire/instrument/redis.rb', line 26

def redis_client_class
  if defined?(::RedisClient) && ::RedisClient.method_defined?(:call)
    ::RedisClient
  elsif defined?(::Redis::Client) && ::Redis::Client.method_defined?(:call)
    ::Redis::Client
  end
rescue StandardError
  nil
end