Module: Datadog::Contrib::MongoDB::Patcher

Defined in:
lib/ddtrace/contrib/mongodb/patcher.rb

Overview

Patcher adds subscribers to the MongoDB driver so that each command is traced. Use the ‘Datadog::Monkey.patch_module(:mongodb)` to activate tracing for this module.

Class Method Summary collapse

Class Method Details

.add_mongo_monitoringObject



45
46
47
48
49
50
51
# File 'lib/ddtrace/contrib/mongodb/patcher.rb', line 45

def add_mongo_monitoring
  # Subscribe to all COMMAND queries with our subscriber class
  ::Mongo::Monitoring::Global.subscribe(
    ::Mongo::Monitoring::COMMAND,
    Datadog::Contrib::MongoDB::MongoCommandSubscriber.new
  )
end

.patchObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ddtrace/contrib/mongodb/patcher.rb', line 22

def patch
  # versions prior to 2.1.0 don't support the Monitoring API
  if !@patched && (defined?(::Mongo::Monitoring::Global) && \
          Gem::Version.new(::Mongo::VERSION) >= Gem::Version.new('2.1.0'))
    begin
      require 'ddtrace/pin'
      require 'ddtrace/ext/net'
      require 'ddtrace/ext/mongo'
      require 'ddtrace/ext/app_types'
      require 'ddtrace/contrib/mongodb/parsers'
      require 'ddtrace/contrib/mongodb/subscribers'

      patch_mongo_client()
      add_mongo_monitoring()

      @patched = true
    rescue StandardError => e
      Datadog::Tracer.log.error("Unable to apply MongoDB integration: #{e}")
    end
  end
  @patched
end

.patch_mongo_clientObject



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
# File 'lib/ddtrace/contrib/mongodb/patcher.rb', line 53

def patch_mongo_client
  ::Mongo::Client.class_eval do
    alias_method :initialize_without_datadog, :initialize
    Datadog::Monkey.without_warnings do
      remove_method :initialize
    end

    def initialize(*args, &blk)
      # attach the Pin instance
      initialize_without_datadog(*args, &blk)
      pin = Datadog::Pin.new(SERVICE, app: APP, app_type: Datadog::Ext::AppTypes::DB)
      pin.onto(self)
      if pin.tracer && pin.service
        pin.tracer.set_service_info(pin.service, 'mongodb', pin.app_type)
      end
    end

    def datadog_pin
      # safe-navigation to avoid crashes during each query
      return unless respond_to? :cluster
      return unless cluster.respond_to? :addresses
      return unless cluster.addresses.respond_to? :first
      Datadog::Pin.get_from(cluster.addresses.first)
    end

    def datadog_pin=(pin)
      # safe-navigation to avoid crashes during each query
      return unless respond_to? :cluster
      return unless cluster.respond_to? :addresses
      return unless cluster.addresses.respond_to? :each
      # attach the PIN to all cluster addresses. One of them is used
      # when executing a Command and it is attached to the Monitoring
      # Event instance.
      cluster.addresses.each { |x| pin.onto(x) }
    end
  end
end

.patched?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ddtrace/contrib/mongodb/patcher.rb', line 18

def patched?
  @patched
end