Sha256: 027cb5825436048a691103e25feb8f89426b6bb1dfe54d610d59578dbedf06fd

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module OpenTracing
  module Instrumentation
    # ObjectWrapper allow trace call methods on any ruby object.
    #
    # Usage:
    #   wrapped_object = OpenTracing::Instrumentation::ObjectWrapper.new(other_object)
    #   wrapped_object.other_object_method
    class ObjectWrapper
      DEFAULT_COMMAND_NAME = 'method_call'

      attr_reader :tracer,
                  :command_name

      def initialize(
        object,
        tracer: OpenTracing.global_tracer,
        command_name: DEFAULT_COMMAND_NAME
      )
        @object = object
        @tracer = tracer
        @command_name = command_name
      end

      # wrapped object
      attr_reader :object

      def respond_to_missing?(method_name, *)
        object.response_to?(method_name)
      end

      def method_missing(method_name, *args)
        return super unless object.respond_to?(method_name)

        tags = build_tags(method_name)
        tracer.start_active_span(command_name, tags: tags) do |scope|
          call_method(scope.span, method_name, *args)
        end
      end

      private

      def call_method(span, method_name, *args)
        object.send(method_name, *args)
      rescue StandardError => e
        span.set_tag('error', true)
        raise e
      end

      def build_tags(method)
        {
          'object.class' => object.class.to_s,
          'object.method' => method.to_s,
        }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
opentracing-instrumentation-0.1.8 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.7 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.6 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.5 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.4 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.3 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.2 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.1 lib/opentracing/instrumentation/object_wrapper.rb
opentracing-instrumentation-0.1.0 lib/opentracing/instrumentation/object_wrapper.rb