class OpenTracing::Instrumentation::Bunny::ConsumeTracer

ConsumeTracer extract parent span from message headers and create span around passed block

Usage:

consumer_tracer = \
  OpenTracing::Instrumentation::Bunny::ConsumeTracer.new

consumer_tracer = \
  OpenTracing::Instrumentation::Bunny::ConsumeTracer.new do |config|
    config.tracer = custom_tracer
  end

queue.subscribe(block: true) do |delivery_info, properties, payload|
  consume_tracer.consume(delivery_info, properties) do
  end
end

Public Class Methods

new(config: ConsumeTracerConfig.new) { |config| ... } click to toggle source

@param config [ConsumeTracerConfig]

# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 26
def initialize(config: ConsumeTracerConfig.new)
  yield config if block_given?
  @config = config
end

Public Instance Methods

consume(delivery_info, properties) { |span_scope| ... } click to toggle source

Extract tracing parent from headers. Create span with tags. If block passed, then span closed after block exit, otherwise return active scope.

@param delivery_info [Bunny::DeliveryInfo] @param properties [Bunny::MessageProperties] @yield if block passed, then it called and after scope closed @yieldparam scope [OpenTracing::Scope] @return [OpenTracing::Scope, nil] return active scope if called without block, otherwise return block result

# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 41
def consume(delivery_info, properties)
  span_scope = safe_start_active_span(delivery_info, properties)
  return span_scope unless block_given?

  handle_error(span_scope) do
    yield span_scope
  end
end

Private Instance Methods

build_operation_name(delivery_info) click to toggle source
# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 89
def build_operation_name(delivery_info)
  operation_name_builder.build_operation_name(delivery_info)
end
build_references(headers) click to toggle source
# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 97
def build_references(headers)
  return if headers.nil?

  span_context = tracer.extract(
    OpenTracing::FORMAT_TEXT_MAP,
    headers,
  )
  return if span_context.nil?

  [OpenTracing::Reference.follows_from(span_context)]
end
build_tags(delivery_info, properties) click to toggle source
# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 93
def build_tags(delivery_info, properties)
  tags_builder.build_tags(delivery_info, properties)
end
handle_error(span_scope) { || ... } click to toggle source
# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 61
def handle_error(span_scope)
  yield
rescue StandardError => e
  error_writer.write_error(span_scope.span, e)
  raise e
ensure
  # Close span if exists
  span_scope&.close
end
safe_start_active_span(delivery_info, properties) click to toggle source
# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 71
def safe_start_active_span(delivery_info, properties)
  start_active_span(delivery_info, properties)
rescue StandardError => e
  logger&.error(e)
  nil
end
start_active_span(delivery_info, properties) click to toggle source
# File lib/opentracing/instrumentation/bunny/consume_tracer.rb, line 78
def start_active_span(delivery_info, properties)
  operation_name = build_operation_name(delivery_info)
  tags = build_tags(delivery_info, properties)
  references = build_references(properties[:headers])
  tracer.start_active_span(
    operation_name,
    tags: tags,
    references: references,
  )
end