Class: NewRelic::Agent::Instrumentation::MongodbCommandSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb

Constant Summary collapse

MONGODB =
'MongoDB'.freeze
COLLECTION =
'collection'.freeze
ERROR_KEYS =
%w[writeErrors writeConcernError writeConcernErrors].freeze

Instance Method Summary collapse

Instance Method Details

#completed(event) ⇒ Object Also known as: succeeded, failed



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb', line 34

def completed(event)
  begin
    return unless NewRelic::Agent::Tracer.tracing_enabled?

    segment = segments.delete(event.operation_id)
    return unless segment

    # operations that succeed but have errors return CommandSucceeded
    # with an error_key that is populated with error specifics
    if error_key = error_key_present?(event)
      # taking the last error as there can potentially be many
      attributes = event.reply[error_key][-1]
      segment.notice_error(Mongo::Error.new('%s (%s)' % [attributes['errmsg'], attributes['code']]))

    # failing commands return a CommandFailed event with an error message
    # in the form of "% (%s)" for the message and code
    elsif event.is_a?(Mongo::Monitoring::Event::CommandFailed)
      segment.notice_error(Mongo::Error.new(event.message))
    end
    segment.finish
  rescue Exception => e
    log_notification_error('completed', e)
  end
end

#error_key_present?(event) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb', line 26

def error_key_present?(event)
  if reply = event.reply
    ERROR_KEYS.detect { |key| reply[key] }
  end
rescue
  false
end

#started(event) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb', line 14

def started(event)
  begin
    return unless NewRelic::Agent::Tracer.tracing_enabled?

    segments[event.operation_id] = start_segment(event)
  rescue Exception => e
    log_notification_error('started', e)
  end
end