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

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

Overview

InstanceMethods - implementing instrumentation

Instance Method Summary collapse

Methods included from HttpAnnotationHelper

#service_name

Instance Method Details

#completeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 45

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 77

def datadog_before_request(parent_span: nil)
  load_datadog_configuration_for(url)
  @datadog_span = datadog_configuration[:tracer].trace(
    Ext::SPAN_REQUEST,
    service: uri ? service_name(uri.host, datadog_configuration) : 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)


95
96
97
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 95

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

#headers=(headers) ⇒ Object



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

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

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



22
23
24
25
26
27
28
29
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 22

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

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

#performObject



37
38
39
40
41
42
43
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 37

def perform
  load_datadog_configuration_for(url)
  return super unless tracer_enabled?

  datadog_before_request
  super
end

#resetObject



68
69
70
71
72
73
74
75
# File 'lib/ddtrace/contrib/ethon/easy_patch.rb', line 68

def reset
  super
ensure
  @datadog_span = nil
  @datadog_method = nil
  @datadog_original_headers = nil
  @datadog_configuration = nil
end