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



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
# File 'lib/ting_yun/instrumentation/mongo.rb', line 24

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

    def record_mongo_duration(duration)
      state = TingYun::Agent::TransactionState.tl_get
      if state
        state.timings.mon_duration = state.timings.mon_duration +  duration * 1000
      end
    end

    def tingyun_host_port
     return @db.connection.host_port if self.instance_variable_defined? :@db
     return @host_to_try if self.instance_variable_defined? :@host_to_try
     return ['Unknown', 'Unknown']
    end

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

    def instrument_with_tingyun(name, payload = {}, &block)
      klass_name, *metrics = tingyun_generate_metrics(name, payload)

      TingYun::Agent::MethodTracerHelpers.trace_execution_scoped(metrics, payload, method(:record_mongo_duration), klass_name) do
        instrument_without_tingyun(name, payload, &block)
      end
    end

    alias_method :instrument_without_tingyun, :instrument
    alias_method :instrument, :instrument_with_tingyun
  end
end

#hook_instrument_methodsObject



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

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
# File 'lib/ting_yun/instrumentation/mongo.rb', line 11

def install_mongo_instrumentation
  hook_instrument_methods
  instrument
end

#instrumentObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ting_yun/instrumentation/mongo.rb', line 59

def instrument
  ::Mongo::Collection.class_eval do
    def save_with_tingyun(doc, opts = {}, &block)
      klass_name, *metrics = tingyun_generate_metrics(:save)
      TingYun::Agent::MethodTracerHelpers.trace_execution_scoped(metrics, opts, method(:record_mongo_duration), klass_name) do
        save_without_tingyun(doc, opts, &block)
      end
    end

    alias_method :save_without_tingyun, :save
    alias_method :save, :save_with_tingyun

    def ensure_index_with_tingyun(spec, opts = {}, &block)
      klass_name, *metrics = tingyun_generate_metrics(:ensureIndex)
      TingYun::Agent::MethodTracerHelpers.trace_execution_scoped(metrics, opts, method(:record_mongo_duration), klass_name) do
        ensure_index_without_tingyun(spec, opts, &block)
      end
    end

    alias_method :ensure_index_without_tingyun, :ensure_index
    alias_method :ensure_index, :ensure_index_with_tingyun
  end
end