module Datadog::Tracing::Contrib::HTTPX::Plugin::RequestTracer

  1. lib/httpx/adapters/datadog.rb

Constants

SPAN_REQUEST = "httpx.request"  

Public Instance methods

call(request)

initializes tracing on the request.

[show source]
   # File lib/httpx/adapters/datadog.rb
41 def call(request)
42   return unless configuration(request).enabled
43 
44   span = nil
45 
46   # request objects are reused, when already buffered requests get rerouted to a different
47   # connection due to connection issues, or when they already got a response, but need to
48   # be retried. In such situations, the original span needs to be extended for the former,
49   # while a new is required for the latter.
50   request.on(:idle) do
51     span = nil
52   end
53   # the span is initialized when the request is buffered in the parser, which is the closest
54   # one gets to actually sending the request.
55   request.on(:headers) do
56     next if span
57 
58     span = initialize_span(request, now)
59   end
60 
61   request.on(:response) do |response|
62     unless span
63       next unless response.is_a?(::HTTPX::ErrorResponse) && response.error.respond_to?(:connection)
64 
65       # handles the case when the +error+ happened during name resolution, which means
66       # that the tracing start point hasn't been triggered yet; in such cases, the approximate
67       # initial resolving time is collected from the connection, and used as span start time,
68       # and the tracing object in inserted before the on response callback is called.
69       span = initialize_span(request, response.error.connection.init_time)
70 
71     end
72 
73     finish(response, span)
74   end
75 end
configuration(request)
[show source]
    # File lib/httpx/adapters/datadog.rb
133 def configuration(request)
134   Datadog.configuration.tracing[:httpx, request.uri.host]
135 end
create_span(request, configuration, start_time)
[show source]
    # File lib/httpx/adapters/datadog.rb
142 def create_span(request, configuration, start_time)
143   Datadog::Tracing.trace(
144     SPAN_REQUEST,
145     service: service_name(request.uri.host, configuration),
146     type: TYPE_OUTBOUND,
147     start_time: start_time
148   )
149 end
finish(response, span)
[show source]
   # File lib/httpx/adapters/datadog.rb
77 def finish(response, span)
78   if response.is_a?(::HTTPX::ErrorResponse)
79     span.set_error(response.error)
80   else
81     span.set_tag(TAG_STATUS_CODE, response.status.to_s)
82 
83     span.set_error(::HTTPX::HTTPError.new(response)) if response.status >= 400 && response.status <= 599
84   end
85 
86   span.finish
87 end
initialize_span(request, start_time)

return a span initialized with the +@request+ state.

[show source]
    # File lib/httpx/adapters/datadog.rb
 90 def initialize_span(request, start_time)
 91   verb = request.verb
 92   uri = request.uri
 93 
 94   config = configuration(request)
 95 
 96   span = create_span(request, config, start_time)
 97 
 98   span.resource = verb
 99 
100   # Add additional request specific tags to the span.
101 
102   span.set_tag(TAG_URL, request.path)
103   span.set_tag(TAG_METHOD, verb)
104 
105   span.set_tag(TAG_TARGET_HOST, uri.host)
106   span.set_tag(TAG_TARGET_PORT, uri.port)
107 
108   # Tag as an external peer service
109   span.set_tag(TAG_PEER_SERVICE, span.service)
110 
111   if config[:distributed_tracing]
112     propagate_trace_http(
113       Datadog::Tracing.active_trace,
114       request.headers
115     )
116   end
117 
118   # Set analytics sample rate
119   if Contrib::Analytics.enabled?(config[:analytics_enabled])
120     Contrib::Analytics.set_sample_rate(span, config[:analytics_sample_rate])
121   end
122 
123   span
124 rescue StandardError => e
125   Datadog.logger.error("error preparing span for http request: #{e}")
126   Datadog.logger.error(e.backtrace)
127 end
now()
[show source]
    # File lib/httpx/adapters/datadog.rb
129 def now
130   ::Datadog::Core::Utils::Time.now.utc
131 end
propagate_trace_http(trace, headers)
[show source]
    # File lib/httpx/adapters/datadog.rb
138 def propagate_trace_http(trace, headers)
139   Datadog::Tracing::Contrib::HTTP.inject(trace, headers)
140 end