module NewRelic::Agent::Instrumentation::Bunny::Exchange

Public Instance Methods

publish_with_tracing(payload, opts = {}) { || ... } click to toggle source
# File lib/new_relic/agent/instrumentation/bunny/instrumentation.rb, line 31
def publish_with_tracing(payload, opts = {})
  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  begin
    destination = exchange_name(name)

    tracing_enabled =
      NewRelic::Agent::CrossAppTracing.cross_app_enabled? ||
      NewRelic::Agent.config[:'distributed_tracing.enabled']
    opts[:headers] ||= {} if tracing_enabled

    segment = NewRelic::Agent::Messaging.start_amqp_publish_segment(
      library: LIBRARY,
      destination_name: destination,
      headers: opts[:headers],
      routing_key: opts[:routing_key] || opts[:key],
      reply_to: opts[:reply_to],
      correlation_id: opts[:correlation_id],
      exchange_type: type
    )
    if segment
      segment.add_agent_attribute('server.address', channel&.connection&.hostname)
      segment.add_agent_attribute('server.port', channel&.connection&.port)
      segment.add_agent_attribute('messaging.destination.name', destination) # for produce, this is exchange name
      segment.add_agent_attribute('messaging.rabbitmq.destination.routing_key', opts[:routing_key])
    end
  rescue => e
    NewRelic::Agent.logger.error('Error starting message broker segment in Bunny::Exchange#publish', e)
    yield
  else
    NewRelic::Agent::Tracer.capture_segment_error(segment) do
      yield
    end
  ensure
    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
end