Module: NewRelic::Agent::Instrumentation::NetHTTP

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

Defined Under Namespace

Modules: Chain, Prepend

Constant Summary collapse

INSTRUMENTATION_NAME =
NewRelic::Agent.base_name(name)

Instance Method Summary collapse

Instance Method Details

#request_with_tracing(request) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/new_relic/agent/instrumentation/net_http/instrumentation.rb', line 11

def request_with_tracing(request)
  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  wrapped_request = NewRelic::Agent::HTTPClients::NetHTTPRequest.new(self, request)

  segment = NewRelic::Agent::Tracer.start_external_request_segment(
    library: wrapped_request.type,
    uri: wrapped_request.uri,
    procedure: wrapped_request.method
  )

  begin
    response = nil
    segment.add_request_headers(wrapped_request)

    # RUBY-1244 Disable further tracing in request to avoid double
    # counting if connection wasn't started (which calls request again).
    NewRelic::Agent.disable_all_tracing do
      response = NewRelic::Agent::Tracer.capture_segment_error(segment) do
        yield
      end
    end

    wrapped_response = NewRelic::Agent::HTTPClients::NetHTTPResponse.new(response)

    if NewRelic::Agent::LLM.openai_parent?(segment)
      NewRelic::Agent::LLM.populate_openai_response_headers(wrapped_response, segment.parent)
    end

    segment.process_response_headers(wrapped_response)

    response
  ensure
    segment&.finish
  end
end