class NewRelic::Agent::Instrumentation::MongodbCommandSubscriber

Constants

COLLECTION
GET_MORE
MONGODB

Public Instance Methods

completed(event) click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 23
def completed(event)
  begin
    state = NewRelic::Agent::TransactionState.tl_get
    return unless state.is_execution_traced?
    started_event = operations.delete(event.operation_id)

    base, *other_metrics = metrics(started_event)

    NewRelic::Agent.instance.stats_engine.tl_record_scoped_and_unscoped_metrics(
      base, other_metrics, event.duration
    )

    notice_nosql_statement(state, started_event, base, event.duration)
  rescue Exception => e
    log_notification_error('completed', e)
  end
end
Also aliased as: succeeded, failed
failed(event)
Alias for: completed
started(event) click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 14
def started(event)
  begin
    return unless NewRelic::Agent.tl_is_execution_traced?
    operations[event.operation_id] = event
  rescue Exception => e
    log_notification_error('started', e)
  end
end
succeeded(event)
Alias for: completed

Private Instance Methods

collection(event) click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 46
def collection(event)
  if event.command_name == GET_MORE
    event.command[COLLECTION]
  else
    event.command.values.first
  end
end
generate_statement(event) click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 67
def generate_statement(event)
  NewRelic::Agent::Datastores::Mongo::EventFormatter.format(
    event.command_name,
    event.database_name,
    event.command
  )
end
log_notification_error(event_type, error) click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 58
def log_notification_error(event_type, error)
  NewRelic::Agent.logger.error("Error during MongoDB #{event_type} event:")
  NewRelic::Agent.logger.log_exception(:error, error)
end
metrics(event) click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 54
def metrics(event)
  NewRelic::Agent::Datastores::MetricHelper.metrics_for(MONGODB, event.command_name, collection(event))
end
notice_nosql_statement(state, event, metric, duration) click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 75
def notice_nosql_statement(state, event, metric, duration)
  end_time = Time.now.to_f

  stack  = state.traced_method_stack

  # enter transaction trace node
  frame = stack.push_frame(state, :mongo_db, end_time - duration)

  NewRelic::Agent.instance.transaction_sampler.notice_nosql_statement(
      generate_statement(event), duration
    )

  # exit transaction trace node
  stack.pop_frame(state, frame, metric, end_time)
end
operations() click to toggle source
# File lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb, line 63
def operations
  @operations ||= {}
end