class OpenTracing::Instrumentation::Bunny::PublishTracer

PublishTracer trace publishing and inject trace headers into AMQP message

Usage:

exchange = channel.topic(BUNNY_EXCHANGE_NAME)
default_publisher \
  = OpenTracing::Instrumentation::Bunny::PublishTracer.new(exchange)
configured_publisher = \
  OpenTracing::Instrumentation::Bunny::PublishTracer.new(exchange) do |c|
    c.tracer = custom_tracer
  end
publisher.publish(
  '{"message": 123}',
  routing_key: 'key',
)

Attributes

exchange[R]

Public Class Methods

new(exchange, config: PublishTracerConfig.new) { |config| ... } click to toggle source

@param exchange [Bunny::Exchange]

# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 25
def initialize(exchange, config: PublishTracerConfig.new)
  @exchange = exchange
  yield config if block_given?
  @config = config.dup
end

Public Instance Methods

publish( payload, active_span: tracer.active_span, **opts ) click to toggle source

Publish message via call `exchange.publish`, with injecting tracing into headers and create span around publising call.

For deatils see: www.rubydoc.info/gems/bunny/Bunny/Exchange#publish-instance_method

@param active [OpenTracing::Span] allow overide span @return [Bunny::Exchange]

# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 39
def publish(
  payload,
  active_span: tracer.active_span,
  **opts
)
  trace_publish(span: active_span, **opts) do |scope|
    publish_with_headers(
      payload,
      span: scope&.span || active_span,
      **opts,
    )
  end
end

Private Instance Methods

build_operation_name(opts) click to toggle source
# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 112
def build_operation_name(opts)
  operation_name_builder.build_operation_name(exchange, opts)
end
build_tags(opts) click to toggle source
# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 116
def build_tags(opts)
  tags_builder.build_tags(exchange, opts)
end
publish_with_headers( payload, span:, headers: {}, **opts ) click to toggle source
# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 71
def publish_with_headers(
  payload,
  span:,
  headers: {},
  **opts
)
  safe_inject_headers(span: span, headers: headers)
  exchange.publish(
    payload,
    headers: headers,
    **opts,
  )
end
safe_inject_headers(span:, headers:) click to toggle source
# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 67
def safe_inject_headers(span:, headers:)
  injector.inject(headers, active_span: span)
end
safe_start_active_span(span:, **opts) click to toggle source
# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 96
def safe_start_active_span(span:, **opts)
  start_active_span(span: span, **opts)
rescue StandardError
  nil
end
start_active_span(span:, **opts) click to toggle source
# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 102
def start_active_span(span:, **opts)
  operation_name = build_operation_name(opts)
  tags = build_tags(opts)
  tracer.start_active_span(
    operation_name,
    tags: tags,
    child_of: span,
  )
end
trace_publish(span:, **opts) { |scope| ... } click to toggle source
# File lib/opentracing/instrumentation/bunny/publish_tracer.rb, line 85
def trace_publish(span:, **opts)
  scope = safe_start_active_span(span: span, **opts)
  yield scope
rescue StandardError => e
  error_writer.write_error(scope.span, e) if scope
  logger&.error(e)
  raise e
ensure
  scope&.close
end