Class: Bigcommerce::Lightstep::ActiveRecord::Tracer

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

Overview

Tracer adapter for ActiveRecord

Instance Method Summary collapse

Constructor Details

#initialize(tracer: nil, span_prefix: nil, span_name: nil, allow_root_spans: nil) ⇒ Tracer

Returns a new instance of Tracer.

Parameters:

  • tracer (Bigcommerce::Lightstep::Tracer) (defaults to: nil)
  • span_prefix (String) (defaults to: nil)
  • allow_root_spans (Boolean) (defaults to: nil)
  • span_name (String) (defaults to: nil)


31
32
33
34
35
36
# File 'lib/bigcommerce/lightstep/active_record/tracer.rb', line 31

def initialize(tracer: nil, span_prefix: nil, span_name: nil, allow_root_spans: nil)
  @tracer = tracer || ::Bigcommerce::Lightstep::Tracer.instance
  @span_prefix = span_prefix || ::Bigcommerce::Lightstep.active_record_span_prefix
  @span_name = span_name || 'mysql'
  @allow_root_spans = allow_root_spans.nil? ? ::Bigcommerce::Lightstep.active_record_allow_root_spans : allow_root_spans
end

Instance Method Details

#active_span?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/bigcommerce/lightstep/active_record/tracer.rb', line 77

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

#db_trace(statement:, host:, adapter:, database:) ⇒ Object

Trace a DB call

Parameters:

  • statement (String)
  • host (String)
  • adapter (String)
  • database (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bigcommerce/lightstep/active_record/tracer.rb', line 46

def db_trace(statement:, host:, adapter:, database:)
  return yield unless @tracer

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

  @tracer.start_span(key) do |span|
    span.set_tag('db.host', host.to_s)
    span.set_tag('db.type', adapter.to_s)
    span.set_tag('db.name', database.to_s)
    span.set_tag('db.statement', statement.to_s)
    span.set_tag('span.kind', 'client')
    begin
      yield
    rescue StandardError => _e
      span.set_tag('error', true)
      raise # re-raise the error
    end
  end
end

#keyString

Returns:

  • (String)


70
71
72
# File 'lib/bigcommerce/lightstep/active_record/tracer.rb', line 70

def key
  @span_prefix.to_s.empty? ? 'mysql' : "#{@span_prefix}.mysql"
end