0

I'm currently writing a moped log parser in order to monitor moped queries runtime. It's work great for QUERY command using the runtime parameter, but INSERT and UPDATE have no runtime parameter. All INSERT and UPDATE are followed by a getLastError COMMAND which contains a runtime.

Here are some samples of moped logs:

QUERY with runtime

MOPED: 127.0.0.1:27017 QUERY        database=X collection=X selector=X
flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.6950ms

INSERT without runtime but with COMMAND

MOPED: 127.0.0.1:27017 INSERT       database=X collection=X documents=X flags=[]
                       COMMAND      database=X command={:getlasterror=>1, :w=>1}
runtime: 0.4750ms

I'm pretty sure that COMMAND runtime is for the getlasterror call and not for my INSERT one. So is there a way to get this runtime info for an INSERT query?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Instead of using a log parser, I use something like this and it works great:

ActiveSupport::Notifications.subscribe('query.moped') do |name, start, finish, id, payload|
    runtime = (finish - start)*1000
    moped_ops = payload[:ops]
    moped_ops.each do |op|
      unless op.collection == '$cmd'
        query = op.class.name.split('::').last.downcase
        query = op.selector.first[0].to_s.gsub(/\$/, '') if query == 'command'
        DO SOMETHING WITH #{op.database}.#{op.collection}.#{query}
      end
    end

  end