Class: OpenTelemetry::Instrumentation::Mongo::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/mongo/subscriber.rb

Overview

Event handler class for Mongo Ruby driver

Constant Summary collapse

THREAD_KEY =
:__opentelemetry_mongo_spans__

Instance Method Summary collapse

Instance Method Details

#failed(event) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opentelemetry/instrumentation/mongo/subscriber.rb', line 44

def failed(event)
  finish_event('failed', event) do |span|
    if event.is_a?(::Mongo::Monitoring::Event::CommandFailed)
      span.add_event('exception',
                     attributes: {
                       'exception.type' => 'CommandFailed',
                       'exception.message' => event.message
                     })
    end
  end
end

#started(event) ⇒ Object

rubocop:disable Metrics/AbcSize



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/opentelemetry/instrumentation/mongo/subscriber.rb', line 16

def started(event) # rubocop:disable Metrics/AbcSize
  # start a trace and store it in the current thread; using the `operation_id`
  # is safe since it's a unique id used to link events together. Also only one
  # thread is involved in this execution so thread-local storage should be safe. Reference:
  # https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/monitoring.rb#L70
  # https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/monitoring/publishable.rb#L38-L56

  collection = get_collection(event.command)

  attributes = {
    'db.system' => 'mongodb',
    'db.name' => event.database_name,
    'db.operation' => event.command_name,
    'net.peer.name' => event.address.host,
    'net.peer.port' => event.address.port
  }

  config = Mongo::Instrumentation.instance.config
  attributes['peer.service'] = config[:peer_service] if config[:peer_service]
  # attributes['db.statement'] = CommandSerializer.new(event.command).serialize
  attributes['db.statement'] = CommandSerializer.new(event.command).serialize if config[:db_statement] == :include
  attributes['db.mongodb.collection'] = collection if collection
  attributes.compact!

  span = tracer.start_span(span_name(collection, event.command_name), attributes: attributes, kind: :client)
  set_span(event, span)
end

#succeeded(event) ⇒ Object



56
57
58
# File 'lib/opentelemetry/instrumentation/mongo/subscriber.rb', line 56

def succeeded(event)
  finish_event('succeeded', event)
end