Class: ScoutApm::Instruments::Mongoid

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/instruments/mongoid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/scout_apm/instruments/mongoid.rb', line 4

def logger
  @logger
end

Class Method Details

.anonymize_filter(filter) ⇒ Object

Example of what a filter looks like: => “founded”=>{“$gte”=>“1980-1-1”, “name”=>“Deftones”, “Melvins”]} Approach: find every leaf-node, clear it. inspect the whole thing when done.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/scout_apm/instruments/mongoid.rb', line 92

def self.anonymize_filter(filter)
  Hash[
    filter.map do |k,v|
      if v.is_a? Hash
        [k, anonymize_filter(v)]
      else
        [k, "?"]
      end
    end
  ]
end

Instance Method Details

#initalize(logger = ScoutApm::Agent.instance.logger) ⇒ Object



6
7
8
9
# File 'lib/scout_apm/instruments/mongoid.rb', line 6

def initalize(logger=ScoutApm::Agent.instance.logger)
  @logger = logger
  @installed = false
end

#installObject



15
16
17
18
19
20
21
22
23
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
72
73
74
75
76
77
78
79
# File 'lib/scout_apm/instruments/mongoid.rb', line 15

def install
  @installed = true

  # Mongoid versions that use Moped should instrument Moped.
  if defined?(::Mongoid) and !defined?(::Moped)
    ScoutApm::Agent.instance.logger.info "Instrumenting Mongoid 2.x"

    ### OLD (2.x) mongoids
    if defined?(::Mongoid::Collection)
      ::Mongoid::Collection.class_eval do
        include ScoutApm::Tracer
        (::Mongoid::Collections::Operations::ALL - [:<<, :[]]).each do |method|
          instrument_method method, :type => "MongoDB", :name => '#{@klass}/' + method.to_s
        end
      end
    end

    ### See moped instrument for Moped driven deploys

    ### 5.x Mongoid
    if mongoid_v5? && defined?(::Mongoid::Contextual::Mongo)
      ScoutApm::Agent.instance.logger.info "Instrumenting Mongoid 5.x"
      # All the public methods from Mongoid::Contextual::Mongo.
      # TODO: Geo and MapReduce support (?). They are in other Contextual::* classes
      methods = [
        :count, :delete, :destroy, :distinct, :each,
        :explain, :find_first, :find_one_and_delete, :find_one_and_replace,
        :find_one_and_update, :first, :geo_near, :initialize, :last,
        :length, :limit, :map, :map_reduce, :pluck,
        :skip, :sort, :update, :update_all,
      ]
      # :exists?,

      methods.each do |method|
        if ::Mongoid::Contextual::Mongo.method_defined?(method)
          with_scout_instruments = %Q[
          def #{method}_with_scout_instruments(*args, &block)

            req = ScoutApm::RequestManager.lookup
            *db, collection = view.collection.namespace.split(".")

            name = collection + "/#{method}"
            filter = ScoutApm::Instruments::Mongoid.anonymize_filter(view.filter)

            layer = ScoutApm::Layer.new("MongoDB", name)
            layer.desc = filter.inspect

            req.start_layer( layer )
            begin
              #{method}_without_scout_instruments(*args, &block)
            ensure
              req.stop_layer
            end
          end

          alias_method :#{method}_without_scout_instruments, :#{method}
          alias_method :#{method}, :#{method}_with_scout_instruments
          ]

          ::Mongoid::Contextual::Mongo.class_eval(with_scout_instruments)
        end
      end
    end
  end
end

#installed?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/scout_apm/instruments/mongoid.rb', line 11

def installed?
  @installed
end

#mongoid_v5?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/scout_apm/instruments/mongoid.rb', line 81

def mongoid_v5?
  if defined?(::Mongoid::VERSION)
    ::Mongoid::VERSION =~ /\A5/
  else
    false
  end
end