Class: Bigcommerce::Lightstep::Redis::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/bigcommerce/lightstep/redis/tracer.rb

Overview

Middleware tracer for Redis clients

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer: nil, excluded_commands: nil, allow_root_spans: nil) ⇒ Tracer

Returns a new instance of Tracer.

Parameters:

  • tracer (Bigcommerce::Lightstep::Tracer) (defaults to: nil)
  • excluded_commands (Array<String>) (defaults to: nil)
  • allow_root_spans (Boolean) (defaults to: nil)


34
35
36
37
38
39
40
41
42
# 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

Instance Attribute Details

#tracerObject

Returns the value of attribute tracer.



27
28
29
# File 'lib/bigcommerce/lightstep/redis/tracer.rb', line 27

def tracer
  @tracer
end

Instance Method Details

#active_span?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/bigcommerce/lightstep/redis/tracer.rb', line 96

def active_span?
  tracer.respond_to?(:active_span) && tracer.active_span
end

#excluded?(command) ⇒ Boolean

Parameters:

  • command (String)

Returns:

  • (Boolean)


89
90
91
# 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:) ⇒ Object

Parameters:

  • key (String)
  • statement (String)
  • instance (String)
  • host (String)
  • port (Integer)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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