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



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

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.



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

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],
    type: Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND,
    **trace_options
  )
  datadog_trace = Tracing.active_trace

  datadog_tag_request

  if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(datadog_trace)
    datadog_trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
  end

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

#datadog_span_started?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/datadog/tracing/contrib/ethon/easy_patch.rb', line 124

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

#headers=(headers) ⇒ Object



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

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



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

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



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

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

  datadog_before_request
  super
end

#resetObject



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

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