Module: Datadog::Contrib::Ethon::EasyPatch::InstanceMethods

Defined in:
lib/ddtrace/contrib/ethon/easy_patch.rb

Overview

InstanceMethods - implementing instrumentation

Instance Method Summary collapse

Instance Method Details

#completeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 39

def complete
  return super unless tracer_enabled?
  begin
    response_options = mirror.options
    response_code = (response_options[:response_code] || response_options[:code]).to_i
    if response_code.zero?
      return_code = response_options[:return_code]
      message = return_code ? ::Ethon::Curl.easy_strerror(return_code) : 'unknown reason'
      set_span_error_message("Request has failed: #{message}")
    else
      @datadog_span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, response_code)
      if Datadog::Ext::HTTP::ERROR_RANGE.cover?(response_code)
        set_span_error_message("Request has failed with HTTP error: #{response_code}")
      end
    end
  ensure
    @datadog_span.finish
    @datadog_span = nil
  end
  super
end

#datadog_before_request(parent_span: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 71

def datadog_before_request(parent_span: nil)
  @datadog_span = datadog_configuration[:tracer].trace(
    Ext::SPAN_REQUEST,
    service: datadog_configuration[:service_name],
    span_type: Datadog::Ext::HTTP::TYPE_OUTBOUND
  )
  @datadog_span.parent = parent_span unless parent_span.nil?

  datadog_tag_request

  if datadog_configuration[:distributed_tracing]
    @datadog_original_headers ||= {}
    Datadog::HTTPPropagator.inject!(@datadog_span.context, @datadog_original_headers)
    self.headers = @datadog_original_headers
  end
end

#datadog_span_started?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 88

def datadog_span_started?
  instance_variable_defined?(:@datadog_span) && !@datadog_span.nil?
end

#headers=(headers) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 25

def headers=(headers)
  return super unless tracer_enabled?

  # Store headers to call this method again when span is ready
  @datadog_original_headers = headers
  super
end

#http_request(url, action_name, options = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 17

def http_request(url, action_name, options = {})
  return super unless tracer_enabled?

  # It's tricky to get HTTP method from libcurl
  @datadog_method = action_name.to_s.upcase
  super
end

#performObject



33
34
35
36
37
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 33

def perform
  return super unless tracer_enabled?
  datadog_before_request
  super
end

#resetObject



61
62
63
64
65
66
67
68
69
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 61

def reset
  super
ensure
  if tracer_enabled?
    @datadog_span = nil
    @datadog_method = nil
    @datadog_original_headers = nil
  end
end