class OpenTracing::Instrumentation::ObjectWrapper

ObjectWrapper allow trace call methods on any ruby object.

Usage:

wrapped_object = OpenTracing::Instrumentation::ObjectWrapper.new(other_object)
wrapped_object.other_object_method

Constants

DEFAULT_OPERATOIN_NAME_PATTERN

Attributes

object[R]

wrapped object

operation_name_pattern[R]
tracer[R]

Public Class Methods

new( object, tracer: OpenTracing.global_tracer, operation_name_pattern: DEFAULT_OPERATOIN_NAME_PATTERN ) click to toggle source
# File lib/opentracing/instrumentation/object_wrapper.rb, line 17
def initialize(
  object,
  tracer: OpenTracing.global_tracer,
  operation_name_pattern: DEFAULT_OPERATOIN_NAME_PATTERN
)
  @object = object
  @tracer = tracer
  @operation_name_pattern = operation_name_pattern
end

Public Instance Methods

method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/opentracing/instrumentation/object_wrapper.rb, line 34
def method_missing(method_name, *args)
  return super unless object.respond_to?(method_name)

  operation_name = buid_operation_name(method_name)
  tags = build_tags(method_name)
  tracer.start_active_span(operation_name, tags: tags) do |scope|
    call_method(scope.span, method_name, *args)
  end
end
respond_to_missing?(method_name, *) click to toggle source
# File lib/opentracing/instrumentation/object_wrapper.rb, line 30
def respond_to_missing?(method_name, *)
  object.respond_to?(method_name)
end

Private Instance Methods

buid_operation_name(method_name) click to toggle source
# File lib/opentracing/instrumentation/object_wrapper.rb, line 46
def buid_operation_name(method_name)
  format(
    operation_name_pattern,
    class_name: class_name,
    method: method_name,
  )
end
build_tags(method) click to toggle source
# File lib/opentracing/instrumentation/object_wrapper.rb, line 65
def build_tags(method)
  {
    'object.class' => class_name,
    'object.method' => method.to_s,
  }
end
call_method(span, method_name, *args) click to toggle source
# File lib/opentracing/instrumentation/object_wrapper.rb, line 54
def call_method(span, method_name, *args)
  object.send(method_name, *args)
rescue StandardError => e
  span.set_tag('error', true)
  raise e
end
class_name() click to toggle source
# File lib/opentracing/instrumentation/object_wrapper.rb, line 61
def class_name
  @class_name ||= object.class.to_s
end