class Bigcommerce::Lightstep::Redis::Tracer

Middleware tracer for Redis clients

Attributes

tracer[RW]

Public Class Methods

new( tracer: nil, excluded_commands: nil, allow_root_spans: nil ) click to toggle source

@param [Bigcommerce::Lightstep::Tracer] tracer @param [Array<String>] excluded_commands @param [Boolean] allow_root_spans

# File lib/bigcommerce/lightstep/redis/tracer.rb, line 34
def initialize(
  tracer: nil,
  excluded_commands: nil,
  allow_root_spans: nil
)
  @tracer = tracer || ::Bigcommerce::Lightstep::Tracer.instance
  @excluded_commands = excluded_commands || ::Bigcommerce::Lightstep.redis_excluded_commands
  @allow_root_spans = allow_root_spans.nil? ? ::Bigcommerce::Lightstep.redis_allow_root_spans : allow_root_spans
end

Public Instance Methods

active_span?() click to toggle source

@return [Boolean]

# File lib/bigcommerce/lightstep/redis/tracer.rb, line 96
def active_span?
  tracer.respond_to?(:active_span) && tracer.active_span
end
excluded?(command) click to toggle source

@param [String] command @return [Boolean]

# File lib/bigcommerce/lightstep/redis/tracer.rb, line 89
def excluded?(command)
  @excluded_commands.include?(command.to_s)
end
trace(key:, statement:, instance:, host:, port:) { || ... } click to toggle source

@param [String] key @param [String] statement @param [String] instance @param [String] host @param [Integer] port

# File lib/bigcommerce/lightstep/redis/tracer.rb, line 51
def trace(key:, statement:, instance:, host:, port:)
  return yield unless @tracer

  # only take the command, not any arguments
  command = statement.to_s.split.first

  # skip excluded commands
  return yield if excluded?(command.to_s)

  # skip if not allowing root spans and there is no active span
  return yield if !@allow_root_spans && !active_span?

  tags = {
    'db.type' => 'redis',
    'db.statement' => command,
    'db.instance' => instance,
    'db.host' => "redis://#{host}:#{port}",
    'span.kind' => 'client'
  }

  @tracer.start_span(key) do |span|
    tags.each do |k, v|
      span.set_tag(k, v)
    end
    begin
      resp = yield
    rescue StandardError => _e
      span.set_tag('error', true)
      raise # re-raise the error
    end
    resp
  end
end