class StackifyRubyAPM::Spies::MongoSpy::Subscriber

@api private

Constants

TYPE

Public Class Methods

new() click to toggle source
# File lib/stackify_apm/spies/mongo.rb, line 23
def initialize
  @events = {}
end

Public Instance Methods

failed(event) click to toggle source
# File lib/stackify_apm/spies/mongo.rb, line 31
def failed(event)
  pop_event(event)
end
started(event) click to toggle source
# File lib/stackify_apm/spies/mongo.rb, line 27
def started(event)
  push_event(event)
end
succeeded(event) click to toggle source
# File lib/stackify_apm/spies/mongo.rb, line 35
def succeeded(event)
  pop_event(event)
end

Private Instance Methods

pop_event(event) click to toggle source
# File lib/stackify_apm/spies/mongo.rb, line 69
def pop_event(event)
  return unless StackifyRubyAPM.current_transaction

  span = @events.delete(event.operation_id)
  span && span.done
end
push_event(event) click to toggle source
# File lib/stackify_apm/spies/mongo.rb, line 41
def push_event(event)
  return unless StackifyRubyAPM.current_transaction

  # Fetches collection name from Mongo event
  #
  document = event.command.find
  col = nil
  if document
    document.each_with_index do |val, idx|
      col = val[1] if idx.zero?
    end
  end

  # Builds span context
  #
  ctx = Span::Context.new(
    CATEGORY: 'MongoDB',
    MONGODB_COLLECTION: col,
    OPERATION: event.command_name.to_s,
    URL: event.address.to_s
  )

  # Creates new span from Mongo event
  #
  span = StackifyRubyAPM.span(event.command_name, TYPE, context: ctx)
  @events[event.operation_id] = span
end