Module: NewRelic::Agent::Instrumentation::Bunny::Exchange

Includes:
NewRelic::Agent::Instrumentation::Bunny
Included in:
Bunny::Prepend::Exchange
Defined in:
lib/new_relic/agent/instrumentation/bunny/instrumentation.rb

Constant Summary

Constants included from NewRelic::Agent::Instrumentation::Bunny

DEFAULT_NAME, DEFAULT_TYPE, INSTRUMENTATION_NAME, LIBRARY, SLASH

Instance Method Summary collapse

Methods included from NewRelic::Agent::Instrumentation::Bunny

exchange_name, exchange_type

Instance Method Details

#publish_with_tracing(payload, opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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