Module: TingYun::Instrumentation::Mongo

Extended by:
Mongo
Included in:
Mongo
Defined in:
lib/ting_yun/instrumentation/mongo.rb

Instance Method Summary collapse

Instance Method Details

#hook_instrument_method(target_class) ⇒ 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
51
52
# File 'lib/ting_yun/instrumentation/mongo.rb', line 25

def hook_instrument_method(target_class)
  target_class.class_eval do
    require 'ting_yun/agent/method_tracer'

    def record_mongo_duration(duration)
      state = TingYun::Agent::TransactionState.tl_get
      unless state.nil?
        state.mon_duration += duration * 1000
      end
    end

    def ting_yun_generate_metrics(operation, payload = nil)
      payload ||= { :collection => self.name, :database => self.db.name }
      TingYun::Instrumentation::Support::MetricTranslator.metrics_for(operation, payload)
    end

    def instrument_with_ting_yun_trace(name, payload = {}, &block)
      metrics = ting_yun_generate_metrics(name, payload)

      TingYun::Agent::MethodTracer.trace_execution_scoped(metrics, payload, method(:record_mongo_duration)) do
        instrument_without_ting_yun_trace(name, payload, &block)
      end
    end

    alias_method :instrument_without_ting_yun_trace, :instrument
    alias_method :instrument, :instrument_with_ting_yun_trace
  end
end

#hook_instrument_methodsObject



17
18
19
20
21
22
# File 'lib/ting_yun/instrumentation/mongo.rb', line 17

def hook_instrument_methods
  hook_instrument_method(::Mongo::Collection)
  hook_instrument_method(::Mongo::Connection)
  hook_instrument_method(::Mongo::Cursor)
  hook_instrument_method(::Mongo::CollectionWriter) if defined?(::Mongo::CollectionWriter)
end

#install_mongo_instrumentationObject



11
12
13
14
15
# File 'lib/ting_yun/instrumentation/mongo.rb', line 11

def install_mongo_instrumentation
  hook_instrument_methods
  instrument_save
  instrument_ensure_index
end

#instrument_ensure_indexObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ting_yun/instrumentation/mongo.rb', line 68

def instrument_ensure_index
  ::Mongo::Collection.class_eval do
    def ensure_index_with_ting_yun_trace(spec, opts = {}, &block)
      metrics = ting_yun_generate_metrics(:ensureIndex)
      TingYun::Agent::MethodTracer.trace_execution_scoped(metrics, opts, method(:record_mongo_duration)) do
        ensure_index_with_out_ting_yun_trace(spec, opts, &block)
      end
    end

    alias_method :ensure_index_with_out_ting_yun_trace, :ensure_index
    alias_method :ensure_index, :ensure_index_with_ting_yun_trace
  end
end

#instrument_saveObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ting_yun/instrumentation/mongo.rb', line 54

def instrument_save
  ::Mongo::Collection.class_eval do
    def save_with_ting_yun_trace(doc, opts = {}, &block)
      metrics = ting_yun_generate_metrics(:save)
      TingYun::Agent::MethodTracer.trace_execution_scoped(metrics, opts, method(:record_mongo_duration)) do
        save_without_ting_yun_trace(doc, opts, &block)
      end
    end

    alias_method :save_without_ting_yun_trace, :save
    alias_method :save, :save_with_ting_yun_trace
  end
end