Sha256: 28178d05d6c21d12daa280869090733abcf17fef83ec324afab7a21d628847bc

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module OpenTelemetry::Instrumentation::HTTP::Patches::Client
  def perform(req, options)
    uri = req.uri
    request_method = req.verb.to_s.upcase

    headers = req.headers.to_h
    attrs = Hypertrace::Instrumentation::DataCapture.headers_to_attribute_keys(headers,
                                                                                      Hypertrace::Instrumentation::DataCapture::TYPE_REQUEST)
    content_type = headers['Content-Type']
    if Hypertrace::Instrumentation::DataCapture.can_capture?(content_type, Hypertrace::Instrumentation::DataCapture::TYPE_REQUEST)
      body_cap = Hypertrace::Instrumentation::DataCapture.capturable_body(req.body.source)
      attrs['http.request.body'] = body_cap if body_cap
    end

    attributes = {
      'http.method' => request_method,
      'http.scheme' => uri.scheme,
      'http.target' => uri.path,
      'http.url' => uri.to_s,
      'net.peer.name' => uri.host,
      'net.peer.port' => uri.port
    }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes).merge!(attrs)

    tracer.in_span("HTTP #{request_method}", attributes: attributes, kind: :client) do |span|
      OpenTelemetry.propagation.inject(req.headers)
      super.tap do |response|
        response_headers = response.headers.to_h
        Hypertrace::Instrumentation::DataCapture.headers_to_attribute_keys(response_headers,
                                                                           Hypertrace::Instrumentation::DataCapture::TYPE_RESPONSE) do |k, v|
          span.set_attribute(k, v)
        end
        content_type = response_headers['Content-Type']
        if Hypertrace::Instrumentation::DataCapture.can_capture?(content_type, Hypertrace::Instrumentation::DataCapture::TYPE_RESPONSE)
          body_cap = Hypertrace::Instrumentation::DataCapture.capturable_body(response.body.to_s)
          span.set_attribute('http.response.body', body_cap) if body_cap
        end
        annotate_span_with_response!(span, response)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hypertrace-agent-0.1.0 lib/hypertrace/instrumentation/http_patch.rb