module Bigcommerce::Lightstep::Redis::Wrapper

Instrumentation wrapper for Redis

Public Class Methods

patch() click to toggle source
# File lib/bigcommerce/lightstep/redis/wrapper.rb, line 26
def patch
  require 'redis' # thread and fork safety
  return if @wrapped

  wrap unless @wrapped
  @wrapped = true
rescue ::LoadError => _e
  @wrapped = false
  # noop
end

Private Class Methods

bc_lightstep_tracer() click to toggle source

@return [::Bigcommerce::Lightstep::Redis::Tracer]

# File lib/bigcommerce/lightstep/redis/wrapper.rb, line 78
def bc_lightstep_tracer
  @bc_lightstep_tracer ||= ::Bigcommerce::Lightstep::Redis::Tracer.new
end
call(command, &block) click to toggle source
# File lib/bigcommerce/lightstep/redis/wrapper.rb, line 46
def call(command, &block)
  return call_original(command) unless bc_lightstep_tracer

  bc_lightstep_tracer.trace(
    key: "redis.#{command[0]}",
    statement: command.join(' '),
    instance: db,
    host: host,
    port: port
  ) do
    call_original(command, &block)
  end
end
call_pipeline(pipeline) click to toggle source
# File lib/bigcommerce/lightstep/redis/wrapper.rb, line 60
def call_pipeline(pipeline)
  return call_pipeline_original(pipeline) unless bc_lightstep_tracer

  commands = pipeline.try(:commands) || []
  bc_lightstep_tracer.trace(
    key: 'redis.pipelined',
    statement: commands.empty? ? '' : commands.map { |arr| arr.join(' ') }.join(', '),
    instance: db,
    host: host,
    port: port
  ) do
    call_pipeline_original(pipeline)
  end
end
wrap() click to toggle source
# File lib/bigcommerce/lightstep/redis/wrapper.rb, line 39
def wrap
  raise ::LoadError, 'Redis not loaded' unless defined?(::Redis::Client)

  ::Redis::Client.class_eval do
    alias_method :call_original, :call
    alias_method :call_pipeline_original, :call_pipeline

    def call(command, &block)
      return call_original(command) unless bc_lightstep_tracer

      bc_lightstep_tracer.trace(
        key: "redis.#{command[0]}",
        statement: command.join(' '),
        instance: db,
        host: host,
        port: port
      ) do
        call_original(command, &block)
      end
    end

    def call_pipeline(pipeline)
      return call_pipeline_original(pipeline) unless bc_lightstep_tracer

      commands = pipeline.try(:commands) || []
      bc_lightstep_tracer.trace(
        key: 'redis.pipelined',
        statement: commands.empty? ? '' : commands.map { |arr| arr.join(' ') }.join(', '),
        instance: db,
        host: host,
        port: port
      ) do
        call_pipeline_original(pipeline)
      end
    end

    ##
    # @return [::Bigcommerce::Lightstep::Redis::Tracer]
    #
    def bc_lightstep_tracer
      @bc_lightstep_tracer ||= ::Bigcommerce::Lightstep::Redis::Tracer.new
    end
  end
end