Class: NewRelic::Agent::SyntheticsMonitor

Inherits:
InboundRequestMonitor show all
Defined in:
lib/new_relic/agent/monitors/synthetics_monitor.rb

Constant Summary collapse

SYNTHETICS_HEADER_KEY =
'HTTP_X_NEWRELIC_SYNTHETICS'
SYNTHETICS_INFO_HEADER_KEY =
'HTTP_X_NEWRELIC_SYNTHETICS_INFO'
NON_HTTP_SYNTHETICS_HEADER_KEY =
'NewRelicSynthetics'
SUPPORTED_VERSION =
1
EXPECTED_PAYLOAD_LENGTH =
5

Instance Attribute Summary

Attributes inherited from InboundRequestMonitor

#obfuscator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InboundRequestMonitor

#deserialize_header, #initialize, #setup_obfuscator

Constructor Details

This class inherits a constructor from NewRelic::Agent::InboundRequestMonitor

Class Method Details

.is_supported_version?(incoming_payload) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/new_relic/agent/monitors/synthetics_monitor.rb', line 46

def is_supported_version?(incoming_payload)
  incoming_payload.first == SUPPORTED_VERSION
end

.is_trusted?(incoming_payload) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/new_relic/agent/monitors/synthetics_monitor.rb', line 50

def is_trusted?(incoming_payload)
   = incoming_payload[1]
  Agent.config[:trusted_account_ids].include?()
end

.is_valid_payload?(incoming_payload) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/new_relic/agent/monitors/synthetics_monitor.rb', line 55

def is_valid_payload?(incoming_payload)
  incoming_payload.length == EXPECTED_PAYLOAD_LENGTH
end

.reject_messaging_synthetics_header(headers) ⇒ Object



59
60
61
# File 'lib/new_relic/agent/monitors/synthetics_monitor.rb', line 59

def reject_messaging_synthetics_header(headers)
  headers.reject { |k, _| k == NON_HTTP_SYNTHETICS_HEADER_KEY }
end

Instance Method Details

#load_json(header, key) ⇒ Object



38
39
40
41
42
43
# File 'lib/new_relic/agent/monitors/synthetics_monitor.rb', line 38

def load_json(header, key)
  ::JSON.parse(header)
rescue => err
  NewRelic::Agent.logger.debug("Failure loading json header '#{key}' in #{self.class}, #{err.class}, #{err.message}")
  nil
end

#on_before_call(request) ⇒ Object

THREAD_LOCAL_ACCESS



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/new_relic/agent/monitors/synthetics_monitor.rb', line 19

def on_before_call(request) # THREAD_LOCAL_ACCESS
  encoded_header = request[SYNTHETICS_HEADER_KEY]
  info_header = request[SYNTHETICS_INFO_HEADER_KEY]
  return unless encoded_header

  incoming_payload = deserialize_header(encoded_header, SYNTHETICS_HEADER_KEY)

  return unless incoming_payload &&
    SyntheticsMonitor.is_valid_payload?(incoming_payload) &&
    SyntheticsMonitor.is_supported_version?(incoming_payload) &&
    SyntheticsMonitor.is_trusted?(incoming_payload)

  txn = Tracer.current_transaction
  txn.raw_synthetics_header = encoded_header
  txn.raw_synthetics_info_header = info_header
  txn.synthetics_payload = incoming_payload
  txn.synthetics_info_payload = load_json(info_header, SYNTHETICS_INFO_HEADER_KEY)
end

#on_finished_configuring(events) ⇒ Object



15
16
17
# File 'lib/new_relic/agent/monitors/synthetics_monitor.rb', line 15

def on_finished_configuring(events)
  events.subscribe(:before_call, &method(:on_before_call))
end