Module: NewRelic::Agent::Messaging

Extended by:
Messaging
Included in:
Messaging
Defined in:
lib/new_relic/agent/messaging.rb

Overview

This module contains helper methods to facilitate instrumentation of message brokers.

Instance Method Summary collapse

Instance Method Details

#start_amqp_consume_segment(library:, destination_name:, delivery_info:, message_properties:, exchange_type: nil, queue_name: nil, start_time: nil) ⇒ NewRelic::Agent::Transaction::MessageBrokerSegment

Start a MessageBroker segment configured to trace an AMQP consume. Finishing this segment will handle timing and recording of the proper metrics for New Relic's messaging features. This method is a convenience wrapper around NewRelic::Agent::Tracer.start_message_broker_segment.

Parameters:

  • library (String)

    The name of the library being instrumented

  • destination_name (String)

    Name of destination (exchange name)

  • delivery_info (Hash)

    Metadata about how the message was delivered

  • message_properties (Hash)

    AMQP-specific metadata about the message including headers and opaque application-level data

  • exchange_type (String) (defaults to: nil)

    Type of exchange which determines how messages are routed (optional)

  • queue_name (String) (defaults to: nil)

    The name of the queue the message was consumed from (optional)

  • start_time (Time) (defaults to: nil)

    An instance of Time class denoting the start time of the segment. Value is set by AbstractSegment#start if not given. (optional)

Returns:

  • (NewRelic::Agent::Transaction::MessageBrokerSegment)

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/new_relic/agent/messaging.rb', line 242

def start_amqp_consume_segment(library:,
  destination_name:,
  delivery_info:,
  message_properties:,
  exchange_type: nil,
  queue_name: nil,
  start_time: nil)

  segment = Tracer.start_message_broker_segment(
    action: :consume,
    library: library,
    destination_name: destination_name,
    destination_type: :exchange,
    headers: message_properties[:headers],
    start_time: start_time
  )

  if segment_parameters_enabled?
    if message_properties[:headers] && !message_properties[:headers].empty?
      non_cat_headers = CrossAppTracing.reject_messaging_cat_headers message_properties[:headers]
      non_synth_headers = SyntheticsMonitor.reject_messaging_synthetics_header non_cat_headers
      segment.params[:headers] = non_synth_headers unless non_synth_headers.empty?
    end

    segment.params[:routing_key] = delivery_info[:routing_key] if delivery_info[:routing_key]
    segment.params[:reply_to] = message_properties[:reply_to] if message_properties[:reply_to]
    segment.params[:queue_name] = queue_name if queue_name
    segment.params[:exchange_type] = exchange_type if exchange_type
    segment.params[:exchange_name] = delivery_info[:exchange_name] if delivery_info[:exchange_name]
    segment.params[:correlation_id] = message_properties[:correlation_id] if message_properties[:correlation_id]
  end

  segment
end

#start_amqp_publish_segment(library:, destination_name:, headers: nil, routing_key: nil, reply_to: nil, correlation_id: nil, exchange_type: nil) ⇒ NewRelic::Agent::Transaction::MessageBrokerSegment

Start a MessageBroker segment configured to trace an AMQP publish. Finishing this segment will handle timing and recording of the proper metrics for New Relic's messaging features. This method is a convenience wrapper around NewRelic::Agent::Tracer.start_message_broker_segment.

Parameters:

  • library (String)

    The name of the library being instrumented

  • destination_name (String)

    Name of destination (exchange name)

  • headers (Hash) (defaults to: nil)

    The message headers

  • routing_key (String) (defaults to: nil)

    The routing key used for the message (optional)

  • reply_to (String) (defaults to: nil)

    A routing key for use in RPC-models for the receiver to publish a response to (optional)

  • correlation_id (String) (defaults to: nil)

    An application-generated value to link up request and responses in RPC-models (optional)

  • exchange_type (String) (defaults to: nil)

    Type of exchange which determines how message are routed (optional)

