module Mmtrix::Agent::DataMapperTracing

Constants

DATA_MAPPER

Public Class Methods

add_tracer(clazz, method_name, operation_only = false) click to toggle source
# File lib/mmtrix/agent/instrumentation/data_mapper.rb, line 99
def self.add_tracer(clazz, method_name, operation_only = false)
  clazz.class_eval do
    if method_defined?(method_name) || private_method_defined?(method_name)
      define_method("#{method_name}_with_mmtrix",
                    Mmtrix::Agent::DataMapperTracing.method_body(clazz, method_name, operation_only))

      alias_method "#{method_name}_without_mmtrix", method_name
      alias_method method_name, "#{method_name}_with_mmtrix"
    end
  end
end
method_body(clazz, method_name, operation_only) click to toggle source
# File lib/mmtrix/agent/instrumentation/data_mapper.rb, line 113
def self.method_body(clazz, method_name, operation_only)
  use_model_name   = Mmtrix::Helper.instance_methods_include?(clazz, :model)
  metric_operation = method_name.to_s.gsub(/[!?]/, "")

  Proc.new do |*args, &blk|
    begin
      if operation_only
        # Used by direct SQL, like ::DataMapper::Adapters::DataObjectsAdapter#select
        name = nil
      elsif use_model_name
        # Used by ::DataMapper::Collection to get contained model name
        name = self.model.name
      elsif self.is_a?(Class)
        # Used by class-style access, like Model.first()
        name = self.name
      else
        # Used by instance-style access, like model.update(attr: "new")
        name = self.class.name
      end

      metrics = Mmtrix::Agent::Datastores::MetricHelper.metrics_for(
        DATA_MAPPER,
        metric_operation,
        name)

      Mmtrix::Agent::MethodTracer.trace_execution_scoped(metrics) do
        self.send("#{method_name}_without_mmtrix", *args, &blk)
      end
    end
  end
end