Class: Excon::Middleware::NewRelicCrossAppTracing

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/instrumentation/excon/middleware.rb

Constant Summary collapse

TRACE_DATA_IVAR =
:@newrelic_trace_data
INSTRUMENTATION_NAME =
'Excon'

Instance Method Summary collapse

Constructor Details

#initialize(stack) ⇒ NewRelicCrossAppTracing

Returns a new instance of NewRelicCrossAppTracing.



11
12
13
# File 'lib/new_relic/agent/instrumentation/excon/middleware.rb', line 11

def initialize(stack)
  @stack = stack
end

Instance Method Details

#error_call(datum) ⇒ Object



46
47
48
49
# File 'lib/new_relic/agent/instrumentation/excon/middleware.rb', line 46

def error_call(datum)
  finish_trace(datum)
  @stack.error_call(datum)
end

#finish_trace(datum) ⇒ Object

THREAD_LOCAL_ACCESS



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/new_relic/agent/instrumentation/excon/middleware.rb', line 51

def finish_trace(datum) # THREAD_LOCAL_ACCESS
  # The following line needs else branch coverage
  segment = datum[:connection] && datum[:connection].instance_variable_get(TRACE_DATA_IVAR) # rubocop:disable Style/SafeNavigation
  if segment
    begin
      segment.notice_error(datum[:error]) if datum[:error]
      datum[:connection].instance_variable_set(TRACE_DATA_IVAR, nil)

      if datum[:response]
        wrapped_response = ::NewRelic::Agent::HTTPClients::ExconHTTPResponse.new(datum[:response])
        segment.process_response_headers(wrapped_response)
      end
    ensure
      ::NewRelic::Agent::Transaction::Segment.finish(segment)
    end
  end
end

#request_call(datum) ⇒ Object

THREAD_LOCAL_ACCESS



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
# File 'lib/new_relic/agent/instrumentation/excon/middleware.rb', line 15

def request_call(datum) # THREAD_LOCAL_ACCESS
  begin
    # Only instrument this request if we haven't already done so, because
    # we can get request_call multiple times for requests marked as
    # :idempotent in the options, but there will be only a single
    # accompanying response_call/error_call.
    if datum[:connection] && !datum[:connection].instance_variable_get(TRACE_DATA_IVAR)
      NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

      wrapped_request = ::NewRelic::Agent::HTTPClients::ExconHTTPRequest.new(datum)
      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)

      datum[:connection].instance_variable_set(TRACE_DATA_IVAR, segment)
    end
  rescue => e
    NewRelic::Agent.logger.debug(e)
  end
  @stack.request_call(datum)
end

#response_call(datum) ⇒ Object



41
42
43
44
# File 'lib/new_relic/agent/instrumentation/excon/middleware.rb', line 41

def response_call(datum)
  finish_trace(datum)
  @stack.response_call(datum)
end