Class: NewRelic::Agent::Transaction::ExternalRequestSegment

Inherits:
Segment show all
Defined in:
lib/new_relic/agent/transaction/external_request_segment.rb

Overview

This class represents an external segment in a transaction trace.

Constant Summary collapse

NR_SYNTHETICS_HEADER =
'X-NewRelic-Synthetics'
APP_DATA_KEY =
'NewRelicAppData'
EXTERNAL_ALL =
'External/all'
EXTERNAL_ALL_WEB =
'External/allWeb'
EXTERNAL_ALL_OTHER =
'External/allOther'
MISSING_STATUS_CODE =
'MissingHTTPStatusCode'

Constants inherited from AbstractSegment

AbstractSegment::INSPECT_IGNORE

Instance Attribute Summary collapse

Attributes inherited from Segment

#custom_transaction_attributes, #unscoped_metrics

Attributes inherited from AbstractSegment

#children_time, #duration, #end_time, #exclusive_duration, #guid, #noticed_error, #parent, #record_on_finish, #record_scoped_metric, #start_time, #starting_segment_key, #transaction, #transaction_name

Instance Method Summary collapse

Methods inherited from Segment

#add_agent_attribute, #add_custom_attributes, #attributes, finish, #merge_untrusted_agent_attributes, merge_untrusted_agent_attributes

Methods inherited from AbstractSegment

#all_code_information_present?, #children_time_ranges?, #code_attributes, #code_information=, #concurrent_children?, #finalize, #finish, #finished?, #inspect, #notice_error, #noticed_error_attributes, #params, #params?, #record_metrics?, #record_on_finish?, #record_scoped_metric?, #set_noticed_error, #start, #time_range, #transaction_assigned

Constructor Details

#initialize(library, uri, procedure, start_time = nil) ⇒ ExternalRequestSegment

:nodoc:



27
28
29
30
31
32
33
34
35
36
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 27

def initialize(library, uri, procedure, start_time = nil) # :nodoc:
  @library = library
  @uri = HTTPClients::URIUtil.obfuscated_uri(uri)
  @procedure = procedure
  @host_header = nil
  @app_data = nil
  @http_status_code = nil
  @record_agent_attributes = false
  super(nil, nil, start_time)
end

Instance Attribute Details

#http_status_codeObject (readonly)



24
25
26
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 24

def http_status_code
  @http_status_code
end

#libraryObject (readonly)



24
25
26
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 24

def library
  @library
end

#procedureObject (readonly)



24
25
26
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 24

def procedure
  @procedure
end

#record_agent_attributes=(value) ⇒ Object (writeonly)



25
26
27
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 25

def record_agent_attributes=(value)
  @record_agent_attributes = value
end

#uriObject (readonly)



24
25
26
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 24

def uri
  @uri
end

Instance Method Details

#add_request_headers(request) ⇒ Object

This method adds New Relic request headers to a given request made to an external API and checks to see if a host header is used for the request. If a host header is used, it updates the segment name to match the host header.

object (must belong to a subclass of NewRelic::Agent::HTTPClients::AbstractRequest)

Parameters:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 63

def add_request_headers(request)
  process_host_header(request)
  synthetics_header = transaction&.raw_synthetics_header
  insert_synthetics_header(request, synthetics_header) if synthetics_header

  return unless record_metrics?

  transaction.distributed_tracer.insert_headers(request)
rescue => e
  NewRelic::Agent.logger.error('Error in add_request_headers', e)
end

#cross_app_request?Boolean

:nodoc:

Returns:

  • (Boolean)


101
102
103
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 101

def cross_app_request? # :nodoc:
  !!@app_data
end

#cross_process_idObject

:nodoc:



105
106
107
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 105

def cross_process_id # :nodoc:
  @app_data && @app_data[0]
end

#cross_process_transaction_nameObject

:nodoc:



113
114
115
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 113

def cross_process_transaction_name # :nodoc:
  @app_data && @app_data[1]
end

#get_request_metadataString

Obtain an obfuscated String suitable for delivery across public networks that identifies this application and transaction to another application which is also running a New Relic agent. This String can be processed by process_request_metadata on the receiving application.

