module Bigcommerce::Lightstep::ActiveRecord::Adapter
Patches mysql and ActiveRecord
to allow for mysql span tracing
Public Class Methods
enabled?()
click to toggle source
Note: we only support patching mysql2 gem at this point
@return [Boolean]
# File lib/bigcommerce/lightstep/active_record/adapter.rb, line 43 def self.enabled? defined?(::ActiveRecord) && ::Bigcommerce::Lightstep.active_record && ::ActiveRecord::Base.connection_config[:adapter].to_s.downcase == 'mysql2' rescue StandardError => e ::Bigcommerce::Lightstep.logger&.warn "Failed to determine ActiveRecord database adapter in bc-lightstep-ruby initializer: #{e.message}" false end
patch()
click to toggle source
Patch ActiveRecord
to enable mysql span traces
# File lib/bigcommerce/lightstep/active_record/adapter.rb, line 30 def self.patch return unless enabled? # rubocop:disable Lint/SendWithMixinArgument ::ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:include, ::Bigcommerce::Lightstep::ActiveRecord::Adapter) # rubocop:enable Lint/SendWithMixinArgument end
Public Instance Methods
execute_with_inst(sql, name = 'SQL')
click to toggle source
@param [String] sql The raw sql query @param [String] name The type of sql query
# File lib/bigcommerce/lightstep/active_record/adapter.rb, line 54 def execute_with_inst(sql, name = 'SQL') # bail out early if not enabled. This should not get here, but is provided as a failsafe. return execute_without_inst(sql, name) unless ::Bigcommerce::Lightstep.active_record sanitized_sql = lightstep_sanitize_sql(sql) name = name.to_s.strip.empty? ? 'QUERY' : name # we dont need to track all sql return execute_without_inst(sql, name) if lightstep_skip_tracing?(name, sanitized_sql) lightstep_tracer.db_trace( statement: sanitized_sql, host: @config[:host], adapter: @config[:adapter], database: @config[:database] ) do execute_without_inst(sql, name) end end
lightstep_sanitization_regexp()
click to toggle source
@return [Regexp]
# File lib/bigcommerce/lightstep/active_record/adapter.rb, line 87 def lightstep_sanitization_regexp @lightstep_sanitization_regexp ||= ::Regexp.new('(\'[\s\S][^\']*\'|\d*\.\d+|\d+|NULL)', ::Regexp::IGNORECASE) end
lightstep_sanitize_sql(sql)
click to toggle source
Sanitize the sql for safe logging
@param [String] @return [String]
# File lib/bigcommerce/lightstep/active_record/adapter.rb, line 80 def lightstep_sanitize_sql(sql) sql.to_s.gsub(lightstep_sanitization_regexp, '?').tr("\n", ' ').to_s end
lightstep_skip_tracing?(name, sql)
click to toggle source
Filter out sql queries from tracing we don't care about
@param [String] name @param [String] sql @return [Boolean]
# File lib/bigcommerce/lightstep/active_record/adapter.rb, line 97 def lightstep_skip_tracing?(name, sql) name.empty? || sql.empty? || sql.include?('COMMIT') || sql.include?('SCHEMA') || sql.include?('SHOW FULL FIELDS') end
lightstep_tracer()
click to toggle source
@return [::Bigcommerce::Lightstep::ActiveRecord::Tracer]
# File lib/bigcommerce/lightstep/active_record/adapter.rb, line 104 def lightstep_tracer @lightstep_tracer ||= ::Bigcommerce::Lightstep::ActiveRecord::Tracer.new end