Class: MongoDB::Instrumentation::CommandSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/mongodb/instrumentation/command_subscriber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer: OpenTracing.global_tracer) ⇒ CommandSubscriber

Returns a new instance of CommandSubscriber.


7
8
9
10
11
# File 'lib/mongodb/instrumentation/command_subscriber.rb', line 7

def initialize(tracer: OpenTracing.global_tracer)
  @tracer = tracer

  @requests = {}
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests


5
6
7
# File 'lib/mongodb/instrumentation/command_subscriber.rb', line 5

def requests
  @requests
end

Instance Method Details

#failed(event) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongodb/instrumentation/command_subscriber.rb', line 45

def failed(event)
  return if @requests[event.request_id].nil?

  # tag the reported duration and any error message that came through
  span = @requests[event.request_id]
  span.set_tag("took.ms", event.duration * 1000)
  span.set_tag("error", true)
  span.log_kv(key: "message", value: event.message)

  span.finish()
  @requests.delete(event.request_id)
end

#started(event) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongodb/instrumentation/command_subscriber.rb', line 13

def started(event)
  # start command span
  tags = {
    # opentracing tags
    'component' => 'ruby-mongodb',
    'db.instance' => event.database_name,
    'db.statement' => event.command,
    'db.type' => 'mongo',
    'span.kind' => 'client',

    # extra info
    'mongo.command.name' => event.command_name,
    'mongo.operation.id' => event.operation_id,
    'mongo.request.id' => event.request_id,
  }
  span =@tracer.start_span(event.command_name, tags: tags)

  @requests[event.request_id] = span
end

#succeeded(event) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mongodb/instrumentation/command_subscriber.rb', line 33

def succeeded(event)
  return if @requests[event.request_id].nil?

  # tag the reported duration, in case it differs from what we saw
  # through the notifications times
  span = @requests[event.request_id]
  span.set_tag("took.ms", event.duration * 1000)

  span.finish()
  @requests.delete(event.request_id)
end