class ElasticAPM::Spies::MongoSpy::Subscriber

@api private

Constants

ACTION
EVENT_KEY
SUBTYPE
TYPE

Public Instance Methods

events() click to toggle source
# File lib/elastic_apm/spies/mongo.rb, line 40
def events
  Thread.current[EVENT_KEY] ||= []
end
failed(event) click to toggle source
# File lib/elastic_apm/spies/mongo.rb, line 48
def failed(event)
  if (span = pop_event(event))
    span.outcome = Span::Outcome::FAILURE
  end

  span
end
started(event) click to toggle source
# File lib/elastic_apm/spies/mongo.rb, line 44
def started(event)
  push_event(event)
end
succeeded(event) click to toggle source
# File lib/elastic_apm/spies/mongo.rb, line 56
def succeeded(event)
  if span = pop_event(event)
    span.outcome = Span::Outcome::SUCCESS
  end

  span
end

Private Instance Methods

build_context(event) click to toggle source
# File lib/elastic_apm/spies/mongo.rb, line 103
def build_context(event)
  Span::Context.new(
    db: {
      instance: event.database_name,
      statement: event.command.to_s,
      type: 'mongodb',
      user: nil
    },
    destination: {
      service: {
        name: SUBTYPE,
        resource: SUBTYPE,
        type: TYPE
      }
    }
  )
end
pop_event(event) click to toggle source
# File lib/elastic_apm/spies/mongo.rb, line 97
def pop_event(event)
  return unless (curr = ElasticAPM.current_span)

  curr == events[-1] && ElasticAPM.end_span(events.pop)
end
push_event(event) click to toggle source
# File lib/elastic_apm/spies/mongo.rb, line 66
def push_event(event)
  return unless ElasticAPM.current_transaction
  # Some MongoDB commands are not on collections but rather are db
  # admin commands. For these commands, the value at the `command_name`
  # key is the integer 1.
  # For getMore commands, the value at `command_name` is the cursor id
  # and the collection name is at the key `collection`
  collection =
    if event.command[event.command_name] == 1 ||
      event.command[event.command_name].is_a?(BSON::Int64)
      event.command[:collection]
    else
      event.command[event.command_name]
    end

  name = [event.database_name,
          collection,
          event.command_name].compact.join('.')

  span =
    ElasticAPM.start_span(
      name,
      TYPE,
      subtype: SUBTYPE,
      action: ACTION,
      context: build_context(event)
    )

  events << span
end