class Bigcommerce::Lightstep::ActiveRecord::Tracer

Tracer adapter for ActiveRecord

Public Class Methods

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

@param [Bigcommerce::Lightstep::Tracer] tracer @param [String] span_prefix @param [Boolean] allow_root_spans @param [String] span_name

# 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

Public Instance Methods

active_span?() click to toggle source

@return [Boolean]

# 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:) { || ... } click to toggle source

Trace a DB call

@param [String] statement @param [String] host @param [String] adapter @param [String] database

# 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
key() click to toggle source

@return [String]

# File lib/bigcommerce/lightstep/active_record/tracer.rb, line 70
def key
  @span_prefix.to_s.empty? ? 'mysql' : "#{@span_prefix}.mysql"
end