Sha256: 8b60ecb209c785fbcb7b9bd2bd0ce0e72aceff3ebb26c7fc00422e2d04f38f1b

Contents?: true

Size: 1.43 KB

Versions: 17

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  # @api private
  module Spies
    # @api private
    class MongoSpy
      def install
        ::Mongo::Monitoring::Global.subscribe(
          ::Mongo::Monitoring::COMMAND,
          Subscriber.new
        )
      end

      # @api private
      class Subscriber
        TYPE = 'db.mongodb.query'

        def initialize
          @events = {}
        end

        def started(event)
          push_event(event)
        end

        def failed(event)
          pop_event(event)
        end

        def succeeded(event)
          pop_event(event)
        end

        private

        def push_event(event)
          return unless ElasticAPM.current_transaction

          span =
            ElasticAPM.start_span(
              event.command_name.to_s,
              TYPE,
              context: build_context(event)
            )

          @events[event.operation_id] = span
        end

        def pop_event(event)
          return unless (curr = ElasticAPM.current_span)
          span = @events.delete(event.operation_id)

          curr == span && ElasticAPM.end_span
        end

        def build_context(event)
          Span::Context.new(
            db: {
              instance: event.database_name,
              statement: nil,
              type: 'mongodb',
              user: nil
            }
          )
        end
      end
    end

    register 'Mongo', 'mongo', MongoSpy.new
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
elastic-apm-2.9.1 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.9.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.8.1 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.8.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.7.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.6.1 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.6.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.5.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.4.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.3.1 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.3.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.2.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.1.2 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.1.1 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.1.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.0.1 lib/elastic_apm/spies/mongo.rb
elastic-apm-2.0.0 lib/elastic_apm/spies/mongo.rb