Sha256: 2b08d8894ee15e93223b33f51b76a7797f63172fb4d84a739ec21fc51e59849b

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 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'.freeze

        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

          ctx = Span::Context.new(
            instance: event.database_name,
            statement: nil,
            type: 'mongodb'.freeze,
            user: nil
          )
          span = ElasticAPM.span(event.command_name, TYPE, context: ctx)
          @events[event.operation_id] = span
        end

        def pop_event(event)
          return unless ElasticAPM.current_transaction

          span = @events.delete(event.operation_id)
          span && span.done
        end
      end
    end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
elastic-apm-1.0.0.beta2 lib/elastic_apm/spies/mongo.rb
elastic-apm-1.0.0.beta1 lib/elastic_apm/spies/mongo.rb
elastic-apm-0.8.0 lib/elastic_apm/spies/mongo.rb
elastic-apm-0.7.4 lib/elastic_apm/spies/mongo.rb
elastic-apm-0.7.3 lib/elastic_apm/spies/mongo.rb
elastic-apm-0.7.2 lib/elastic_apm/spies/mongo.rb
elastic-apm-0.7.1 lib/elastic_apm/spies/mongo.rb
elastic-apm-0.7.0 lib/elastic_apm/spies/mongo.rb