module TingYun::Instrumentation::Support::ActiveRecordHelper

Constants

ACTIVE_RECORD
DATA_MAPPER
DEFAULT_PRODUCT_NAME
EMPTY
OPERATION_NAMES

These are used primarily to optimize and avoid allocation on well known operations coming in. Anything not matching the list is fine, it just needs to get downcased directly for use.

PRODUCT_NAMES
SPACE

Public Instance Methods

delete_all(*args, &blk) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 56
def delete_all(*args, &blk)
  ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
    delete_all_without_tingyun(*args, &blk)
  end
end
destroy_all(*args, &blk) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 64
def destroy_all(*args, &blk)
  ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
    destroy_all_without_tingyun(*args, &blk)
  end
end
instrument_additional_methods() click to toggle source

Used by both the AR 3.x and 4.x instrumentation

# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 18
def instrument_additional_methods
  instrument_save_methods
  instrument_relation_methods
end
instrument_relation_methods() click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 43
def instrument_relation_methods

  ::ActiveRecord::Relation.class_eval do
    alias_method :update_all_without_tingyun, :update_all

    def update_all(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
        update_all_without_tingyun(*args, &blk)
      end
    end

    alias_method :delete_all_without_tingyun, :delete_all

    def delete_all(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
        delete_all_without_tingyun(*args, &blk)
      end
    end

    alias_method :destroy_all_without_tingyun, :destroy_all

    def destroy_all(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
        destroy_all_without_tingyun(*args, &blk)
      end
    end
  end
end
instrument_save_methods() click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 23
def instrument_save_methods
  ::ActiveRecord::Base.class_eval do
    alias_method :save_without_tingyun, :save

    def save(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.class.name, nil, ACTIVE_RECORD) do
        save_without_tingyun(*args, &blk)
      end
    end

    alias_method :save_without_tingyun!, :save!

    def save!(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.class.name, nil, ACTIVE_RECORD) do
        save_without_tingyun!(*args, &blk)
      end
    end
  end
end
map_operation(raw_operation) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 143
def map_operation(raw_operation)
  direct_op = OPERATION_NAMES[raw_operation]
  return direct_op if direct_op

  # raw_operation.upcase
end
map_product(adapter_name) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 192
def map_product(adapter_name)
  PRODUCT_NAMES.fetch(adapter_name, DEFAULT_PRODUCT_NAME)
end
metrics_for(name, sql, config) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 72
def metrics_for(name, sql, config)
  config ||={}
  product = map_product(config[:adapter])
  splits = split_name(name)
  model = model_from_splits(splits) || product
  operation = operation_from_splits(splits, sql)
  config[:operation] = operation
  config[:product] = product
  config[:model] = model
  config[:type] = "Database"
  config
  # TingYun::Agent::Datastore::MetricHelper.metrics_for(product, operation, config[:host], config[:port], config[:database], model, ACTIVE_RECORD)
end
metrics_for_data_mapper(name, sql, config, model=nil) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 86
def metrics_for_data_mapper(name, sql, config, model=nil)
  if config
    product = map_product(config.query['adapter'])
    operation = name || TingYun::Instrumentation::Support::Database.parse_operation_from_query(sql)
    model ||= product
    db = config.query['database'] || config.path.split('/').last
    host = config.host
    port = config.port
    host = nil if config.host && config.host.empty?
    port = nil if config.host && config.host.empty?
    TingYun::Agent::Datastore::MetricHelper.metrics_for(product, operation, host, port, db, model, DATA_MAPPER)
  end
end
model_from_splits(splits) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 111
def model_from_splits(splits)
  if splits.length == 2
    splits.first
  else
    nil
  end
end
operation_from_splits(splits, sql) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 119
def operation_from_splits(splits, sql)
  if splits.length == 2
    map_operation(splits[1])
  else
    TingYun::Instrumentation::Support::Database.parse_operation_from_query(sql)
  end
end
save(*args, &blk) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 27
def save(*args, &blk)
  ::TingYun::Agent.with_database_metric_name(self.class.name, nil, ACTIVE_RECORD) do
    save_without_tingyun(*args, &blk)
  end
end
save!(*args, &blk) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 35
def save!(*args, &blk)
  ::TingYun::Agent.with_database_metric_name(self.class.name, nil, ACTIVE_RECORD) do
    save_without_tingyun!(*args, &blk)
  end
end
split_name(name) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 103
def split_name(name)
  if name && name.respond_to?(:split)
    name.split(SPACE)
  else
    EMPTY
  end
end
update_all(*args, &blk) click to toggle source
# File lib/ting_yun/instrumentation/support/active_record_helper.rb, line 48
def update_all(*args, &blk)
  ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
    update_all_without_tingyun(*args, &blk)
  end
end