Module: NewRelic::Agent::Instrumentation::HTTPX

Included in:
Prepend
Defined in:
lib/new_relic/agent/instrumentation/httpx/chain.rb,
lib/new_relic/agent/instrumentation/httpx/prepend.rb,
lib/new_relic/agent/instrumentation/httpx/instrumentation.rb

Defined Under Namespace

Modules: Chain, Prepend

Constant Summary collapse

INSTRUMENTATION_NAME =
'HTTPX'
NOTICEABLE_ERROR_CLASS =
'HTTPX::Error'

Instance Method Summary collapse

Instance Method Details

#nr_finish_segmentObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/new_relic/agent/instrumentation/httpx/instrumentation.rb', line 31

def nr_finish_segment
  proc do |request, segment|
    response = @responses[request]

    unless response
      NewRelic::Agent.logger.debug('Processed an on-response callback for HTTPX but could not find the response!')
      next
    end

    wrapped_response = NewRelic::Agent::HTTPClients::HTTPXHTTPResponse.new(response)
    segment.process_response_headers(wrapped_response)

    if response.is_a?(::HTTPX::ErrorResponse)
      e = NewRelic::Agent::NoticeableError.new(NOTICEABLE_ERROR_CLASS, "Couldn't connect: #{response}")
      segment.notice_error(e)
    end

    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
end

#nr_start_segment(request) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/new_relic/agent/instrumentation/httpx/instrumentation.rb', line 17

def nr_start_segment(request)
  return unless NewRelic::Agent::Tracer.state.is_execution_traced?

  wrapped_request = NewRelic::Agent::HTTPClients::HTTPXHTTPRequest.new(request)
  segment = NewRelic::Agent::Tracer.start_external_request_segment(
    library: wrapped_request.type,
    uri: wrapped_request.uri,
    procedure: wrapped_request.method
  )
  segment.add_request_headers(wrapped_request)

  request.on(:response) { nr_finish_segment.call(request, segment) }
end

#send_requests_with_tracing(*requests) ⇒ Object



11
12
13
14
15
# File 'lib/new_relic/agent/instrumentation/httpx/instrumentation.rb', line 11

def send_requests_with_tracing(*requests)
  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
  requests.each { |r| nr_start_segment(r) }
  yield
end