module OneApm::Agent::Instrumentation::Memcache

Constants

OA_METHODS

Public Instance Methods

enabled?() click to toggle source
# File lib/one_apm/inst/nosql/memcache.rb, line 11
def enabled?
  !OneApm::Manager.config[:disable_memcache]
end
instrument_methods(client_class, requested_methods = OA_METHODS) click to toggle source
# File lib/one_apm/inst/nosql/memcache.rb, line 24
def instrument_methods(client_class, requested_methods = OA_METHODS)
  supported_methods_for(client_class, requested_methods).each do |method_name|

    visibility = OneApm::Helper.instance_method_visibility client_class, method_name
    method_name_without = :"#{method_name}_without_oneapm_trace"

    client_class.class_eval do
      alias_method method_name_without, method_name

      if defined?(::MemCache)
         def oneapm_product 
           @product ||= begin 
             s = Array(servers)
             host, port = s.first.host, s.first.port if s.first rescue nil
             OneApm::Agent::Datastore.oneapm_product("Memcached", host,  port)
           end
         end
         private :oneapm_product
      else
         def oneapm_product 
            @product ||= begin 
              s = Array(@servers || servers)
              host, port, weight = s.first.split(":") if s.first && s.first.to_s.include?(':') rescue nil
              OneApm::Agent::Datastore.oneapm_product("Memcached", host,  port)
            end
         end
         private :oneapm_product
      end
      
      define_method method_name do |*args, &block|
        metrics = Datastore::MetricHelper.metrics_for(oneapm_product, method_name)
        OneApm::Support::MethodTracer.trace_execution_scoped(metrics) do
          t0 = Time.now
          begin
            send method_name_without, *args, &block
          ensure
            if OneApm::Manager.config[:capture_memcache_keys]
              OneApm::Manager.agent.transaction_sampler.notice_nosql(args.first.inspect, (Time.now - t0).to_f) rescue nil
            end
          end
        end
      end

      send visibility, method_name
      send visibility, method_name_without
    end
  end
end
oneapm_product() click to toggle source
# File lib/one_apm/inst/nosql/memcache.rb, line 34
def oneapm_product 
  @product ||= begin 
    s = Array(servers)
    host, port = s.first.host, s.first.port if s.first rescue nil
    OneApm::Agent::Datastore.oneapm_product("Memcached", host,  port)
  end
end
supported_methods_for(client_class, methods) click to toggle source
# File lib/one_apm/inst/nosql/memcache.rb, line 18
def supported_methods_for(client_class, methods)
  methods.select do |method_name|
    client_class.method_defined?(method_name) || client_class.private_method_defined?(method_name)
  end
end