class TRACING_MODULE::Contrib::HTTPX::Plugin::RequestTracer

def build_span

def build_span
  TRACING_MODULE.trace(
    SPAN_REQUEST,
    service: service_name(@request.uri.host, configuration, Datadog.configuration_for(self)),
    span_type: TYPE_OUTBOUND
  )
end

def build_span

def build_span
  service_name = configuration[:split_by_domain] ? @request.uri.host : configuration[:service_name]
  configuration[:tracer].trace(
    SPAN_REQUEST,
    service: service_name,
    span_type: TYPE_OUTBOUND
  )
end

def call

def call
  return unless tracing_enabled?
  @request.on(:response, &method(:finish))
  verb = @request.verb
  uri = @request.uri
  @span = build_span
  @span.resource = verb
  # Add additional request specific tags to the span.
  @span.set_tag(TAG_URL, @request.path)
  @span.set_tag(TAG_METHOD, verb)
  @span.set_tag(TAG_TARGET_HOST, uri.host)
  @span.set_tag(TAG_TARGET_PORT, uri.port.to_s)
  # Tag as an external peer service
  @span.set_tag(TAG_PEER_SERVICE, @span.service)
  propagate_headers if @configuration[:distributed_tracing]
  # Set analytics sample rate
  if Contrib::Analytics.enabled?(@configuration[:analytics_enabled])
    Contrib::Analytics.set_sample_rate(@span, @configuration[:analytics_sample_rate])
  end
rescue StandardError => e
  Datadog.logger.error("error preparing span for http request: #{e}")
  Datadog.logger.error(e.backtrace)
end

def configuration

def configuration
  @configuration ||= Datadog.configuration.tracing[:httpx, @request.uri.host]
end

def configuration

def configuration
  @configuration ||= Datadog.configuration[:httpx, @request.uri.host]
end

def finish(response)

def finish(response)
  return unless @span
  if response.is_a?(::HTTPX::ErrorResponse)
    @span.set_error(response.error)
  else
    @span.set_tag(TAG_STATUS_CODE, response.status.to_s)
    @span.set_error(::HTTPX::HTTPError.new(response)) if response.status >= 400 && response.status <= 599
  end
  @span.finish
end

def initialize(request)

def initialize(request)
  @request = request
end

def propagate_headers

def propagate_headers
  TRACING_MODULE::Propagation::HTTP.inject!(TRACING_MODULE.active_trace, @request.headers)
end

def propagate_headers

def propagate_headers
  Datadog::HTTPPropagator.inject!(@span.context, @request.headers)
end

def tracing_enabled?

def tracing_enabled?
  TRACING_MODULE.enabled?
end

def tracing_enabled?

def tracing_enabled?
  configuration[:tracer].enabled
end