class OpenTracing::Instrumentation::Mongo::TraceSubscriber

TraceSubscriber trace mongo requests

Constants

ERROR_TAG

Attributes

monitor[R]
operation_name_builder[R]
sanitazer[R]
scope_store[R]
tracer[R]

Public Class Methods

new( tracer: OpenTracing.global_tracer, scope_store: {}, operation_name_builder: OperationNameBuilder.new, sanitazer: QuerySanitazer.new ) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 13
def initialize(
  tracer: OpenTracing.global_tracer,
  scope_store: {},
  operation_name_builder: OperationNameBuilder.new,
  sanitazer: QuerySanitazer.new
)
  @tracer = tracer
  @monitor = Monitor.new
  @scope_store = scope_store
  @operation_name_builder = operation_name_builder
  @sanitazer = sanitazer
end

Public Instance Methods

failed(event) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 39
def failed(event)
  tag_error(event.operation_id, event.message, event.failure)
  clean_scope(event.operation_id)
end
started(event) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 26
def started(event)
  scope = tracer.start_active_span(
    build_operation_name(event),
    tags: base_tags(event).merge(mongo_tags(event)),
  )

  store_scope(event.operation_id, scope)
end
succeeded(event) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 35
def succeeded(event)
  clean_scope(event.operation_id)
end

Private Instance Methods

base_tags(event) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 56
def base_tags(event)
  {
    'component' => 'db',

    'db.type' => 'mongo',
    'db.instance' => event.address.to_s,
  }
end
build_operation_name(event) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 52
def build_operation_name(event)
  operation_name_builder.build_operation_name(event)
end
clean_scope(operation_id) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 85
def clean_scope(operation_id)
  monitor.synchronize do
    scope_store
      .delete(operation_id)
      .close
  end
end
mongo_tags(event) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 65
def mongo_tags(event)
  collection_name = event.command[event.command_name]
  command_args = sanitazer.sanitaze(event.command, event.command_name)

  {
    'mongo.request_id' => event.request_id,
    'mongo.operation_id' => event.operation_id,
    'mongo.database_name' => event.database_name,
    'mongo.collection_name' => collection_name,
    'mongo.command_name' => event.command_name,
    'mongo.command_args' => JSON.dump(command_args),
  }
end
store_scope(operation_id, scope) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 79
def store_scope(operation_id, scope)
  monitor.synchronize do
    scope_store[operation_id] = scope
  end
end
tag_error(operation_id, message, object) click to toggle source
# File lib/opentracing/instrumentation/mongo/trace_subscriber.rb, line 95
def tag_error(operation_id, message, object)
  monitor.synchronize do
    scope_store[operation_id]
      .span
      .set_tag(ERROR_TAG, true)
      .log_kv(
        'error.kind': message,
        'error.object': JSON.dump(object),
      )
  end
end