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
50 def call(request)
51   return unless configuration(request).enabled
52 
53   span = nil
54 
55   # request objects are reused, when already buffered requests get rerouted to a different
56   # connection due to connection issues, or when they already got a response, but need to
57   # be retried. In such situations, the original span needs to be extended for the former,
58   # while a new is required for the latter.
59   request.on(:idle) do
60     span = nil
61   end
62   # the span is initialized when the request is buffered in the parser, which is the closest
63   # one gets to actually sending the request.
64   request.on(:headers) do
65     next if span
66 
67     span = initialize_span(request, now)
68   end
69 
70   request.on(:response) do |response|
71     span = initialize_span(request, request.init_time) if !span && request.init_time
72 
73     finish(response, span)
74   end
75 end
configuration(request)
[show source]
    # File lib/httpx/adapters/datadog.rb
149 def configuration(request)
150   Datadog.configuration.tracing[:httpx, request.uri.host]
151 end
create_span(request, configuration, start_time)
[show source]
    # File lib/httpx/adapters/datadog.rb
158 def create_span(request, configuration, start_time)
159   Datadog::Tracing.trace(
160     SPAN_REQUEST,
161     service: service_name(request.uri.host, configuration),
162     type: TYPE_OUTBOUND,
163     start_time: start_time
164   )
165 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 
85     span.set_tags(
86       Datadog.configuration.tracing.header_tags.response_tags(response.headers.to_h)
87     ) if Datadog.configuration.tracing.respond_to?(:header_tags)
88   end
89 
90   span.finish
91 end
initialize_span(request, start_time)

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

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