Module: TraceView::Inst::Dalli

Includes:
API::Memcache
Defined in:
lib/traceview/inst/dalli.rb

Constant Summary

Constants included from API::Memcache

API::Memcache::MEMCACHE_OPS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API::Memcache

#memcache_hit?, #remote_host

Class Method Details

.included(cls) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/traceview/inst/dalli.rb', line 9

def self.included(cls)
  cls.class_eval do
    TraceView.logger.info '[traceview/loading] Instrumenting memcache (dalli)' if TraceView::Config[:verbose]
    if ::Dalli::Client.private_method_defined? :perform
      alias perform_without_traceview perform
      alias perform perform_with_traceview
    else TraceView.logger.warn '[traceview/loading] Couldn\'t properly instrument Memcache (Dalli).  Partial traces may occur.'
    end

    if ::Dalli::Client.method_defined? :get_multi
      alias get_multi_without_traceview get_multi
      alias get_multi get_multi_with_traceview
    end
  end
end

Instance Method Details

#get_multi_with_traceview(*keys) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/traceview/inst/dalli.rb', line 52

def get_multi_with_traceview(*keys)
  return get_multi_without_traceview(keys) unless TraceView.tracing?

  info_kvs = {}

  begin
    info_kvs[:KVKeyCount] = keys.flatten.length
    info_kvs[:KVKeyCount] = (info_kvs[:KVKeyCount] - 1) if keys.last.is_a?(Hash) || keys.last.nil?
    if @servers.is_a?(Array) && !@servers.empty?
      info_kvs[:RemoteHost] = @servers.join(", ")
    end
  rescue
    TraceView.logger.debug "[traceview/debug] Error collecting info keys: #{e.message}"
    TraceView.logger.debug e.backtrace
  end

  TraceView::API.trace('memcache', { :KVOp => :get_multi }, :get_multi) do
    values = get_multi_without_traceview(keys)

    info_kvs[:KVHitCount] = values.length
    info_kvs[:Backtrace] = TraceView::API.backtrace if TraceView::Config[:dalli][:collect_backtraces]
    TraceView::API.log('memcache', 'info', info_kvs)

    values
  end
end

#perform_with_traceview(*all_args, &blk) ⇒ Object



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
# File 'lib/traceview/inst/dalli.rb', line 25

def perform_with_traceview(*all_args, &blk)
  op, key, *args = *all_args

  report_kvs = {}
  report_kvs[:KVOp] = op
  report_kvs[:KVKey] = key
  if @servers.is_a?(Array) && !@servers.empty?
    report_kvs[:RemoteHost] = @servers.join(", ")
  end

  if TraceView.tracing? && !TraceView.tracing_layer_op?(:get_multi)
    TraceView::API.trace('memcache', report_kvs) do
      result = perform_without_traceview(*all_args, &blk)

      # Clear the hash for a potential info event
      report_kvs.clear
      report_kvs[:KVHit] = memcache_hit?(result) if op == :get && key.class == String
      report_kvs[:Backtrace] = TraceView::API.backtrace if TraceView::Config[:dalli][:collect_backtraces]

      TraceView::API.log('memcache', 'info', report_kvs) unless report_kvs.empty?
      result
    end
  else
    perform_without_traceview(*all_args, &blk)
  end
end