Module: OneApm::Agent::Instrumentation::Memcache

Defined in:
lib/one_apm/inst/nosql/memcache.rb

Constant Summary collapse

OA_METHODS =
[:get, :get_multi, :set, :add, :incr, :decr, :delete, :replace, :append,
:prepend, :cas, :single_get, :multi_get, :single_cas, :multi_cas]

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


11
12
13
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

.supported_methods_for(client_class, methods) ⇒ Object



18
19
20
21
22
# 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