Module: NewRelic::Agent::Instrumentation::Typhoeus

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

Defined Under Namespace

Modules: Chain, Prepend

Constant Summary collapse

HYDRA_SEGMENT_NAME =
'External/Multiple/Typhoeus::Hydra/run'
NOTICEABLE_ERROR_CLASS =
'Typhoeus::Errors::TyphoeusError'
EARLIEST_VERSION =
Gem::Version.new('0.5.3')
INSTRUMENTATION_NAME =
NewRelic::Agent.base_name(name)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.is_supported_version?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb', line 14

def self.is_supported_version?
  Gem::Version.new(::Typhoeus::VERSION) >= EARLIEST_VERSION
end

.request_is_hydra_enabled?(request) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb', line 18

def self.request_is_hydra_enabled?(request)
  request.respond_to?(:hydra) && request.hydra
end

.response_message(response) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb', line 22

def self.response_message(response)
  if response.respond_to?(:response_message)
    response.response_message
  elsif response.respond_to?(:return_message)
    response.return_message
  else
    # 0.5.4 seems to have lost xxxx_message methods altogether.
    'timeout'
  end
end

.trace(request) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb', line 45

def self.trace(request)
  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  state = NewRelic::Agent::Tracer.state
  return unless state.is_execution_traced?

  wrapped_request = ::NewRelic::Agent::HTTPClients::TyphoeusHTTPRequest.new(request)

  parent = if request_is_hydra_enabled?(request)
    request.hydra.instance_variable_get(:@__newrelic_hydra_segment)
  end

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

  segment.add_request_headers(wrapped_request)

  callback = proc do
    wrapped_response = HTTPClients::TyphoeusHTTPResponse.new(request.response)

    segment.process_response_headers(wrapped_response)

    if request.response.code == 0
      segment.notice_error(NoticeableError.new(NOTICEABLE_ERROR_CLASS, response_message(request.response)))
    end

    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
  request.on_complete.unshift(callback)
rescue => e
  NewRelic::Agent.logger.error('Exception during trace setup for Typhoeus request', e)
end

Instance Method Details

#with_tracingObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb', line 33

def with_tracing
  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  segment = NewRelic::Agent::Tracer.start_segment(name: HYDRA_SEGMENT_NAME)
  instance_variable_set(:@__newrelic_hydra_segment, segment)
  begin
    yield
  ensure
    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
end