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

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

[show source]
    # File lib/httpx/adapters/datadog.rb
 99 def initialize_span(request, start_time)
100   verb = request.verb
101   uri = request.uri
102 
103   config = configuration(request)
104 
105   span = create_span(request, config, start_time)
106 
107   span.resource = verb
108 
109   # Tag original global service name if not used
110   span.set_tag(TAG_BASE_SERVICE, Datadog.configuration.service) if span.service != Datadog.configuration.service
111 
112   span.set_tag(TAG_KIND, TAG_CLIENT)
113 
114   span.set_tag(TAG_COMPONENT, "httpx")
115   span.set_tag(TAG_OPERATION, "request")
116 
117   span.set_tag(TAG_URL, request.path)
118   span.set_tag(TAG_METHOD, verb)
119 
120   span.set_tag(TAG_TARGET_HOST, uri.host)
121   span.set_tag(TAG_TARGET_PORT, uri.port)
122 
123   span.set_tag(TAG_PEER_HOSTNAME, uri.host)
124 
125   # Tag as an external peer service
126   # span.set_tag(TAG_PEER_SERVICE, span.service)
127 
128   if config[:distributed_tracing]
129     propagate_trace_http(
130       Datadog::Tracing.active_trace,
131       request.headers
132     )
133   end
134 
135   # Set analytics sample rate
136   if Contrib::Analytics.enabled?(config[:analytics_enabled])
137     Contrib::Analytics.set_sample_rate(span, config[:analytics_sample_rate])
138   end
139 
140   span.set_tags(
141     Datadog.configuration.tracing.header_tags.request_tags(request.headers.to_h)
142   )
143 
144   span
145 rescue StandardError => e
146   Datadog.logger.error("error preparing span for http request: #{e}")
147   Datadog.logger.error(e.backtrace)
148 end
now()
[show source]
    # File lib/httpx/adapters/datadog.rb
150 def now
151   ::Datadog::Core::Utils::Time.now.utc
152 end
propagate_trace_http(trace, headers)
[show source]
    # File lib/httpx/adapters/datadog.rb
159 def propagate_trace_http(trace, headers)
160   Datadog::Tracing::Contrib::HTTP.inject(trace, headers)
161 end