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

Constructor Details

#initialize(context) ⇒ Mongoid

Returns a new instance of Mongoid.



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

def initialize(context)
  @context = context
  @installed = false
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
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.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/scout_apm/instruments/mongoid.rb', line 96

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

#install(prepend:) ⇒ Object



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
80
81
82
83
84
# File 'lib/scout_apm/instruments/mongoid.rb', line 19

def install(prepend:)
  @installed = true

  # Mongoid versions that use Moped should instrument Moped.
  ### See moped instrument for Moped driven deploys
  if defined?(::Mongoid) and !defined?(::Moped)
    @installed = true

    if (mongoid_at_least_5?) && defined?(::Mongoid::Contextual::Mongo)
      logger.info "Instrumenting Mongoid"
      # 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}"

            # Between Mongo gem version 2.1 and 2.3, this method name was
            # changed. Accomodate both.  If for some reason neither is
            # there, try to continue with an empty "filter" hash.
            raw_filter = if view.respond_to?(:selector)
              view.selector
            elsif view.respond_to?(:filter)
              view.filter
            else
              {}
            end

            filter = ScoutApm::Instruments::Mongoid.anonymize_filter(raw_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)
        else
          logger.warn "Expected method #{method} not defined in Mongoid::Contextual::Mongo."
        end
      end
    end
  end
end

#installed?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/scout_apm/instruments/mongoid.rb', line 15

def installed?
  @installed
end

#loggerObject



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

def logger
  context.logger
end

#mongoid_at_least_5?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/scout_apm/instruments/mongoid.rb', line 86

def mongoid_at_least_5?
  if defined?(::Mongoid::VERSION)
    ::Mongoid::VERSION =~ /\A[56789]/
  else
    false
  end
end