Returns:

  • (NewRelic::Agent::Transaction::MessageBrokerSegment)

Raises:

  • (ArgumentError)

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/new_relic/agent/messaging.rb', line 183

def start_amqp_publish_segment(library:,
  destination_name:,
  headers: nil,
  routing_key: nil,
  reply_to: nil,
  correlation_id: nil,
  exchange_type: nil)

  raise ArgumentError, 'missing required argument: headers' if headers.nil? && CrossAppTracing.cross_app_enabled?

  original_headers = headers.nil? ? nil : headers.dup

  segment = Tracer.start_message_broker_segment(
    action: :produce,
    library: library,
    destination_type: :exchange,
    destination_name: destination_name,
    headers: headers
  )

  if segment_parameters_enabled?
    segment.params[:headers] = original_headers if original_headers && !original_headers.empty?
    segment.params[:routing_key] = routing_key if routing_key
    segment.params[:reply_to] = reply_to if reply_to
    segment.params[:correlation_id] = correlation_id if correlation_id
    segment.params[:exchange_type] = exchange_type if exchange_type
  end

  segment
end

#start_message_broker_segment(action: nil, library: nil, destination_type: nil, destination_name: nil, headers: nil, parameters: nil, start_time: nil) ⇒ NewRelic::Agent::Transaction::MessageBrokerSegment

Start a MessageBroker segment configured to trace a messaging action. Finishing this segment will handle timing and recording of the proper metrics for New Relic's messaging features..

Parameters:

  • action (Symbol) (defaults to: nil)

    The message broker action being traced (see NewRelic::Agent::Transaction::MessageBrokerSegment::ACTIONS) for all options.

  • library (String) (defaults to: nil)

    The name of the library being instrumented

  • destination_type (Symbol) (defaults to: nil)

    Type of destination (see NewRelic::Agent::Transaction::MessageBrokerSegment::DESTINATION_TYPES) for all options.

  • destination_name (String) (defaults to: nil)

    Name of destination (queue or exchange name)

  • headers (Hash) (defaults to: nil)

    Metadata about the message and opaque application-level data (optional)

  • parameters (Hash) (defaults to: nil)

    A hash of parameters to be attached to this segment (optional)

  • start_time (Time) (defaults to: nil)

    An instance of Time class denoting the start time of the segment. Value is set by AbstractSegment#start if not given. (optional)

Returns:

  • (NewRelic::Agent::Transaction::MessageBrokerSegment)

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/new_relic/agent/messaging.rb', line 55

def start_message_broker_segment(action: nil,
  library: nil,
  destination_type: nil,
  destination_name: nil,
  headers: nil,
  parameters: nil,
  start_time: nil)

  Tracer.start_message_broker_segment(
    action: action,
    library: library,
    destination_type: destination_type,
    destination_name: destination_name,
    headers: headers,
    parameters: parameters,
    start_time: start_time
  )
end

#wrap_amqp_consume_transaction(library: nil, destination_name: nil, delivery_info: nil, message_properties: nil, exchange_type: nil, queue_name: nil, &block) ⇒ Object

Wrap a MessageBroker transaction trace around a AMQP messaging handling block. This API is intended to be used in AMQP-specific library instrumentation when a “push”-style callback is invoked to handle an incoming message.

Parameters:

  • library (String) (defaults to: nil)

    The name of the library being instrumented

  • destination_name (String) (defaults to: nil)

    Name of destination (queue or exchange name)

  • message_properties (Hash) (defaults to: nil)

    Metadata about the message and opaque application-level data (optional)

  • exchange_type (Symbol) (defaults to: nil)

    Type of AMQP exchange the message was recieved from (see NewRelic::Agent::Transaction::MessageBrokerSegment::DESTINATION_TYPES)

  • queue_name (String) (defaults to: nil)

    name of the AMQP queue on which the message was received

  • &block (Proc)

    The block should handle calling the original subscribed callback function

