Class: ScoutApm::Instruments::Mongoid
- Inherits:
-
Object
- Object
- ScoutApm::Instruments::Mongoid
- Defined in:
- lib/scout_apm/instruments/mongoid.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#initialize(context) ⇒ Mongoid
constructor
A new instance of Mongoid.
- #install ⇒ Object
- #installed? ⇒ Boolean
- #logger ⇒ Object
- #mongoid_v5? ⇒ Boolean
- #mongoid_v6? ⇒ Boolean
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
#context ⇒ Object (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.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/scout_apm/instruments/mongoid.rb', line 118 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 ⇒ 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 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/scout_apm/instruments/mongoid.rb', line 19 def install @installed = true # Mongoid versions that use Moped should instrument Moped. if defined?(::Mongoid) and !defined?(::Moped) logger.info "Instrumenting Mongoid 2.x" @installed = true ### 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? || mongoid_v6?) && defined?(::Mongoid::Contextual::Mongo) logger.info "Instrumenting Mongoid 5.x/6.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}" # 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) end end end end end |
#installed? ⇒ Boolean
15 16 17 |
# File 'lib/scout_apm/instruments/mongoid.rb', line 15 def installed? @installed end |
#logger ⇒ Object
11 12 13 |
# File 'lib/scout_apm/instruments/mongoid.rb', line 11 def logger context.logger end |
#mongoid_v5? ⇒ Boolean
99 100 101 102 103 104 105 |
# File 'lib/scout_apm/instruments/mongoid.rb', line 99 def mongoid_v5? if defined?(::Mongoid::VERSION) ::Mongoid::VERSION =~ /\A5/ else false end end |
#mongoid_v6? ⇒ Boolean
107 108 109 110 111 112 113 |
# File 'lib/scout_apm/instruments/mongoid.rb', line 107 def mongoid_v6? if defined?(::Mongoid::VERSION) ::Mongoid::VERSION =~ /\A6/ else false end end |