Returns:

  • (String)

    obfuscated request metadata to send



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
156
157
158
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 125

def 
  NewRelic::Agent.record_api_supportability_metric(:get_request_metadata)
  return unless CrossAppTracing.cross_app_enabled?

  if transaction

    # build hash of CAT metadata
    #
    rmd = {
      NewRelicID: NewRelic::Agent.config[:cross_process_id],
      NewRelicTransaction: [
        transaction.guid,
        false,
        transaction.distributed_tracer.cat_trip_id,
        transaction.distributed_tracer.cat_path_hash
      ]
    }

    # flag cross app in the state so transaction knows to add bits to payload
    #
    transaction.distributed_tracer.is_cross_app_caller = true

    # add Synthetics header if we have it
    #
    rmd[:NewRelicSynthetics] = transaction.raw_synthetics_header if transaction.raw_synthetics_header

    # obfuscate the generated request metadata JSON
    #
    obfuscator.obfuscate(::JSON.dump(rmd))

  end
rescue => e
  NewRelic::Agent.logger.error('error during get_request_metadata', e)
end

#hostObject

:nodoc:



42
43
44
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 42

def host # :nodoc:
  @host_header || uri.host
end

#nameObject

:nodoc:



38
39
40
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 38

def name # :nodoc:
  @name ||= "External/#{host}/#{library}/#{procedure}"
end

#process_response_headers(response) ⇒ Object

:nodoc:



193
194
195
196
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 193

def process_response_headers(response) # :nodoc:
  set_http_status_code(response)
  read_response_headers(response)
end

#process_response_metadata(response_metadata) ⇒ Object

Process obfuscated String sent from a called application that is also running a New Relic agent and save information in current transaction for inclusion in a trace. This String is generated by get_response_metadata on the receiving application.

Parameters:

  • response_metadata (String)

    received obfuscated response metadata



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 168

def ()
  NewRelic::Agent.record_api_supportability_metric(:process_response_metadata)
  if transaction
    app_data = ::JSON.parse(obfuscator.deobfuscate())[APP_DATA_KEY]

    # validate cross app id
    #
    if Array === app_data and CrossAppTracing.trusted_valid_cross_app_id?(app_data[0])
      @app_data = app_data
      update_segment_name
    else
      NewRelic::Agent.logger.error('error processing response metadata: invalid/non-trusted ID')
    end
  end

  nil
rescue => e
  NewRelic::Agent.logger.error('error during process_response_metadata', e)
end

#read_response_headers(response) ⇒ Object

This method extracts app data from an external response if present. If a valid cross-app ID is found, the name of the segment is updated to reflect the cross-process ID and transaction name.

Parameters:

  • response (Hash)

    a hash of response headers



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 82

def read_response_headers(response)
  return unless record_metrics? && CrossAppTracing.cross_app_enabled?
  return unless CrossAppTracing.response_has_crossapp_header?(response)

  unless data = CrossAppTracing.extract_appdata(response)
    NewRelic::Agent.logger.debug("Couldn't extract_appdata from external segment response")
    return
  end

  if CrossAppTracing.valid_cross_app_id?(data[0])
    @app_data = data
    update_segment_name
  else
    NewRelic::Agent.logger.debug('External segment response has invalid cross_app_id')
  end
rescue => e
  NewRelic::Agent.logger.error('Error in read_response_headers', e)
end

#record_agent_attributes?Boolean

By default external request segments only have errors and the http url recorded as agent attributes. To have all the agent attributes recorded, use the attr_writer like so ‘segment.record_agent_attributes = true` See: SpanEventPrimitive#for_external_request_segment

Returns:

  • (Boolean)


50
51
52
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 50

def record_agent_attributes?
  @record_agent_attributes
end

#record_metricsObject



188
189
190
191
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 188

def record_metrics
  add_unscoped_metrics
  super
end

#transaction_guidObject

:nodoc:



109
110
111
# File 'lib/new_relic/agent/transaction/external_request_segment.rb', line 109

def transaction_guid # :nodoc:
  @app_data && @app_data[5]
end