Returns:

  • return value of given block, which will be the same as the return value of an un-instrumented subscribed callback


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/new_relic/agent/messaging.rb', line 303

def wrap_amqp_consume_transaction library: nil,
  destination_name: nil,
  delivery_info: nil,
  message_properties: nil,
  exchange_type: nil,
  queue_name: nil,
  &block

  wrap_message_broker_consume_transaction library: library,
    destination_type: :exchange,
    destination_name: Instrumentation::Bunny.exchange_name(destination_name),
    routing_key: delivery_info[:routing_key],
    reply_to: message_properties[:reply_to],
    queue_name: queue_name,
    exchange_type: exchange_type,
    headers: message_properties[:headers],
    correlation_id: message_properties[:correlation_id], &block
end

#wrap_message_broker_consume_transaction(library:, destination_type:, destination_name:, headers: nil, routing_key: nil, queue_name: nil, exchange_type: nil, reply_to: nil, correlation_id: nil) ⇒ Object

Wrap a MessageBroker transaction trace around a messaging handling block. This API is intended to be used in library instrumentation when a “push”- style callback is invoked to handle an incoming message.

Parameters:

  • library (String)

    The name of the library being instrumented

  • destination_type (Symbol)

    Type of destination (see NewRelic::Agent::Transaction::MessageBrokerSegment::DESTINATION_TYPES) for all options.

  • destination_name (String)

    Name of destination (queue or exchange name)

  • headers (Hash) (defaults to: nil)

    Metadata about the message and opaque application-level data (optional)

  • routing_key (String) (defaults to: nil)

    Value used by AMQP message brokers to route messages to queues

  • queue_name (String) (defaults to: nil)

    Name of AMQP queue that received the message (optional)

  • exchange_type (Symbol) (defaults to: nil)

    Type of last AMQP exchange to deliver the message (optional)

  • reply_to (String) (defaults to: nil)

    Routing key to be used to send AMQP-based RPC response messages (optional)

  • correlation_id (String) (defaults to: nil)

    Application-level value used to correlate AMQP-based RPC response messages to request messages (optional)

  • &block (Proc)

    The block should handle calling the original subscribed callback function

Returns:

  • return value of given block, which will be the same as the return value of an un-instrumented subscribed callback


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/new_relic/agent/messaging.rb', line 113

def wrap_message_broker_consume_transaction(library:,
  destination_type:,
  destination_name:,
  headers: nil,
  routing_key: nil,
  queue_name: nil,
  exchange_type: nil,
  reply_to: nil,
  correlation_id: nil)

  state = Tracer.state
  return yield if state.current_transaction
  txn = nil

  begin
    txn_name = transaction_name library, destination_type, destination_name

    txn = Tracer.start_transaction name: txn_name, category: :message

    if headers
      txn.distributed_tracer.consume_message_headers headers, state, RABBITMQ_TRANSPORT_TYPE
      CrossAppTracing.reject_messaging_cat_headers(headers).each do |k, v|
        txn.add_agent_attribute :"message.headers.#{k}", v, AttributeFilter::DST_NONE unless v.nil?
      end
    end

    txn.add_agent_attribute :'message.routingKey', routing_key, ATTR_DESTINATION if routing_key
    txn.add_agent_attribute :'message.exchangeType', exchange_type, AttributeFilter::DST_NONE if exchange_type
    txn.add_agent_attribute :'message.correlationId', correlation_id, AttributeFilter::DST_NONE if correlation_id
    txn.add_agent_attribute :'message.queueName', queue_name, ATTR_DESTINATION if queue_name
    txn.add_agent_attribute :'message.replyTo', reply_to, AttributeFilter::DST_NONE if reply_to
  rescue => e
    NewRelic::Agent.logger.error "Error starting Message Broker consume transaction", e
  end

  yield
ensure
  begin
    txn.finish if txn
  rescue => e
    NewRelic::Agent.logger.error "Error stopping Message Broker consume transaction", e
  end
end