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

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

Overview

InstanceMethods - implementing instrumentation

Instance Method Summary collapse

Methods included from HttpAnnotationHelper

#service_name

Instance Method Details

#completeObject



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
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 46

def complete
  return super unless Tracing.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(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response_code)
      if Tracing::Metadata::Ext::HTTP::ERROR_RANGE.cover?(response_code)
        set_span_error_message("Request has failed with HTTP error: #{response_code}")
      end
    end

    @datadog_span.set_tags(
      Datadog.configuration.tracing.header_tags.response_tags(
        Core::Utils::Hash::CaseInsensitiveWrapper.new(parse_response_headers)
      )
    )
  ensure
    @datadog_span.finish
    @datadog_span = nil
  end
  super
end

#datadog_before_request(continue_from: nil) ⇒ Object

Starts or retrieves the already started Easy request span.

When tracing in Multi request context, child spans for each Easy request are created early, and then finished as their HTTP response becomes available. Because many Easy requests are open at the same time, many Datadog::Tracing::Spans are also open at the same time. To avoid each separate Easy request becoming the parented to the previous open request we set the parent_span parameter with the parent Multi span. This correctly assigns all open Easy spans to the currently executing Multi context.

Parameters:

  • continue_from (Datadog::Tracing::Span) (defaults to: nil)

    the Multi span, if executing in a Multi context.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 95

def datadog_before_request(continue_from: nil)
  load_datadog_configuration_for(url)

  trace_options = continue_from ? { continue_from: continue_from } : {}
  uri = try_parse_uri

  @datadog_span = Tracing.trace(
    Ext::SPAN_REQUEST,
    service: uri ? service_name(uri.host, datadog_configuration) : datadog_configuration[:service_name],
    span_type: Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND,
    **trace_options
  )
  datadog_trace = Tracing.active_trace

  datadog_tag_request

  if datadog_configuration[:distributed_tracing]
    @datadog_original_headers ||= {}
    Tracing::Propagation::HTTP.inject!(datadog_trace, @datadog_original_headers)
    self.headers = @datadog_original_headers
  end
end

#datadog_span_started?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 118

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

#headers=(headers) ⇒ Object



32
33
34
35
36
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 32

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



23
24
25
26
27
28
29
30
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 23

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

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

#performObject



38
39
40
41
42
43
44
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 38

def perform
  load_datadog_configuration_for(url)
  return super unless Tracing.enabled?

  datadog_before_request
  super
end

#resetObject



75
76
77
78
79
80
81
82
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 75

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