Module: OpenTelemetry::Instrumentation::HTTP::Patches::Client

Defined in:
lib/hypertrace/instrumentation/http_patch.rb

Instance Method Summary collapse

Instance Method Details

#perform(req, options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hypertrace/instrumentation/http_patch.rb', line 2

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.find{|k, v| k.downcase == "content-type"}&.last
  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.find{|k, v| k.downcase == "content-type"}&.last
      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